Skip to content

Commit

Permalink
Check if contexts are of the same sequence.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramarren committed Sep 24, 2009
1 parent e887f71 commit f034dd9
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions contexts.lisp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(in-package :parser-combinators)

(defclass context ()
((cache :accessor cache-of :initarg :cache :initform (make-hash-table))
((sequence-id :accessor sequence-id-of :initarg :sequence-id :initform (gensym))
(cache :accessor cache-of :initarg :cache :initform (make-hash-table))
(storage :accessor storage-of :initarg :storage :initform nil)
(position :accessor position-of :initarg :position :initform 0)
(length :accessor length-of :initarg :length :initform 0)))
Expand All @@ -11,6 +12,8 @@
(defgeneric make-context (sequence))
(defgeneric context-interval (context1 context2 &optional result-type)
(:method ((context1 context) (context2 context) &optional (result-type 'string))
(assert (eql (sequence-id-of context1)
(sequence-id-of context2)))
(assert (<= (position-of context1)
(position-of context2)))
(if (= (position-of context1) (position-of context2))
Expand Down Expand Up @@ -45,18 +48,21 @@
(make-instance 'list-context :storage list :length (length list))))

(defmethod context-next ((context list-context))
(with-accessors ((cache cache-of) (storage storage-of) (position position-of) (length length-of))
(with-accessors ((cache cache-of) (storage storage-of) (position position-of)
(length length-of) (sequence-id sequence-id-of))
context
(let ((new-position (1+ position)))
(or (gethash new-position cache)
(setf (gethash new-position cache)
(if (= new-position length)
(make-instance 'end-context
:sequence-id sequence-id
:position new-position
:length length
:cache cache
:storage nil)
(make-instance 'list-context
:sequence-id sequence-id
:storage (cdr storage)
:position new-position
:length length
Expand All @@ -71,7 +77,7 @@
(defmethod make-context ((vector vector))
(if (zerop (length vector))
(make-instance 'end-context)
(make-instance 'vector-context :storage vector :length (length vector))))
(make-instance 'vector-context :storage vector :length (length vector) :sequence-id vector)))

(defmethod context-next ((context vector-context))
(with-accessors ((cache cache-of) (storage storage-of) (position position-of) (length length-of))
Expand All @@ -81,11 +87,13 @@
(setf (gethash new-position cache)
(if (= new-position length)
(make-instance 'end-context
:sequence-id storage
:position new-position
:length length
:cache cache
:storage nil)
(make-instance 'vector-context
:sequence-id storage
:storage storage
:position new-position
:length length
Expand Down

0 comments on commit f034dd9

Please sign in to comment.