-
Notifications
You must be signed in to change notification settings - Fork 22
/
random_access_skip_list.h
1348 lines (1142 loc) · 41.6 KB
/
random_access_skip_list.h
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//==============================================================================
// random_access_skip_list.h
// Copyright (c) 2011 Pete Goodliffe. All rights reserved.
//==============================================================================
#pragma once
#include "skip_list_detail.h"
#include <memory> // for std::allocator
#include <functional> // for std::less
#include <iterator> // for std::reverse_iterator
#include <utility> // for std::pair
//==============================================================================
#ifdef _MSC_VER
#pragma warning( push )
#pragma warning (disable : 4068 ) /* disable unknown pragma warnings */
#endif
//==============================================================================
#pragma mark - internal forward declarations
namespace goodliffe {
namespace detail
{
template <typename T,typename C,typename A,typename LG>
class rasl_impl;
template <typename LIST> class rasl_iterator;
template <typename LIST> class rasl_const_iterator;
}
}
//==============================================================================
#pragma mark - random_access_skip_list
//==============================================================================
namespace goodliffe {
/// A random_access_skip_list is a skip_list variant that provides
/// O(log N) random access and random access iterators.
///
/// That is, it provides relatively fast operator[] and the ability to
/// write:
/// iterator i = list.begin()
/// i += 5;
///
/// This speed comes at the expense of a little extra storage within the
/// data structure.
///
/// @see skip_list
template <typename T,
typename Compare = std::less<T>,
typename Allocator = std::allocator<T>,
typename LevelGenerator = detail::skip_list_level_generator<32> >
class random_access_skip_list
{
private:
typedef typename detail::rasl_impl<T,Compare,Allocator,LevelGenerator> impl_type;
typedef typename impl_type::node_type node_type;
template <typename T1> friend class detail::rasl_iterator;
template <typename T1> friend class detail::rasl_const_iterator;
public:
//======================================================================
// types
typedef T value_type;
typedef Allocator allocator_type;
typedef typename impl_type::size_type size_type;
typedef typename allocator_type::difference_type difference_type;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::const_pointer const_pointer;
typedef Compare compare;
typedef typename detail::rasl_iterator<impl_type> iterator;
typedef typename iterator::const_iterator const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
//======================================================================
// lifetime management
explicit random_access_skip_list(const Allocator &alloc = Allocator());
template <class InputIterator>
random_access_skip_list(InputIterator first, InputIterator last, const Allocator &alloc = Allocator());
random_access_skip_list(const random_access_skip_list &other);
random_access_skip_list(const random_access_skip_list &other, const Allocator &alloc);
// C++11
//random_access_skip_list(const random_access_skip_list &&other);
//random_access_skip_list(const random_access_skip_list &&other, const Allocator &alloc);
//random_access_skip_list(std::initializer_list<T> init, const Allocator &alloc = Allocator());
allocator_type get_allocator() const { return impl.get_allocator(); }
//======================================================================
// assignment
random_access_skip_list &operator=(const random_access_skip_list &other);
//C++11 random_access_skip_list& operator=(random_access_skip_list&& other);
template <typename InputIterator>
void assign(InputIterator first, InputIterator last);
//======================================================================
// element access
reference front();
const_reference front() const;
reference back();
const_reference back() const;
//======================================================================
// iterators
iterator begin() { return iterator(&impl, impl.front()); }
const_iterator begin() const { return const_iterator(&impl, impl.front()); }
const_iterator cbegin() const { return const_iterator(&impl, impl.front()); }
iterator end() { return iterator(&impl, impl.one_past_end()); }
const_iterator end() const { return const_iterator(&impl, impl.one_past_end()); }
const_iterator cend() const { return const_iterator(&impl, impl.one_past_end()); }
reverse_iterator rbegin() { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }
//======================================================================
// capacity
bool empty() const { return impl.size() == 0; }
size_type size() const { return impl.size(); }
size_type max_size() const { return impl.get_allocator().max_size(); }
//======================================================================
// modifiers
void clear();
typedef typename std::pair<iterator,bool> insert_by_value_result;
insert_by_value_result insert(const value_type &value);
iterator insert(const_iterator hint, const value_type &value);
//C++11iterator insert(value_type &&value);
//C++11iterator insert(const_iterator hint, const value_type &&value);
template <class InputIterator>
void insert(InputIterator first, InputIterator last);
//C++11iterator insert(std::initializer_list<value_type> ilist);
//C++11emplace
size_type erase(const value_type &value);
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);
void swap(random_access_skip_list &other) { impl.swap(other.impl); }
friend void swap(random_access_skip_list &lhs, random_access_skip_list &rhs) { lhs.swap(rhs); }
//======================================================================
// lookup
bool contains(const value_type &value) const { return count(value) != 0; }
size_type count(const value_type &value) const;
iterator find(const value_type &value);
const_iterator find(const value_type &value) const;
//======================================================================
// random access
const_reference operator[](unsigned index) const;
iterator iterator_at(unsigned index);
const_iterator iterator_at(unsigned index) const;
const_iterator citerator_at(unsigned index) const { return iterator_at(index); }
size_type index_of(const const_iterator &i) const;
void erase_at(size_type index);
//======================================================================
// other operations
template <typename STREAM>
void dump(STREAM &stream) const { impl.dump(stream); }
protected:
impl_type impl;
};
} // namespace goodliffe
//==============================================================================
#pragma mark - non-members
namespace goodliffe {
template <class T, class C, class A, class LG>
inline
bool operator==(const random_access_skip_list<T,C,A,LG> &lhs, const random_access_skip_list<T,C,A,LG> &rhs)
{
return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
}
template <class T, class C, class A, class LG>
inline
bool operator!=(const random_access_skip_list<T,C,A,LG> &lhs, const random_access_skip_list<T,C,A,LG> &rhs)
{
return !operator==(lhs, rhs);
}
template <class T, class C, class A, class LG>
inline
bool operator<(const random_access_skip_list<T,C,A,LG> &lhs, const random_access_skip_list<T,C,A,LG> &rhs)
{
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
template <class T, class C, class A, class LG>
inline
bool operator<=(const random_access_skip_list<T,C,A,LG> &lhs, const random_access_skip_list<T,C,A,LG> &rhs)
{
return !(rhs < lhs);
}
template <class T, class C, class A, class LG>
inline
bool operator>(const random_access_skip_list<T,C,A,LG> &lhs, const random_access_skip_list<T,C,A,LG> &rhs)
{
return rhs < lhs;
}
template <class T, class C, class A, class LG>
inline
bool operator>=(const random_access_skip_list<T,C,A,LG> &lhs, const random_access_skip_list<T,C,A,LG> &rhs)
{
return !(lhs < rhs);
}
} // namespace goodliffe
namespace std
{
template <class T, class C, class A, class LG>
void swap(goodliffe::random_access_skip_list<T,C,A,LG> &lhs, goodliffe::random_access_skip_list<T,C,A,LG> &rhs)
{
lhs.swap(rhs);
}
}
//==============================================================================
#pragma mark - iterators
//==============================================================================
namespace goodliffe {
namespace detail {
template <typename RASL_IMPL>
class rasl_iterator
: public std::iterator<std::random_access_iterator_tag,
typename RASL_IMPL::value_type,
typename RASL_IMPL::difference_type,
typename RASL_IMPL::const_pointer,
typename RASL_IMPL::const_reference>
{
public:
typedef RASL_IMPL impl_type;
typedef rasl_const_iterator<impl_type> const_iterator;
typedef typename impl_type::node_type node_type;
typedef rasl_iterator<impl_type> self_type;
typedef typename impl_type::difference_type difference_type;
typedef typename impl_type::const_reference const_reference;
typedef typename impl_type::const_pointer const_pointer;
rasl_iterator()
: impl(0), node(0) {}
rasl_iterator(impl_type *impl_, node_type *node_)
: impl(impl_), node(node_) {}
rasl_iterator(const rasl_iterator &other)
: impl(other.impl), node(other.node) {}
self_type &operator++()
{ node = node->next[0]; return *this; }
self_type operator++(int) // postincrement
{ self_type old(*this); node = node->next[0]; return old; }
self_type &operator--()
{ node = node->prev; return *this; }
self_type operator--(int) // postdecrement
{ self_type old(*this); node = node->prev; return old; }
self_type &operator+=(difference_type n)
{ node = impl->at(impl->index_of(node)+n); return *this; }
self_type &operator-=(difference_type n)
{ node = impl->at(impl->index_of(node)-n); return *this; }
rasl_iterator operator+(difference_type rhs) const
{ return rasl_iterator(*this) += rhs; }
rasl_iterator operator-(difference_type rhs) const
{ return rasl_iterator(*this) -= rhs; }
const_reference operator[](int index) const
{ return *operator+(index); }
bool operator<(const self_type &rhs) const
{ return impl->index_of(node) < impl->index_of(rhs.node); }
difference_type operator-(const self_type &rhs) const
{ return impl->index_of(node) - impl->index_of(rhs.node); }
const_reference operator*() { return node->value; }
const_pointer operator->() { return node->value; }
bool operator==(const self_type &rhs) const
{ return impl == rhs.impl && node == rhs.node; }
bool operator!=(const self_type &rhs) const
{ return !operator==(rhs); }
bool operator==(const const_iterator &rhs) const
{ return impl == rhs.impl && node == rhs.node; }
bool operator!=(const const_iterator &rhs) const
{ return !operator==(rhs); }
const impl_type *get_impl() const { return impl; } ///< @internal
const node_type *get_node() const { return node; } ///< @internal
private:
impl_type *impl;
node_type *node;
};
template <typename I>
inline
rasl_iterator<I> operator+(int lhs, rasl_iterator<I> rhs)
{ return rhs+lhs; }
template <typename RASL_IMPL>
class rasl_const_iterator
: public std::iterator<std::random_access_iterator_tag,
typename RASL_IMPL::value_type,
typename RASL_IMPL::difference_type,
typename RASL_IMPL::const_pointer,
typename RASL_IMPL::const_reference>
{
public:
typedef const RASL_IMPL impl_type;
typedef rasl_iterator<RASL_IMPL> normal_iterator;
typedef const typename impl_type::node_type node_type;
typedef rasl_const_iterator<RASL_IMPL> self_type;
typedef typename impl_type::difference_type difference_type;
typedef typename impl_type::const_reference const_reference;
typedef typename impl_type::const_pointer const_pointer;
rasl_const_iterator()
: impl(0), node(0) {}
rasl_const_iterator(const normal_iterator &i)
: impl(i.get_impl()), node(i.get_node()) {}
rasl_const_iterator(const impl_type *impl_, node_type *node_)
: impl(impl_), node(node_) {}
rasl_const_iterator(const rasl_const_iterator &other)
: impl(other.impl), node(other.node) {}
self_type &operator++()
{ node = node->next[0]; return *this; }
self_type operator++(int) // postincrement
{ self_type old(*this); node = node->next[0]; return old; }
self_type &operator--()
{ node = node->prev; return *this; }
self_type operator--(int) // postdecrement
{ self_type old(*this); node = node->prev; return old; }
self_type &operator+=(difference_type n)
{ node = impl->at(impl->index_of(node)+n); return *this; }
self_type &operator-=(difference_type n)
{ node = impl->at(impl->index_of(node)-n); return *this; }
rasl_const_iterator operator+(difference_type rhs) const
{ return rasl_const_iterator(*this) += rhs; }
rasl_const_iterator operator-(difference_type rhs) const
{ return rasl_const_iterator(*this) -= rhs; }
const_reference operator[](int index) const
{ return *operator+(index); }
bool operator<(const self_type &rhs) const
{ return impl->index_of(node) < impl->index_of(rhs.node); }
difference_type operator-(const self_type &rhs) const
{ return impl->index_of(node) - impl->index_of(rhs.node); }
const_reference operator*() { return node->value; }
const_pointer operator->() { return node->value; }
bool operator==(const self_type &other) const
{ return impl == other.impl && node == other.node; }
bool operator!=(const self_type &other) const
{ return !operator==(other); }
bool operator==(const normal_iterator &other) const
{ return impl == other.impl && node == other.node; }
bool operator!=(const normal_iterator &other) const
{ return !operator==(other); }
const impl_type *get_impl() const { return impl; } ///< @internal
const node_type *get_node() const { return node; } ///< @internal
private:
impl_type *impl;
node_type *node;
};
template <typename I>
inline
rasl_const_iterator<I> operator+(int lhs, rasl_const_iterator<I> rhs)
{ return rhs+lhs; }
} // namespace detail
} // namespace goodliffe
//==============================================================================
#pragma mark - lifetime management
//==============================================================================
namespace goodliffe {
template <class T, class C, class A, class LG>
inline
random_access_skip_list<T,C,A,LG>::random_access_skip_list(const allocator_type &alloc_)
: impl(alloc_)
{
}
template <class T, class C, class A, class LG>
template <class InputIterator>
inline
random_access_skip_list<T,C,A,LG>::random_access_skip_list(InputIterator first, InputIterator last, const allocator_type &alloc_)
: impl(alloc_)
{
assign(first, last);
}
template <class T, class C, class A, class LG>
inline
random_access_skip_list<T,C,A,LG>::random_access_skip_list(const random_access_skip_list &other)
: impl(other.get_allocator())
{
assign(other.begin(), other.end());
}
template <class T, class C, class A, class LG>
inline
random_access_skip_list<T,C,A,LG>::random_access_skip_list(const random_access_skip_list &other, const allocator_type &alloc_)
: impl(alloc_)
{
assign(other.begin(), other.end());
}
// C++11
//skip_list(const skip_list &&other);
//skip_list(const skip_list &&other, const Allocator &alloc);
//skip_list(std::initializer_list<T> init, const Allocator &alloc = Allocator());
//==============================================================================
#pragma mark assignment
template <class T, class C, class A, class LG>
inline
random_access_skip_list<T,C,A,LG> &
random_access_skip_list<T,C,A,LG>::operator=(const random_access_skip_list<T,C,A,LG> &other)
{
assign(other.begin(), other.end());
return *this;
}
//C++11 skip_list& operator=(skip_list&& other);
template <class T, class C, class A, class LG>
template <typename InputIterator>
inline
void random_access_skip_list<T,C,A,LG>::assign(InputIterator first, InputIterator last)
{
clear();
while (first != last) insert(*first++);
}
//==============================================================================
#pragma mark element access
template <class T, class C, class A, class LG>
inline
typename random_access_skip_list<T,C,A,LG>::reference
random_access_skip_list<T,C,A,LG>::front()
{
assert_that(!empty());
return impl.front()->value;
}
template <class T, class C, class A, class LG>
inline
typename random_access_skip_list<T,C,A,LG>::const_reference
random_access_skip_list<T,C,A,LG>::front() const
{
assert_that(!empty());
return impl.front()->value;
}
template <class T, class C, class A, class LG>
inline
typename random_access_skip_list<T,C,A,LG>::reference
random_access_skip_list<T,C,A,LG>::back()
{
assert_that(!empty());
return impl.one_past_end()->prev->value;
}
template <class T, class C, class A, class LG>
inline
typename random_access_skip_list<T,C,A,LG>::const_reference
random_access_skip_list<T,C,A,LG>::back() const
{
assert_that(!empty());
return impl.one_past_end()->prev->value;
}
//==============================================================================
#pragma mark modifiers
template <class T, class C, class A, class LG>
inline
void random_access_skip_list<T,C,A,LG>::clear()
{
impl.remove_all();
}
template <class T, class C, class A, class LG>
inline
typename random_access_skip_list<T,C,A,LG>::insert_by_value_result
random_access_skip_list<T,C,A,LG>::insert(const value_type &value)
{
node_type *node = impl.insert(value);
return std::make_pair(iterator(&impl, node), impl.is_valid(node));
}
template <class T, class C, class A, class LG>
inline
typename random_access_skip_list<T,C,A,LG>::iterator
random_access_skip_list<T,C,A,LG>::insert(const_iterator hint, const value_type &value)
{
assert_that(hint.get_impl() == &impl);
const node_type *hint_node = hint.get_node();
if (impl.is_valid(hint_node) && detail::less_or_equal(value, hint_node->value, impl.less))
return iterator(&impl,impl.insert(value)); // bad hint, resort to "normal" insert
else
return iterator(&impl,impl.insert(value,const_cast<node_type*>(hint_node)));
}
//C++11iterator insert const_iterator pos, value_type &&value);
template <class T, class C, class A, class LG>
template <class InputIterator>
inline
void
random_access_skip_list<T,C,A,LG>::insert(InputIterator first, InputIterator last)
{
iterator last_inserted = end();
while (first != last)
{
last_inserted = insert(last_inserted, *first++);
}
}
//C++11iterator insert(std::initializer_list<value_type> ilist);
// C++11 emplace
template <class T, class C, class A, class LG>
inline
typename random_access_skip_list<T,C,A,LG>::size_type
random_access_skip_list<T,C,A,LG>::erase(const value_type &value)
{
node_type *node = impl.find(value);
if (impl.is_valid(node) && detail::equivalent(node->value, value, impl.less))
{
impl.remove(node);
return 1;
}
else
{
return 0;
}
}
template <class T, class C, class A, class LG>
inline
typename random_access_skip_list<T,C,A,LG>::iterator
random_access_skip_list<T,C,A,LG>::erase(const_iterator position)
{
assert_that(position.get_impl() == &impl);
assert_that(impl.is_valid(position.get_node()));
node_type *node = const_cast<node_type*>(position.get_node());
node_type *next = node->next[0];
impl.remove(node);
return iterator(&impl, next);
}
template <class T, class C, class A, class LG>
inline
typename random_access_skip_list<T,C,A,LG>::iterator
random_access_skip_list<T,C,A,LG>::erase(const_iterator first, const_iterator last)
{
assert_that(first.get_impl() == &impl);
assert_that(last.get_impl() == &impl);
if (first != last)
{
node_type *first_node = const_cast<node_type*>(first.get_node());
node_type *last_node = const_cast<node_type*>(last.get_node()->prev);
impl.remove_between(first_node, last_node);
}
return iterator(&impl, const_cast<node_type*>(last.get_node()));
}
//==============================================================================
#pragma mark lookup
template <class T, class C, class A, class LG>
inline
typename random_access_skip_list<T,C,A,LG>::size_type
random_access_skip_list<T,C,A,LG>::count(const value_type &value) const
{
const node_type *node = impl.find(value);
return impl.is_valid(node) && detail::equivalent(node->value, value, impl.less);
}
template <class T, class C, class A, class LG>
inline
typename random_access_skip_list<T,C,A,LG>::iterator
random_access_skip_list<T,C,A,LG>::find(const value_type &value)
{
node_type *node = impl.find(value);
return impl.is_valid(node) && detail::equivalent(node->value, value, impl.less)
? iterator(&impl, node)
: end();
}
template <class T, class C, class A, class LG>
inline
typename random_access_skip_list<T,C,A,LG>::const_iterator
random_access_skip_list<T,C,A,LG>::find(const value_type &value) const
{
const node_type *node = impl.find(value);
return impl.is_valid(node) && detail::equivalent(node->value, value, impl.less)
? const_iterator(&impl, node)
: end();
}
//==============================================================================
#pragma mark random access
template <class T, class C, class A, class LG>
inline
typename random_access_skip_list<T,C,A,LG>::const_reference
random_access_skip_list<T,C,A,LG>::operator[](unsigned index) const
{
const node_type *node = impl.at(index);
assert_that(impl.is_valid(node));
return node->value;
}
template <class T, class C, class A, class LG>
inline
void
random_access_skip_list<T,C,A,LG>::erase_at(size_type index)
{
node_type *node = impl.at(index);
assert_that(impl.is_valid(node));
impl.remove(node);
}
template <class T, class C, class A, class LG>
inline
typename random_access_skip_list<T,C,A,LG>::iterator
random_access_skip_list<T,C,A,LG>::iterator_at(unsigned index)
{
node_type *node = impl.at(index);
return iterator(&impl, node);
}
template <class T, class C, class A, class LG>
inline
typename random_access_skip_list<T,C,A,LG>::const_iterator
random_access_skip_list<T,C,A,LG>::iterator_at(unsigned index) const
{
const node_type *node = impl.at(index);
return const_iterator(&impl, node);
}
template <class T, class C, class A, class LG>
inline
typename random_access_skip_list<T,C,A,LG>::size_type
random_access_skip_list<T,C,A,LG>::index_of(const const_iterator &i) const
{
return impl.index_of(i.get_node());
}
} // namespace goodliffe
//==============================================================================
#pragma mark - rasl_impl
//==============================================================================
namespace goodliffe {
namespace detail {
template <typename T, typename SPAN>
struct rasl_node
{
typedef SPAN size_type;
#ifdef SKIP_LIST_IMPL_DIAGNOSTICS
unsigned magic;
#endif
T value;
unsigned level;
rasl_node *prev;
rasl_node **next; ///< effectively node_type *next[level+1];
size_type *span; ///< effectively unsigned span[level+1];
};
/// Internal implementation of skip_list data structure and methods for
/// modifying it.
///
/// Not for "public" access.
///
/// @internal
template <typename T, typename Compare, typename Allocator, typename LevelGenerator>
class rasl_impl
{
public:
typedef T value_type;
typedef typename Allocator::size_type size_type;
typedef typename Allocator::difference_type difference_type;
typedef typename Allocator::const_reference const_reference;
typedef typename Allocator::const_pointer const_pointer;
typedef Allocator allocator_type;
typedef Compare compare_type;
typedef LevelGenerator generator_type;
typedef rasl_node<T, size_type> node_type;
static const unsigned num_levels = LevelGenerator::num_levels;
rasl_impl(const Allocator &alloc = Allocator());
~rasl_impl();
Allocator get_allocator() const { return alloc; }
size_type size() const { return item_count; }
bool is_valid(const node_type *node) const { return node && node != head && node != tail; }
node_type *front() { return head->next[0]; }
const node_type *front() const { return head->next[0]; }
node_type *one_past_end() { return tail; }
const node_type *one_past_end() const { return tail; }
node_type *find(const value_type &value) const;
node_type *at(size_type index);
const node_type *at(size_type index) const;
node_type *insert(const value_type &value, node_type *hint = 0);
void remove(node_type *value);
void remove_all();
void remove_between(node_type *first, node_type *last);
void swap(rasl_impl &other);
size_type index_of(const node_type *node) const;
template <typename STREAM>
void dump(STREAM &stream) const;
bool check() const;
unsigned new_level();
compare_type less;
private:
typedef typename Allocator::template rebind<node_type>::other node_allocator;
typedef typename Allocator::template rebind<node_type*>::other list_allocator;
typedef typename Allocator::template rebind<size_type>::other span_allocator;
rasl_impl(const rasl_impl &other);
rasl_impl &operator=(const rasl_impl &other);
size_type find_chain(const value_type &value, node_type **chain) const;
size_type find_chain(const value_type &value, node_type **chain, size_type *indexes) const;
size_type find_chain(const node_type *node, node_type **chain, size_type *indexes) const;
size_type find_end_chain(node_type **chain, size_type *indexes) const;
allocator_type alloc;
generator_type generator;
unsigned levels;
node_type *head;
node_type *tail;
size_type item_count;
node_type *allocate(unsigned level)
{
node_type *node = node_allocator(alloc).allocate(1, (void*)0);
node->next = list_allocator(alloc).allocate(level+1, (void*)0);
node->span = span_allocator(alloc).allocate(level+1, (void*)0);
node->level = level;
for (unsigned n = 0; n <= level; ++n) node->span[n] = 1;
#ifdef SKIP_LIST_IMPL_DIAGNOSTICS
for (unsigned n = 0; n <= level; ++n) node->next[n] = 0;
node->magic = MAGIC_GOOD;
#endif
return node;
}
void deallocate(node_type *node)
{
#ifdef SKIP_LIST_IMPL_DIAGNOSTICS
assert_that(node->magic == MAGIC_GOOD);
node->magic = MAGIC_BAD;
for (unsigned n = 0; n <= node->level; ++n) node->next[n] = 0;
node->prev = 0;
#endif
span_allocator(alloc).deallocate(node->span, node->level+1);
list_allocator(alloc).deallocate(node->next, node->level+1);
node_allocator(alloc).deallocate(node, 1);
}
};
template <class T, class C, class A, class LG>
inline
rasl_impl<T,C,A,LG>::rasl_impl(const allocator_type &alloc_)
: alloc(alloc_),
levels(0),
head(allocate(num_levels)),
tail(allocate(num_levels)),
item_count(0)
{
for (unsigned n = 0; n < num_levels; n++)
{
head->next[n] = tail;
tail->next[n] = 0;
head->span[n] = 1;
}
head->prev = 0;
tail->prev = head;
}
template <class T, class C, class A, class LG>
inline
rasl_impl<T,C,A,LG>::~rasl_impl()
{
remove_all();
deallocate(head);
deallocate(tail);
}
template <class T, class C, class A, class LG>
inline
typename rasl_impl<T,C,A,LG>::node_type *
rasl_impl<T,C,A,LG>::find(const value_type &value) const
{
// I could have a const and non-const overload, but this cast is simpler
node_type *search = const_cast<node_type*>(head);
for (unsigned l = levels; l; )
{
--l;
while (search->next[l] != tail && detail::less_or_equal(search->next[l]->value, value, less))
{
search = search->next[l];
}
}
return search;
}
template <class T, class C, class A, class LG>
inline
typename rasl_impl<T,C,A,LG>::size_type
rasl_impl<T,C,A,LG>::find_chain(const value_type &value, node_type **chain, size_type *indexes) const
{
size_type index = 0;
node_type *cur = head;
unsigned l = num_levels;
while (l)
{
--l;
impl_assert_that(l <= cur->level);
while (cur->next[l] != tail && less(cur->next[l]->value, value))
{
index += cur->span[l];
cur = cur->next[l];
impl_assert_that(l <= cur->level);
}
chain[l] = cur;
indexes[l] = index;
}
#ifdef SKIP_LIST_IMPL_DIAGNOSTICS
for (unsigned l1 = 0; l1 < num_levels; ++l1)
{
assert_that(chain[l1]->level >= l1);
}
#endif
return index;
}
// TODO: fold these down
template <class T, class C, class A, class LG>
inline
typename rasl_impl<T,C,A,LG>::size_type
rasl_impl<T,C,A,LG>::find_chain(const node_type *node, node_type **chain, size_type *indexes) const
{
assert_that(node && node != head);
if (node == tail) return find_end_chain(chain, indexes);
assert_that(is_valid(node));
size_type index = 0;
node_type *cur = head;
unsigned l = num_levels;
while (l)
{
--l;
impl_assert_that(l <= cur->level);
while (cur->next[l] != tail && less(cur->next[l]->value, node->value))
{
index += cur->span[l];
cur = cur->next[l];
impl_assert_that(l <= cur->level);
}
chain[l] = cur;
indexes[l] = index;
}
#ifdef SKIP_LIST_IMPL_DIAGNOSTICS
for (unsigned l1 = 0; l1 < num_levels; ++l1)
{
assert_that(chain[l1]->level >= l1);
}
#endif
return index;
}
// TODO: fold these down, up, sideways
template <class T, class C, class A, class LG>
inline
typename rasl_impl<T,C,A,LG>::size_type
rasl_impl<T,C,A,LG>::find_end_chain(node_type **chain, size_type *indexes) const
{
size_type index = 0;
node_type *cur = head;
unsigned l = num_levels;
while (l)
{
--l;
impl_assert_that(l <= cur->level);
while (cur->next[l] != tail)
{
index += cur->span[l];
cur = cur->next[l];
impl_assert_that(l <= cur->level);
}
chain[l] = cur;
indexes[l] = index;
}
#ifdef SKIP_LIST_IMPL_DIAGNOSTICS
for (unsigned l1 = 0; l1 < num_levels; ++l1)
{
impl_assert_that(chain[l1]->level >= l1);
}
#endif
return index;
}
// TODO: Hint is now ignored (has to be, for spans to work)
template <class T, class C, class A, class LG>
inline
typename rasl_impl<T,C,A,LG>::node_type*
rasl_impl<T,C,A,LG>::insert(const value_type &value, node_type *hint)
{
const unsigned level = new_level();
node_type *chain[num_levels] = {0};
size_type indexes[num_levels] = {0};
size_type index = find_chain(value, chain, indexes);
// Do not allow repeated values in the list (we could in a "multi_skip_list")
{
node_type *next = chain[0]->next[0];