@@ -9,10 +9,10 @@ use crate::unify_log as ul;
99
1010use ena:: undo_log:: { Rollback , Snapshots , UndoLogs } ;
1111
12- enum UndoLog < K : ut:: UnifyKey , I > {
12+ pub enum UndoLog < K : ut:: UnifyKey , I = K > {
1313 Relation ( sv:: UndoLog < ut:: Delegate < K > > ) ,
1414 UnifyLog ( ul:: Undo < I > ) ,
15- ModifiedSet ( ms:: Undo ) ,
15+ ModifiedSet ( ms:: Undo < I > ) ,
1616}
1717
1818impl < K : ut:: UnifyKey , I > From < sv:: UndoLog < ut:: Delegate < K > > > for UndoLog < K , I > {
@@ -27,8 +27,8 @@ impl<K: ut::UnifyKey, I> From<ul::Undo<I>> for UndoLog<K, I> {
2727 }
2828}
2929
30- impl < K : ut:: UnifyKey , I > From < ms:: Undo > for UndoLog < K , I > {
31- fn from ( l : ms:: Undo ) -> Self {
30+ impl < K : ut:: UnifyKey , I > From < ms:: Undo < I > > for UndoLog < K , I > {
31+ fn from ( l : ms:: Undo < I > ) -> Self {
3232 UndoLog :: ModifiedSet ( l)
3333 }
3434}
5656 self . logs . push ( undo. into ( ) )
5757 }
5858 }
59+ fn clear ( & mut self ) {
60+ self . logs . clear ( ) ;
61+ self . num_open_snapshots = 0 ;
62+ }
5963 fn extend < J > ( & mut self , undos : J )
6064 where
6165 Self : Sized ,
6771 }
6872}
6973
70- struct RollbackView < ' a , K : ut:: UnifyKey , I : Idx > {
71- relations : & ' a mut ut:: UnificationStorage < K > ,
72- unify_log : & ' a mut ul:: UnifyLog < I > ,
73- modified_set : & ' a mut ms:: ModifiedSet < I > ,
74- }
75-
76- impl < K : ut:: UnifyKey , I : Idx > Rollback < UndoLog < K , I > > for RollbackView < ' _ , K , I > {
74+ impl < K : ut:: UnifyKey , I : Idx > Rollback < UndoLog < K , I > > for LoggedUnificationTableStorage < K , I > {
7775 fn reverse ( & mut self , undo : UndoLog < K , I > ) {
7876 match undo {
7977 UndoLog :: Relation ( undo) => self . relations . reverse ( undo) ,
@@ -93,12 +91,18 @@ impl<K: ut::UnifyKey, I: Idx> Snapshots<UndoLog<K, I>> for Logs<K, I> {
9391 unreachable ! ( )
9492 }
9593
96- fn rollback_to ( & mut self , values : & mut impl Rollback < UndoLog < K , I > > , snapshot : Self :: Snapshot ) {
94+ fn rollback_to < R > ( & mut self , values : impl FnOnce ( ) -> R , snapshot : Self :: Snapshot )
95+ where
96+ R : Rollback < UndoLog < K , I > > ,
97+ {
9798 debug ! ( "rollback_to({})" , snapshot. undo_len) ;
9899 self . assert_open_snapshot ( & snapshot) ;
99100
100- while self . logs . len ( ) > snapshot. undo_len {
101- values. reverse ( self . logs . pop ( ) . unwrap ( ) ) ;
101+ if self . logs . len ( ) > snapshot. undo_len {
102+ let mut values = values ( ) ;
103+ while self . logs . len ( ) > snapshot. undo_len {
104+ values. reverse ( self . logs . pop ( ) . unwrap ( ) ) ;
105+ }
102106 }
103107
104108 if self . num_open_snapshots == 1 {
@@ -135,14 +139,18 @@ impl<K: ut::UnifyKey, I: Idx> Logs<K, I> {
135139 }
136140}
137141
138- pub struct LoggedUnificationTable < K : ut:: UnifyKey , I : Idx = K > {
139- relations : ut:: UnificationStorage < K > ,
142+ pub struct LoggedUnificationTableStorage < K : ut:: UnifyKey , I : Idx = K > {
143+ relations : ut:: UnificationTableStorage < K > ,
140144 unify_log : ul:: UnifyLog < I > ,
141145 modified_set : ms:: ModifiedSet < I > ,
142- undo_log : Logs < K , I > ,
143146}
144147
145- impl < K , I > LoggedUnificationTable < K , I >
148+ pub struct LoggedUnificationTable < ' a , K : ut:: UnifyKey , I : Idx , L > {
149+ storage : & ' a mut LoggedUnificationTableStorage < K , I > ,
150+ undo_log : L ,
151+ }
152+
153+ impl < K , I > LoggedUnificationTableStorage < K , I >
146154where
147155 K : ut:: UnifyKey + From < I > ,
148156 I : Idx + From < K > ,
@@ -152,14 +160,34 @@ where
152160 relations : Default :: default ( ) ,
153161 unify_log : ul:: UnifyLog :: new ( ) ,
154162 modified_set : ms:: ModifiedSet :: new ( ) ,
155- undo_log : Logs :: default ( ) ,
156163 }
157164 }
158165
166+ pub fn with_log < L > ( & mut self , undo_log : L ) -> LoggedUnificationTable < ' _ , K , I , L > {
167+ LoggedUnificationTable { storage : self , undo_log }
168+ }
169+ }
170+
171+ impl < K , I , L > LoggedUnificationTable < ' _ , K , I , L >
172+ where
173+ K : ut:: UnifyKey ,
174+ I : Idx ,
175+ {
176+ pub fn len ( & self ) -> usize {
177+ self . storage . relations . len ( )
178+ }
179+ }
180+
181+ impl < K , I , L > LoggedUnificationTable < ' _ , K , I , L >
182+ where
183+ K : ut:: UnifyKey + From < I > ,
184+ I : Idx + From < K > ,
185+ L : UndoLogs < ms:: Undo < I > > + UndoLogs < ul:: Undo < I > > + UndoLogs < sv:: UndoLog < ut:: Delegate < K > > > ,
186+ {
159187 fn relations (
160188 & mut self ,
161- ) -> ut:: UnificationTable < ut:: InPlace < K , & mut ut:: UnificationStorage < K > , & mut Logs < K , I > > > {
162- ut:: UnificationTable :: with_log ( & mut self . relations , & mut self . undo_log )
189+ ) -> ut:: UnificationTable < ut:: InPlace < K , & mut ut:: UnificationStorage < K > , & mut L > > {
190+ ut:: UnificationTable :: with_log ( & mut self . storage . relations , & mut self . undo_log )
163191 }
164192
165193 pub fn unify ( & mut self , a : I , b : I )
@@ -173,9 +201,9 @@ where
173201 where
174202 K :: Value : ut:: UnifyValue < Error = ut:: NoError > ,
175203 {
176- if self . unify_log . needs_log ( vid) {
204+ if self . storage . unify_log . needs_log ( vid) {
177205 warn ! ( "ModifiedSet {:?} => {:?}" , vid, ty) ;
178- self . modified_set . set ( & mut self . undo_log , vid) ;
206+ self . storage . modified_set . set ( & mut self . undo_log , vid) ;
179207 }
180208 let vid = vid. into ( ) ;
181209 let mut relations = self . relations ( ) ;
@@ -195,8 +223,8 @@ where
195223 value : K :: Value ,
196224 ) -> Result < ( ) , <K :: Value as ut:: UnifyValue >:: Error > {
197225 let vid = self . find ( vid) . into ( ) ;
198- if self . unify_log . needs_log ( vid) {
199- self . modified_set . set ( & mut self . undo_log , vid) ;
226+ if self . storage . unify_log . needs_log ( vid) {
227+ self . storage . modified_set . set ( & mut self . undo_log , vid) ;
200228 }
201229 self . relations ( ) . unify_var_value ( vid, value)
202230 }
@@ -212,9 +240,9 @@ where
212240 relations. unify_var_var ( a, b) ?;
213241
214242 if a == relations. find ( a) {
215- self . unify_log . unify ( & mut self . undo_log , a. into ( ) , b. into ( ) ) ;
243+ self . storage . unify_log . unify ( & mut self . undo_log , a. into ( ) , b. into ( ) ) ;
216244 } else {
217- self . unify_log . unify ( & mut self . undo_log , b. into ( ) , a. into ( ) ) ;
245+ self . storage . unify_log . unify ( & mut self . undo_log , b. into ( ) , a. into ( ) ) ;
218246 }
219247 Ok ( ( ) )
220248 }
@@ -240,14 +268,11 @@ where
240268 self . relations ( ) . new_key ( value)
241269 }
242270
243- pub fn len ( & self ) -> usize {
244- self . relations . len ( )
245- }
246-
247271 pub fn vars_since_snapshot ( & mut self , s : & Snapshot < K , I > ) -> Range < K > {
248272 K :: from ( I :: new ( s. value_count ) ) ..K :: from ( I :: new ( self . relations ( ) . len ( ) ) )
249273 }
250274
275+ /* FIXME
251276 pub fn snapshot(&mut self) -> Snapshot<K, I> {
252277 self.undo_log.num_open_snapshots += 1;
253278 Snapshot {
@@ -258,44 +283,44 @@ where
258283 }
259284
260285 pub fn rollback_to(&mut self, snapshot: Snapshot<K, I>) {
261- let Self { relations, unify_log, modified_set, .. } = self ;
286+ let UnificationTableStorage { relations, unify_log, modified_set, .. } = self.storage ;
262287
263- self . undo_log
264- . rollback_to ( & mut RollbackView { relations, unify_log, modified_set } , snapshot) ;
288+ self.undo_log.rollback_to(|| RollbackView { relations, unify_log, modified_set }, snapshot);
265289
266290 if self.undo_log.num_open_snapshots == 0 {
267- self . modified_set . clear ( ) ;
291+ self.storage. modified_set.clear();
268292 }
269293 }
270294
271295 pub fn commit(&mut self, snapshot: Snapshot<K, I>) {
272296 self.undo_log.commit(snapshot);
273297
274- if self . undo_log . num_open_snapshots == 0 {
275- self . modified_set . clear ( ) ;
298+ if self.undo_log.num_open_snapshots() == 0 {
299+ self.storage. modified_set.clear();
276300 }
277301 }
302+ */
278303
279304 pub fn register ( & mut self ) -> ms:: Offset < I > {
280- self . modified_set . register ( )
305+ self . storage . modified_set . register ( )
281306 }
282307
283308 pub fn deregister ( & mut self , offset : ms:: Offset < I > ) {
284- self . modified_set . deregister ( offset) ;
309+ self . storage . modified_set . deregister ( offset) ;
285310 }
286311
287312 pub fn watch_variable ( & mut self , index : I ) {
288313 debug_assert ! ( index == self . relations( ) . find( index) . into( ) ) ;
289- self . unify_log . watch_variable ( index)
314+ self . storage . unify_log . watch_variable ( index)
290315 }
291316
292317 pub fn unwatch_variable ( & mut self , index : I ) {
293- self . unify_log . unwatch_variable ( index)
318+ self . storage . unify_log . unwatch_variable ( index)
294319 }
295320
296321 pub fn drain_modified_set ( & mut self , offset : & ms:: Offset < I > , mut f : impl FnMut ( I ) -> bool ) {
297- let unify_log = & self . unify_log ;
298- self . modified_set . drain ( & mut self . undo_log , offset, |vid| {
322+ let unify_log = & self . storage . unify_log ;
323+ self . storage . modified_set . drain ( & mut self . undo_log , offset, |vid| {
299324 for & unified_vid in unify_log. get ( vid) {
300325 f ( unified_vid) ;
301326 }
0 commit comments