-
Notifications
You must be signed in to change notification settings - Fork 0
/
deque.h
1583 lines (1406 loc) · 49 KB
/
deque.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
/* THOR - THOR Template Library
* Joshua M. Kriegshauser
*
* deque.h
*
* This file defines an STL-compatible deque (double-ended queue) container.
*
* Extensions/Changes:
* - Blocks of 256 items are allocated as necessary.
* - Debug builds check that all iterators are valid (i.e. no trying to erase using an iterator from a different container).
* - push_back() has changed but remains compatible with STL usage:
* * push_back() returns a reference to the added item
* * push_back() with zero parameters will default-construct an element
* * push_back() variations exist with 1-4 parameters that will in-place
* construct an element without necessarily needing a copy constructor.
* * push_back_placement() can be used with placement new to construct
* elements with more than 4 parameters.
* - push_front() has changed but remains compatible with STL usage:
* * push_front() returns a reference to the added item
* * push_front() with zero parameters will default-construct an element
* * push_front() variations exist with 1-4 parameters that will in-place
* construct an element without necessarily needing a copy constructor.
* * push_front_placement() can be used with placement new to construct
* elements with more than 4 parameters.
* - insert() has changed but remains compatible with STL usage:
* * insert(pos) will default-construct an element at pos
* * insert(pos,...) variations with 1-4 additional parameters will
* in-place construct an element at pos without necessarily needing a
* copy constructor.
* * insert_placement(pos) can be used with placement new to construct
* elements with more than 4 parameters.
* - get_contiguous(pos, count):
* * returns a pointer to a contiguous block of memory. count is an output parameter
* that receives the size of the contiguous block.
* - Assistance for raw pointer types:
* * delete_all() will call delete on every element and clear() the container.
* * erase_and_delete() can be used to delete an element and erase it from
* the list.
* * pop_front_delete() will delete the first element and pop it from the container.
* * pop_back_delete() will delete the last element and pop it from the container.
* - O(1) size() function (spec says that size() may be O(n))
*/
#ifndef THOR_DEQUE_H
#define THOR_DEQUE_H
#pragma once
#ifndef THOR_VECTOR_H
#include "vector.h"
#endif
namespace thor
{
template <class T>
class deque
{
struct deque_node;
public:
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef const T* const_pointer;
typedef const T& const_reference;
typedef thor_size_type size_type;
typedef thor_diff_type difference_type;
// extension: the number of elements allocated at once
enum { block_count = 256 };
// iterator definitions
struct iterator_base : public iterator_type<random_access_iterator_tag, T>
{
deque_node* m_node;
T* m_value;
#ifdef THOR_DEBUG
const deque* m_owner;
iterator_base(deque_node* n, T* v, const deque* o) : m_node(n), m_value(v), m_owner(o) {}
void verify_iterator() const { m_owner->verify_iterator(*this); }
#else
iterator_base(deque_node* n, T* v, const deque*) : m_node(n), m_value(v) {}
void verify_iterator() const {}
#endif
void verify_not_end() const { THOR_DEBUG_ASSERT(m_node && m_value); }
void decr(size_type i)
{
THOR_DEBUG_ASSERT(m_node != 0);
THOR_DEBUG_ASSERT(m_value == 0 || m_node->is_valid(m_value));
if (m_node->size() == 0 && i != 0)
{
m_node = m_node->prev;
THOR_DEBUG_ASSERT(m_node->size() != 0); // Will happen if the deque is empty
m_value = m_node->end();
}
while (i != 0)
{
size_type cur = min<size_type>(i, (m_value - m_node->start()) + 1);
m_value -= cur;
i -= cur;
if (m_value < m_node->start())
{
m_node = m_node->prev;
if (m_node->size() != 0)
{
m_value = m_node->end() - 1;
}
else
{
// Hit the end() node; should be done now.
m_value = 0;
THOR_DEBUG_ASSERT(i == 0);
}
}
}
}
void decr()
{
THOR_DEBUG_ASSERT(m_node != 0);
THOR_DEBUG_ASSERT(m_value == 0 || m_node->is_valid(m_value));
if (m_value == 0 || --m_value < m_node->start())
{
m_node = m_node->prev;
if (m_node->size() != 0)
{
m_value = m_node->end() - 1;
}
else
{
m_value = 0;
}
}
}
void incr(size_type i)
{
THOR_DEBUG_ASSERT(m_node != 0);
THOR_DEBUG_ASSERT(m_value == 0 || m_node->is_valid(m_value));
if (m_node->size() == 0)
{
m_node = m_node->next;
THOR_DEBUG_ASSERT(m_node->size() != 0); // Will happen if the deque is empty
m_value = m_node->start();
}
while (i != 0)
{
size_type cur = min<size_type>(i, m_node->end() - m_value);
m_value += cur;
i -= cur;
if (m_value == m_node->end())
{
m_node = m_node->next;
if (m_node->size() != 0)
{
m_value = m_node->start();
}
else
{
// Hit the end() node; should be done now.
m_value = 0;
THOR_DEBUG_ASSERT(i == 0);
}
}
}
}
void incr()
{
THOR_DEBUG_ASSERT(m_node != 0);
THOR_DEBUG_ASSERT(m_value == 0 || m_node->is_valid(m_value));
if (m_value == 0 || ++m_value == m_node->end())
{
m_node = m_node->next;
if (m_node->size() != 0)
{
m_value = m_node->start();
}
else
{
m_value = 0;
}
}
}
void mod(difference_type i)
{
if (i > 0)
{
incr((size_type)i);
}
else
{
decr((size_type)-i);
}
}
private:
static difference_type internal_diff(const iterator_base& high, const iterator_base& low)
{
THOR_DEBUG_ASSERT(high.m_node != low.m_node);
difference_type val = high.m_node->size() == 0 ? 0 : high.m_value - high.m_node->start();
val += (low.m_node->size() == 0 ? 0 : low.m_node->end() - low.m_value);
deque_node* node = high.m_node->prev;
while (node != low.m_node)
{
THOR_DEBUG_ASSERT(node->size() != 0); // Terminator should never be encountered.
if (node->size() == 0)
{
return val;
}
val += node->size();
node = node->prev;
}
return val;
}
public:
difference_type diff(const iterator_base& rhs) const
{
THOR_DEBUG_ASSERT(m_owner == rhs.m_owner);
THOR_DEBUG_ASSERT(m_node != 0 && rhs.m_node != 0);
if (m_node == rhs.m_node)
{
// Best case: both iterators reference values in the same node
return m_value - rhs.m_value;
}
// Optimization if either iterator is the terminator
if (m_node->size() == 0)
{
return internal_diff(*this, rhs);
}
else if (rhs.m_node->size() == 0)
{
return -internal_diff(rhs, *this);
}
// Need to figure out which iterator is higher than the previous one. Walk forward
// until either the given node is encountered or the terminator. This should tell
// us the order.
deque_node* node = m_node->prev;
difference_type diffnodes = 0;
for (;;)
{
if (node->size() == 0)
{
// Encountered the terminator, so the given node must be higher (or unrelated)
return -internal_diff(rhs, *this);
}
if (node == rhs.m_node)
{
// Encountered the rhs node, so now we can calculate the size difference.
difference_type val = diffnodes * block_count;
val += (m_value - m_node->start());
val += (rhs.m_node->end() - rhs.m_value);
return val;
}
++diffnodes;
node = node->prev;
}
}
bool operator == ( const iterator_base& i ) const
{
THOR_DEBUG_ASSERT(m_owner == i.m_owner);
THOR_DEBUG_ASSERT(m_value == 0 || m_node->size() != 0);
THOR_DEBUG_ASSERT(i.m_value == 0 || i.m_node->size() != 0);
return m_value == i.m_value && m_node == i.m_node;
}
bool operator != ( const iterator_base& i ) const { return ! operator == (i); }
};
template<class Traits> class fwd_iterator : public iterator_base
{
public:
typedef typename Traits::pointer pointer;
typedef typename Traits::reference reference;
typedef fwd_iterator<nonconst_traits<T> > nonconst_iterator;
typedef fwd_iterator<Traits> selftype;
fwd_iterator(deque_node* n = 0, T* v = 0, const deque* o = 0) : iterator_base(n, v, o) {}
fwd_iterator(const nonconst_iterator& i) : iterator_base(i) {}
selftype& operator = (const nonconst_iterator& i) { iterator_base::operator = (i); return *this; }
reference operator * () const { verify_not_end(); verify_iterator(); return *m_value; }
pointer operator -> () const { return &(operator*()); }
selftype operator - (difference_type i) const { selftype n(*this); n.mod(-i); return n; }
selftype& operator -= (difference_type i) { mod(-i); return *this; }
selftype& operator -- () /* --iterator */ { decr(); return *this; }
selftype operator -- (int) /* iterator-- */ { selftype n(*this); decr(); return n; }
selftype operator + (difference_type i) const { selftype n(*this); n.mod(i); return n; }
selftype& operator += (difference_type i) { mod(i); return *this; }
selftype& operator ++ () /* ++iterator */ { incr(); return *this; }
selftype operator ++ (int) /* iterator++ */ { selftype n(*this); incr(); return n; }
difference_type operator - (const selftype& i) const { return diff(i); }
};
template<class Traits> class rev_iterator : public iterator_base
{
public:
typedef typename Traits::pointer pointer;
typedef typename Traits::reference reference;
typedef rev_iterator<nonconst_traits<T> > nonconst_iterator;
typedef rev_iterator<Traits> selftype;
rev_iterator(deque_node* n = 0, T* v = 0, const deque* o = 0) : iterator_base(n, v, o) {}
rev_iterator(const nonconst_iterator& i) : iterator_base(i) {}
selftype& operator = (const nonconst_iterator& i) { iterator_base::operator = (i); return *this; }
reference operator * () const { verify_not_end(); verify_iterator(); return *m_value; }
pointer operator -> () const { return &(operator*()); }
selftype operator - (difference_type i) const { selftype n(*this); n.mod(i); return n; }
selftype& operator -= (difference_type i) { mod(i); return *this; }
selftype& operator -- () /* --iterator */ { incr(); return *this; }
selftype operator -- (int) /* iterator-- */ { selftype n(*this); incr(); return n; }
selftype operator + (difference_type i) const { selftype n(*this); n.mod(-i); return n; }
selftype& operator += (difference_type i) { mod(-i); return *this; }
selftype& operator ++ () /* ++iterator */ { decr(); return *this; }
selftype operator ++ (int) /* iterator++ */ { selftype n(*this); decr(); return n; }
difference_type operator - (const selftype& i) const { return i.diff(*this); }
};
typedef fwd_iterator<nonconst_traits<T> > iterator;
typedef fwd_iterator<const_traits<T> > const_iterator;
typedef rev_iterator<nonconst_traits<T> > reverse_iterator;
typedef rev_iterator<const_traits<T> > const_reverse_iterator;
// constructors
deque() :
m_head(terminator(), terminator(), 0, 0),
m_size(0)
{}
deque(size_type n) :
m_head(terminator(), terminator(), 0, 0),
m_size(n)
{
if (n != 0)
{
// Round up
const size_type nodecount = ((n + (block_count-1)) / block_count);
m_nodes.reserve(nodecount);
for (size_type i = 0; i != nodecount; ++i )
{
const size_type elems = min<size_type>(block_count, n);
n -= elems;
deque_node* node = insert_node(terminator(), 0, elems);
typetraits<T>::range_construct(node->start(), node->end());
m_nodes.push_back(node);
}
}
}
deque(size_type n, const T& t) :
m_head(terminator(), terminator(), 0, 0),
m_size(n)
{
if (n != 0)
{
// Round up
const size_type nodecount = ((n + (block_count-1)) / block_count);
m_nodes.reserve(nodecount);
for (size_type i = 0; i != nodecount; ++i )
{
const size_type elems = min<size_type>(block_count, n);
n -= elems;
deque_node* node = insert_node(terminator(), 0, elems);
typetraits<T>::range_construct(node->start(), node->end(), t);
m_nodes.push_back(node);
}
}
}
deque(const deque& D) :
m_head(terminator(), terminator(), 0, 0),
m_size(0)
{
operator = (D);
}
template <class InputIterator> deque(InputIterator first, InputIterator last) :
m_head(terminator(), terminator(), 0, 0),
m_size(0)
{
insert(end(), first, last);
}
~deque()
{
clear();
}
template <class InputIterator> void assign(InputIterator first, InputIterator last)
{
// TODO: This could probably be done better (re-use existing nodes, etc)
clear();
insert(end(), first, last);
}
deque& operator = (const deque& D)
{
clear();
m_size = D.m_size;
m_nodes.reserve(D.m_nodes.size());
for (size_type i = 0; i != D.m_nodes.size(); ++i)
{
const deque_node* rhsnode = D.m_nodes[i];
deque_node* lhsnode = insert_node(terminator(), rhsnode->startindex, rhsnode->endindex);
THOR_DEBUG_ASSERT(rhsnode->size() == lhsnode->size());
typetraits<T>::range_construct(lhsnode->start(), lhsnode->end(), rhsnode->start());
m_nodes.push_back(lhsnode);
}
return *this;
}
size_type size() const
{
return m_size;
}
size_type max_size() const
{
return size_type(-1);
}
bool empty() const
{
return m_size == 0;
}
// element access
T& operator [] (size_type n)
{
return *element(n);
}
const T& operator [] (size_type n) const
{
return *element(n);
}
T& at(size_type n)
{
return *element(n);
}
const T& at(size_type n) const
{
return *element(n);
}
T& front()
{
THOR_ASSERT(!empty());
return *element(0);
}
const T& front() const
{
THOR_ASSERT(!empty());
return *element(0);
}
T& back()
{
THOR_ASSERT(!empty());
return *element(m_size - 1);
}
const T& back() const
{
THOR_ASSERT(!empty());
return *element(m_size - 1);
}
// extension that returns a pointer to the contiguous space at pos.
// the count parameter returns the number of items available.
// get_contiguous(end()) returns a null pointer with a count of zero.
T* get_contiguous(iterator pos, size_type& count)
{
verify_iterator(pos);
if (pos.m_node != terminator())
{
count = pos.m_node->end() - pos.m_value;
return pos.m_value;
}
count = 0;
return 0;
}
const T* get_contiguous(const_iterator pos, size_type& count) const
{
verify_iterator(pos);
if (pos.m_node != terminator())
{
count = pos.m_node->end() - pos.m_value;
return pos.m_value;
}
count = 0;
return 0;
}
// iteration
iterator begin()
{
return iterator(m_head.next, empty() ? 0 : m_head.next->start(), this);
}
const_iterator begin() const
{
return const_iterator(m_head.next, empty() ? 0 : m_head.next->start(), this);
}
iterator end()
{
return iterator(terminator(), 0, this);
}
const_iterator end() const
{
return const_iterator(terminator(), 0, this);
}
reverse_iterator rbegin()
{
return reverse_iterator(m_head.prev, empty() ? 0 : m_head.prev->end() - 1, this);
}
const_reverse_iterator rbegin() const
{
return const_reverse_iterator(m_head.prev, empty() ? 0 : m_head.prev->end() - 1, this);
}
reverse_iterator rend()
{
return reverse_iterator(terminator(), 0, this);
}
const_reverse_iterator rend() const
{
return const_reverse_iterator(terminator(), 0, this);
}
void swap(deque& D)
{
// Fixup terminators
deque_node *&lhsn = m_head.prev->next, *&lhsp = m_head.next->prev;
deque_node *&rhsn = D.m_head.prev->next, *&rhsp = D.m_head.next->prev;
lhsn = lhsp = D.terminator();
rhsn = rhsp = terminator();
m_nodes.swap(D.m_nodes);
thor::swap(m_head, D.m_head);
thor::swap(m_size, D.m_size);
}
// element insertion
T& push_front()
{
T* p = internal_push_front();
typetraits<T>::construct(p);
return *p;
}
template <class T1> T& push_front(const T1& t1)
{
T* p = internal_push_front();
typetraits<T>::construct(p, t1);
return *p;
}
template <class T1, class T2> T& push_front(const T1& t1, const T2& t2)
{
T* p = internal_push_front();
typetraits<T>::construct(p, t1, t2);
return *p;
}
template <class T1, class T2, class T3> T& push_front(const T1& t1, const T2& t2, const T3& t3)
{
T* p = internal_push_front();
typetraits<T>::construct(p, t1, t2, t3);
return *p;
}
template <class T1, class T2, class T3, class T4> T& push_front(const T1& t1, const T2& t2, const T3& t3, const T4& t4)
{
T* p = internal_push_front();
typetraits<T>::construct(p, t1, t2, t3, t4);
return *p;
}
// Requires use of placement new to construct the item
// Example: new (d.push_front_placement()) Value;
void* push_front_placement()
{
return internal_push_front();
}
T& push_back()
{
T* p = internal_push_back();
typetraits<T>::construct(p);
return *p;
}
template <class T1> T& push_back(const T1& t1)
{
T* p = internal_push_back();
typetraits<T>::construct(p, t1);
return *p;
}
template <class T1, class T2> T& push_back(const T1& t1, const T2& t2)
{
T* p = internal_push_back();
typetraits<T>::construct(p, t1, t2);
return *p;
}
template <class T1, class T2, class T3> T& push_back(const T1& t1, const T2& t2, const T3& t3)
{
T* p = internal_push_back();
typetraits<T>::construct(p, t1, t2, t3);
return *p;
}
template <class T1, class T2, class T3, class T4> T& push_back(const T1& t1, const T2& t2, const T3& t3, const T4& t4)
{
T* p = internal_push_back();
typetraits<T>::construct(p, t1, t2, t3, t4);
return *p;
}
// Requires use of placement new to construct the item
// Example: new (d.push_back_placement()) Value;
void* push_back_placement()
{
return internal_push_back();
}
iterator insert(iterator pos)
{
verify_iterator(pos);
internal_insert(pos.m_node, pos.m_value);
typetraits<T>::construct(pos.m_value);
return pos;
}
template <class T1> iterator insert(iterator pos, const T1& t1)
{
verify_iterator(pos);
internal_insert(pos.m_node, pos.m_value);
typetraits<T>::construct(pos.m_value, t1);
return pos;
}
template <class T1, class T2> iterator insert(iterator pos, const T1& t1, const T2& t2)
{
verify_iterator(pos);
internal_insert(pos.m_node, pos.m_value);
typetraits<T>::construct(pos.m_value, t1, t2);
return pos;
}
template <class T1, class T2, class T3> iterator insert(iterator pos, const T1& t1, const T2& t2, const T3& t3)
{
verify_iterator(pos);
internal_insert(pos.m_node, pos.m_value);
typetraits<T>::construct(pos.m_value, t1, t2, t3);
return pos;
}
template <class T1, class T2, class T3, class T4> iterator insert(iterator pos, const T1& t1, const T2& t2, const T3& t3, const T4& t4)
{
verify_iterator(pos);
internal_insert(pos.m_node, pos.m_value);
typetraits<T>::construct(pos.m_value, t1, t2, t3, t4);
return pos;
}
// Requires use of placement new to construct the item
// Example: new (d.insert_placement(pos)) Value;
void* insert_placement(iterator pos)
{
verify_iterator(pos);
internal_insert(pos.m_node, pos.m_value);
return pos.m_value;
}
void insert(iterator pos, size_type n, const T& t)
{
verify_iterator(pos);
if (n == 0)
{
return;
}
// Just grow the middle; could optimize for front/back
pos.m_value = grow(pos.m_node, pos.m_value, n);
// Loop and construct
while (n != 0)
{
size_type remain = min<size_type>(n, pos.m_node->end() - pos.m_value);
typetraits<T>::range_construct(pos.m_value, pos.m_value + remain, t);
n -= remain;
pos.m_node = pos.m_node->next;
pos.m_value = pos.m_node->start();
}
validate();
}
template <class InputIterator> void insert(iterator pos, InputIterator first, InputIterator last)
{
verify_iterator(pos);
const size_type n = distance(first, last);
if (n == 0)
{
return;
}
// Just grow the middle; could optimize for front/back
pos.m_value = grow(pos.m_node, pos.m_value, n);
// Loop and construct
while (first != last)
{
THOR_DEBUG_ASSERT(pos.m_node != terminator());
typetraits<T>::construct(pos.m_value, *first);
++first, ++pos.m_value;
if (pos.m_value == pos.m_node->end())
{
pos.m_node = pos.m_node->next;
pos.m_value = pos.m_node->start();
}
}
validate();
}
// element removal
void pop_front()
{
THOR_ASSERT(!empty());
if (!empty())
{
deque_node* node = m_head.next;
THOR_DEBUG_ASSERT(node == m_nodes.front());
THOR_DEBUG_ASSERT(node != terminator());
node->destroy_start();
if (node->size() == 0)
{
// Destroy the front node.
node->prev->next = node->next;
node->next->prev = node->prev;
destroy_node(node);
m_nodes.erase(m_nodes.begin());
}
--m_size;
validate();
}
}
// extension that deletes the front item and pops it from the deque
// valid for containers of pointer types only
void pop_front_delete()
{
delete front();
pop_front();
}
void pop_back()
{
THOR_ASSERT(!empty());
if (!empty())
{
deque_node* node = m_head.prev;
THOR_DEBUG_ASSERT(node == m_nodes.back());
THOR_DEBUG_ASSERT(node != terminator());
node->destroy_end();
if (node->size() == 0)
{
// Destroy the back node
node->prev->next = node->next;
node->next->prev = node->prev;
destroy_node(node);
m_nodes.pop_back();
}
--m_size;
validate();
}
}
// extension that deletes the back item and pops it from the deque
// valid for containers of pointer types only
void pop_back_delete()
{
delete back();
pop_back();
}
void clear()
{
for (size_type i = 0; i != m_nodes.size(); ++i)
{
deque_node* node = m_nodes[i];
THOR_DEBUG_ASSERT(node->size() != 0);
destroy_node(node);
}
m_size = 0;
m_head.next = m_head.prev = terminator();
m_nodes.clear();
validate();
}
// Extension that deletes all elements, followed by a clear().
// Valid for deques of pointer types only
void delete_all()
{
for (size_type i = 0; i != m_nodes.size(); ++i)
{
deque_node* node = m_nodes[i];
for (T* p = node->start(); p != node->end(); ++p)
{
delete *p;
}
THOR_DEBUG_ASSERT(node->size() != 0);
destroy_node(node);
}
m_size = 0;
m_head.next = m_head.prev = terminator();
m_nodes.clear();
validate();
}
iterator erase(iterator pos)
{
THOR_ASSERT(!empty());
verify_iterator(pos);
pos.verify_not_end();
--m_size;
size_type nodeindex = find(m_nodes.begin(), m_nodes.end(), pos.m_node) - m_nodes.begin();
if (nodeindex < (m_nodes.size() / 2))
{
// Closer to beginning
deque_node* fromnode = pos.m_node;
deque_node* tonode = fromnode;
T* to = pos.m_value;
const T* from = to - 1;
for (;;)
{
if (from < fromnode->start())
{
fromnode = fromnode->prev;
from = fromnode->end() - 1;
if (fromnode == terminator())
{
break;
}
}
if (to < tonode->start())
{
tonode = tonode->prev;
to = tonode->end() - 1;
THOR_DEBUG_ASSERT(tonode != terminator());
}
*to = *from;
--to, --from;
}
THOR_DEBUG_ASSERT(m_nodes.front() == tonode);
tonode->destroy_start();
if (tonode->size() == 0)
{
tonode->next->prev = tonode->prev;
tonode->prev->next = tonode->next;
m_nodes.erase(m_nodes.begin());
destroy_node(tonode);
}
}
else
{
// Closer to end
deque_node* tonode = pos.m_node;
deque_node* fromnode = tonode;
T* to = pos.m_value;
const T* from = to + 1;
for (;;)
{
if (from == fromnode->end())
{
fromnode = fromnode->next;
from = fromnode->start();
if (fromnode == terminator())
{
break;
}
}
if (to == tonode->end())
{
tonode = tonode->next;
to = tonode->start();
THOR_DEBUG_ASSERT(tonode != terminator());
}
*to = *from;
++to, ++from;
}
THOR_DEBUG_ASSERT(m_nodes.back() == tonode);
tonode->destroy_end();
if (tonode->size() == 0)
{
tonode->next->prev = tonode->prev;
tonode->prev->next = tonode->next;
m_nodes.pop_back();
destroy_node(tonode);
}
}
validate();
return m_nodes.empty() ? end() : pos;
}
// extension that deletes the element at pos followed by erasing it
// valid for deques of pointer types only
iterator erase_and_delete(iterator pos)
{
delete *pos;
return erase(pos);
}
iterator erase(iterator first, iterator last)
{
verify_iterator(first);
verify_iterator(last);
if (first == last)
{
return first;
}
first.verify_not_end();
if (last.m_node == terminator())
{
// ends at end(), no need to copy anything
size_type removenodes = 0;
deque_node* node = first.m_node;
T* start = first.m_value;
do
{
const size_type num = node->end() - start;
node->destroy_end(num);
m_size -= num;
deque_node* next = node->next;
if (node->size() == 0)
{
++removenodes;
node->next->prev = node->prev;
node->prev->next = node->next;
destroy_node(node);
}
node = next;
start = node->start();
} while (node != terminator());
m_nodes.erase(m_nodes.end() - removenodes, m_nodes.end());
validate();
return last;
}
// Guaranteed to not end at end().
// Search the node array to approximate if we're closer to the front or back
size_type nodeindex = find(m_nodes.begin(), m_nodes.end(), first.m_node) - m_nodes.begin();
if (nodeindex < (m_nodes.size() / 2))
{
// Closer to front, does not end at end()
deque_node* tonode = last.m_node;
T* to = last.m_value;
deque_node* fromnode = first.m_node;
const T* from = first.m_value;
// Copy backwards
for (;;)
{
--to, --from;
if (from < fromnode->start())