You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/csp/domain.rkt

70 lines
2.2 KiB
Racket

10 years ago
#lang racket/base
(require racket/class racket/list "helper.rkt")
(provide (all-defined-out))
(define domain%
10 years ago
;; Class used to control possible values for variables
;; When list or tuples are used as domains, they are automatically
;; converted to an instance of that class.
(class* object% (printable<%>)
(super-new)
(init-field set)
(field [_list set][_hidden null][_states null])
(define (repr) (format "<Domain ~v>" _list))
(define/public (custom-print out quoting-depth) (print (repr) out))
(define/public (custom-display out) (displayln (repr) out))
(define/public (custom-write out) (write (repr) out))
10 years ago
(define/public (reset-state)
10 years ago
;; Reset to the original domain state, including all possible values
(py-extend! _list _hidden)
(set! _hidden null)
(set! _states null))
10 years ago
(define/public (push-state)
10 years ago
;; Save current domain state
;; Variables hidden after that call are restored when that state
;; is popped from the stack.
(py-append! _states (length _list)))
10 years ago
(define/public (pop-state)
10 years ago
;; Restore domain state from the top of the stack
;; Variables hidden since the last popped state are then available
;; again.
(define diff (- (py-pop! _states) (length _list)))
(when (not (= 0 diff))
(py-extend! _list (take-right _hidden diff))
(set! _hidden (take _hidden (- (length _hidden) diff)))))
10 years ago
(define/public (hide-value value)
10 years ago
;; Hide the given value from the domain
;; After that call the given value won't be seen as a possible value
;; on that domain anymore. The hidden value will be restored when the
;; previous saved state is popped.
(set! _list (remove value _list))
(py-append! _hidden value))
10 years ago
(define/public (get-values)
_list)
(define/public (values-empty?)
(null? _list))
10 years ago
(define/public (domain-pop!)
(py-pop! _list))
(define/public (copy)
(define copied-domain (new domain% [set _list]))
10 years ago
(set-field! _hidden copied-domain _hidden)
(set-field! _states copied-domain _states)
copied-domain)
))
(define domain%? (is-a?/c domain%))
10 years ago