1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#![warn(missing_docs)]
use std::rc::Rc;
use std::cell::RefCell;
use std::vec::Vec;
use std::slice;
#[derive(Debug)]
enum EntryOrigin {
Index(usize),
Detached,
}
impl From<usize> for EntryOrigin {
fn from(v: usize) -> Self {
EntryOrigin::Index(v)
}
}
#[derive(Debug)]
pub struct Entry<T> {
val: T,
index: EntryOrigin,
}
impl<T> Entry<T> {
pub fn new(val: T, index: usize) -> Entry<T> {
Entry {
val: val,
index: EntryOrigin::Index(index),
}
}
pub fn new_detached(val: T) -> Entry<T> {
Entry {
val: val,
index: EntryOrigin::Detached,
}
}
pub fn order(&self) -> Option<usize> {
match self.index {
EntryOrigin::Detached => None,
EntryOrigin::Index(idx) => Some(idx),
}
}
}
impl<T> ::std::ops::Deref for Entry<T> {
type Target = T;
fn deref(&self) -> &T {
&self.val
}
}
impl<T> ::std::ops::DerefMut for Entry<T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.val
}
}
#[derive(Debug)]
pub struct EntryRef<T>(Rc<RefCell<Entry<T>>>);
impl<T> Clone for EntryRef<T> {
fn clone(&self) -> Self {
EntryRef(self.0.clone())
}
}
impl<T> From<Entry<T>> for EntryRef<T> {
fn from(v: Entry<T>) -> Self {
EntryRef(Rc::new(RefCell::new(v)))
}
}
impl<T> EntryRef<T> {
pub fn read(&self) -> ::std::cell::Ref<Entry<T>> {
self.0.borrow()
}
pub fn write(&self) -> ::std::cell::RefMut<Entry<T>> {
self.0.borrow_mut()
}
pub fn order(&self) -> Option<usize> {
self.0.borrow().order()
}
pub fn link_count(&self) -> usize {
Rc::strong_count(&self.0) - 1
}
}
#[derive(Debug)]
pub struct RefList<T> {
items: Vec<EntryRef<T>>,
}
impl<T> Default for RefList<T> {
fn default() -> Self {
RefList { items: Default::default() }
}
}
impl<T> RefList<T> {
pub fn new() -> Self { Self::default() }
pub fn push(&mut self, t: T) -> EntryRef<T> {
let idx = self.items.len();
let val: EntryRef<_> = Entry::new(t, idx).into();
self.items.push(val.clone());
val
}
pub fn begin_delete(&mut self) -> DeleteTransaction<T> {
DeleteTransaction {
list: self,
deleted: Vec::new(),
}
}
pub fn begin_insert(&mut self, at: usize) -> InsertTransaction<T> {
InsertTransaction {
at: at,
list: self,
items: Vec::new(),
}
}
pub fn begin_insert_after<F>(&mut self, mut f: F) -> InsertTransaction<T>
where F : FnMut(&T) -> bool
{
let pos = self
.items.iter()
.position(|rf| f(&**rf.read())).map(|x| x + 1)
.unwrap_or(self.items.len());
self.begin_insert(pos)
}
pub fn begin_insert_not_until<F>(&mut self, mut f: F) -> InsertTransaction<T>
where F : FnMut(&T) -> bool
{
let pos = self.items.iter().take_while(|rf| f(&**rf.read())).count();
self.begin_insert(pos)
}
pub fn get(&self, idx: usize) -> Option<EntryRef<T>> {
self.items.get(idx).cloned()
}
fn done_delete(&mut self, indices: &[usize]) {
for entry in self.items.iter_mut() {
let mut entry = entry.write();
let total_less = indices.iter()
.take_while(|x| **x < entry.order().expect("Items in the list always have order; qed"))
.count();
match entry.index {
EntryOrigin::Detached => unreachable!("Items in the list always have order!"),
EntryOrigin::Index(ref mut idx) => { *idx -= total_less; },
};
}
let mut total_removed = 0;
for idx in indices {
let detached = self.items.remove(*idx - total_removed);
detached.write().index = EntryOrigin::Detached;
total_removed += 1;
}
}
fn done_insert(&mut self, index: usize, mut items: Vec<EntryRef<T>>) {
let mut offset = 0;
for item in items.drain(..) {
item.write().index = EntryOrigin::Index(index + offset);
self.items.insert(index + offset, item);
offset += 1;
}
for idx in (index+offset)..self.items.len() {
self.get_ref(idx).write().index = EntryOrigin::Index(idx);
}
}
pub fn delete(&mut self, indices: &[usize]) {
self.done_delete(indices)
}
pub fn delete_one(&mut self, index: usize) {
self.done_delete(&[index])
}
pub fn from_slice(list: &[T]) -> Self
where T: Clone
{
let mut res = Self::new();
for t in list {
res.push(t.clone());
}
res
}
pub fn len(&self) -> usize {
self.items.len()
}
pub fn clone_ref(&self, idx: usize) -> EntryRef<T> {
self.items[idx].clone()
}
pub fn get_ref(&self, idx: usize) -> &EntryRef<T> {
&self.items[idx]
}
pub fn iter(&self) -> slice::Iter<EntryRef<T>> {
self.items.iter()
}
}
#[must_use]
pub struct DeleteTransaction<'a, T> {
list: &'a mut RefList<T>,
deleted: Vec<usize>,
}
impl<'a, T> DeleteTransaction<'a, T> {
pub fn push(self, idx: usize) -> Self {
let mut tx = self;
tx.deleted.push(idx);
tx
}
pub fn done(self) {
let indices = self.deleted;
let list = self.list;
list.done_delete(&indices[..]);
}
}
#[must_use]
pub struct InsertTransaction<'a, T> {
at: usize,
list: &'a mut RefList<T>,
items: Vec<EntryRef<T>>,
}
impl<'a, T> InsertTransaction<'a, T> {
pub fn push(&mut self, val: T) -> EntryRef<T> {
let val: EntryRef<_> = Entry::new_detached(val).into();
self.items.push(val.clone());
val
}
pub fn done(self) {
let items = self.items;
let list = self.list;
let at = self.at;
list.done_insert(at, items);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn order() {
let mut list = RefList::<u32>::new();
let item00 = list.push(0);
let item10 = list.push(10);
let item20 = list.push(20);
let item30 = list.push(30);
assert_eq!(item00.order(), Some(0));
assert_eq!(item10.order(), Some(1));
assert_eq!(item20.order(), Some(2));
assert_eq!(item30.order(), Some(3));
assert_eq!(**item00.read(), 0);
assert_eq!(**item10.read(), 10);
assert_eq!(**item20.read(), 20);
assert_eq!(**item30.read(), 30);
}
#[test]
fn delete() {
let mut list = RefList::<u32>::new();
let item00 = list.push(0);
let item10 = list.push(10);
let item20 = list.push(20);
let item30 = list.push(30);
list.begin_delete().push(2).done();
assert_eq!(item00.order(), Some(0));
assert_eq!(item10.order(), Some(1));
assert_eq!(item30.order(), Some(2));
assert_eq!(item20.order(), None);
}
#[test]
fn complex_delete() {
let mut list = RefList::<u32>::new();
let item00 = list.push(0);
let item10 = list.push(10);
let item20 = list.push(20);
let item30 = list.push(30);
let item40 = list.push(40);
let item50 = list.push(50);
let item60 = list.push(60);
let item70 = list.push(70);
let item80 = list.push(80);
let item90 = list.push(90);
list.begin_delete().push(1).push(2).push(4).push(6).done();
assert_eq!(item00.order(), Some(0));
assert_eq!(item10.order(), None);
assert_eq!(item20.order(), None);
assert_eq!(item30.order(), Some(1));
assert_eq!(item40.order(), None);
assert_eq!(item50.order(), Some(2));
assert_eq!(item60.order(), None);
assert_eq!(item70.order(), Some(3));
assert_eq!(item80.order(), Some(4));
assert_eq!(item90.order(), Some(5));
}
#[test]
fn insert() {
let mut list = RefList::<u32>::new();
let item00 = list.push(0);
let item10 = list.push(10);
let item20 = list.push(20);
let item30 = list.push(30);
let mut insert_tx = list.begin_insert(3);
let item23 = insert_tx.push(23);
let item27 = insert_tx.push(27);
insert_tx.done();
assert_eq!(item00.order(), Some(0));
assert_eq!(item10.order(), Some(1));
assert_eq!(item20.order(), Some(2));
assert_eq!(item23.order(), Some(3));
assert_eq!(item27.order(), Some(4));
assert_eq!(item30.order(), Some(5));
}
#[test]
fn insert_end() {
let mut list = RefList::<u32>::new();
let mut insert_tx = list.begin_insert(0);
let item0 = insert_tx.push(0);
insert_tx.done();
assert_eq!(item0.order(), Some(0));
}
#[test]
fn insert_end_more() {
let mut list = RefList::<u32>::new();
let item0 = list.push(0);
let mut insert_tx = list.begin_insert(1);
let item1 = insert_tx.push(1);
insert_tx.done();
assert_eq!(item0.order(), Some(0));
assert_eq!(item1.order(), Some(1));
}
#[test]
fn insert_after() {
let mut list = RefList::<u32>::new();
let item00 = list.push(0);
let item10 = list.push(10);
let item20 = list.push(20);
let item30 = list.push(30);
let mut insert_tx = list.begin_insert_after(|i| *i == 20);
let item23 = insert_tx.push(23);
let item27 = insert_tx.push(27);
insert_tx.done();
assert_eq!(item00.order(), Some(0));
assert_eq!(item10.order(), Some(1));
assert_eq!(item20.order(), Some(2));
assert_eq!(item23.order(), Some(3));
assert_eq!(item27.order(), Some(4));
assert_eq!(item30.order(), Some(5));
}
#[test]
fn insert_not_until() {
let mut list = RefList::<u32>::new();
let item10 = list.push(10);
let item20 = list.push(20);
let item30 = list.push(30);
let mut insert_tx = list.begin_insert_not_until(|i| *i <= 20);
let item23 = insert_tx.push(23);
let item27 = insert_tx.push(27);
insert_tx.done();
assert_eq!(item10.order(), Some(0));
assert_eq!(item20.order(), Some(1));
assert_eq!(item23.order(), Some(2));
assert_eq!(item27.order(), Some(3));
assert_eq!(item30.order(), Some(4));
}
#[test]
fn insert_after_none() {
let mut list = RefList::<u32>::new();
let item10 = list.push(10);
let item20 = list.push(20);
let item30 = list.push(30);
let mut insert_tx = list.begin_insert_after(|i| *i == 50);
let item55 = insert_tx.push(23);
let item59 = insert_tx.push(27);
insert_tx.done();
assert_eq!(item10.order(), Some(0));
assert_eq!(item20.order(), Some(1));
assert_eq!(item30.order(), Some(2));
assert_eq!(item55.order(), Some(3));
assert_eq!(item59.order(), Some(4));
}
#[test]
fn insert_not_until_none() {
let mut list = RefList::<u32>::new();
let item10 = list.push(10);
let item20 = list.push(20);
let item30 = list.push(30);
let mut insert_tx = list.begin_insert_not_until(|i| *i < 50);
let item55 = insert_tx.push(23);
let item59 = insert_tx.push(27);
insert_tx.done();
assert_eq!(item10.order(), Some(0));
assert_eq!(item20.order(), Some(1));
assert_eq!(item30.order(), Some(2));
assert_eq!(item55.order(), Some(3));
assert_eq!(item59.order(), Some(4));
}
#[test]
fn insert_after_empty() {
let mut list = RefList::<u32>::new();
let mut insert_tx = list.begin_insert_after(|x| *x == 100);
let item0 = insert_tx.push(0);
insert_tx.done();
assert_eq!(item0.order(), Some(0));
}
#[test]
fn insert_more() {
let mut list = RefList::<u32>::new();
let item10 = list.push(10);
let item20 = list.push(20);
let item30 = list.push(30);
let item40 = list.push(10);
let item50 = list.push(20);
let item60 = list.push(30);
let mut insert_tx = list.begin_insert(3);
let item35 = insert_tx.push(23);
let item37 = insert_tx.push(27);
insert_tx.done();
assert_eq!(item10.order(), Some(0));
assert_eq!(item20.order(), Some(1));
assert_eq!(item30.order(), Some(2));
assert_eq!(item35.order(), Some(3));
assert_eq!(item37.order(), Some(4));
assert_eq!(item40.order(), Some(5));
assert_eq!(item50.order(), Some(6));
assert_eq!(item60.order(), Some(7));
}
}