-
Notifications
You must be signed in to change notification settings - Fork 0
/
deque_vector_chunk.cpp
838 lines (741 loc) · 21.5 KB
/
deque_vector_chunk.cpp
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
#ifndef SJTU_DEQUE_HPP
#define SJTU_DEQUE_HPP
#include "exceptions.hpp"
#include <cstddef>
#include <cstring>
#include <cstdlib>
namespace sjtu
{
template <class T>
class deque
{
private:
friend class iterator;
static const unsigned chunk_size = 512; // cannot be 1 otherwise there'd be something wrong with iterator
struct Chunk
{
T *data;
Chunk *prev;
Chunk *next;
std::allocator<T> allocator;
/* TODO: write our own allocator */
Chunk(Chunk *prev = NULL, Chunk *next = NULL) : prev(prev),
next(next)
{
data = allocator.allocate(chunk_size);
}
Chunk(const Chunk &other) = delete;
static Chunk *construct_from(const Chunk &other, int data_begin, int data_end)
{
Chunk *chunk = new Chunk;
for (int i = data_begin; i < data_end; i++)
chunk->allocator.construct(chunk->data + i, other.data[i]);
return chunk;
}
void destruct_range(int data_begin, int data_end)
{
for (int i = data_begin; i < data_end; i++)
allocator.destroy(data + i);
}
~Chunk() { allocator.deallocate(data, chunk_size); }
} * head, *tail;
T *chunk_head, *chunk_tail;
void destruct()
{
Chunk *ptr = head;
while (ptr->prev)
ptr = ptr->prev;
bool is_in_range = false;
while (ptr)
{
Chunk *tmp = ptr->next;
int data_begin = 0;
int data_end = chunk_size;
if (ptr == head)
{
data_begin = chunk_head - head->data;
is_in_range = true;
}
if (ptr == tail)
data_end = chunk_tail - tail->data;
if (is_in_range)
ptr->destruct_range(data_begin, data_end);
if (ptr == tail)
is_in_range = false;
delete ptr;
ptr = tmp;
}
}
void copy_from(const deque &other)
{
Chunk *ptr = other.head;
Chunk *prev = NULL;
while (ptr)
{
int data_begin = 0;
int data_end = chunk_size;
if (ptr == other.head)
data_begin = other.chunk_head - other.head->data;
if (ptr == other.tail)
data_end = other.chunk_tail - other.tail->data;
Chunk *cur = Chunk::construct_from(*ptr, data_begin, data_end);
cur->prev = prev;
if (prev)
prev->next = cur;
else
this->head = cur;
prev = cur;
ptr = ptr->next;
}
this->tail = prev;
this->chunk_head = this->head->data + (other.chunk_head - other.head->data);
this->chunk_tail = this->tail->data + (other.chunk_tail - other.tail->data);
}
void create_new()
{
tail = head = new Chunk;
chunk_head = chunk_tail = head->data;
}
void append_chunk()
{
if (!tail->next)
tail->next = new Chunk(tail);
tail = tail->next;
}
void prepend_chunk()
{
if (!head->prev)
head->prev = new Chunk(NULL, head);
head = head->prev;
}
void shrink_tail_chunk()
{
Chunk *tmp = tail->prev;
delete tail;
tail = tmp;
tail->next = NULL;
}
void shrink_head_chunk()
{
Chunk *tmp = head->next;
delete head;
head = tmp;
head->prev = NULL;
}
void throw_when_empty() const
{
if (this->empty())
throw container_is_empty();
}
public:
class const_iterator;
class iterator
{
private:
friend deque<T>;
void debug(const char *msg) const
{
fprintf(stderr, "%s: chunk %x at %d\n", msg, chunk, pos - chunk->data);
}
bool is_at_the_end() const { return pos == chunk->data + chunk_size; }
int distance_to_head() const
{
Chunk *ptr = q->head;
int chunk_cnt = 0;
while (ptr != chunk)
{
++chunk_cnt;
ptr = ptr->next;
}
int head_offset = q->chunk_head - q->head->data;
int tail_offset = pos - chunk->data;
return tail_offset + chunk_cnt * chunk_size - head_offset;
};
protected:
deque *q;
Chunk *chunk;
T *pos;
iterator(deque *q, Chunk *chunk, T *pos) : q(q), chunk(chunk), pos(pos) {}
void move_chunk(int i)
{
if (i == 0)
return;
int offset = pos - chunk->data;
while (i > 0)
{
--i;
if (!chunk->next)
{
if (chunk == q->tail && offset == 0)
{
offset = chunk_size;
}
else
throw index_out_of_bound();
}
else
chunk = chunk->next;
}
while (i < 0)
{
++i;
if (!chunk->prev)
{
throw index_out_of_bound();
}
chunk = chunk->prev;
}
pos = chunk->data + offset;
}
void move_forward(int i)
{
if (i == 0)
return;
while (i < 0)
{
++i;
if (pos == chunk->data)
{
if (!chunk->prev)
throw index_out_of_bound();
chunk = chunk->prev;
pos = chunk->data + chunk_size;
}
--pos;
if (chunk == q->head && pos < q->chunk_head)
throw index_out_of_bound();
}
while (i > 0)
{
--i;
++pos;
if (pos == chunk->data + chunk_size)
{
if (chunk != q->tail)
{
if (!chunk->next)
throw index_out_of_bound();
chunk = chunk->next;
pos = chunk->data;
}
}
if (chunk == q->tail && pos > q->chunk_tail)
throw index_out_of_bound();
}
}
public:
/** constructors **/
iterator() : q(NULL), chunk(NULL), pos(NULL) {}
iterator(const iterator &other) : iterator(other.q, other.chunk, other.pos) {}
/**
* return a new iterator which pointer n-next elements
* even if there are not enough elements, the behaviour is **undefined**.
* as well as operator-
*/
iterator operator+(const int &n) const
{
iterator that(*this);
if (n > 0)
{
if (n <= chunk_size)
that.move_forward(n);
else
{
that.move_forward(n % chunk_size);
that.move_chunk(n / chunk_size);
}
}
else if (n < 0)
{
if (n >= -chunk_size)
that.move_forward(n);
else
{
that.move_forward(-(int)((-n) % chunk_size));
that.move_chunk(-(int)((-n) / chunk_size));
}
}
return that;
}
iterator operator-(const int &n) const { return this->operator+(-n); }
// return th distance between two iterator,
// if these two iterators points to different vectors, throw invaild_iterator.
int operator-(const iterator &rhs) const
{
if (rhs.q != this->q)
throw invalid_iterator();
return this->distance_to_head() - rhs.distance_to_head();
}
iterator operator+=(const int &n)
{
this->move_forward(n);
return *this;
}
iterator operator-=(const int &n)
{
this->move_forward(-n);
return *this;
}
/**
* iter++
*/
iterator operator++(int)
{
iterator that(*this);
this->move_forward(1);
return that;
}
/**
* ++iter
*/
iterator &operator++()
{
this->move_forward(1);
return *this;
}
/**
* iter--
*/
iterator operator--(int)
{
iterator that(*this);
this->move_forward(-1);
return that;
}
/**
* --iter
*/
iterator &operator--()
{
this->move_forward(-1);
return *this;
}
/**
* *it
*/
T &operator*() const
{
if (chunk == q->tail && pos == q->chunk_tail)
throw index_out_of_bound();
return *pos;
}
/**
* it->field
*/
T *operator->() const noexcept { return pos; }
/**
* a operator to check whether two iterators are same (pointing to the same memory).
*/
bool operator==(const iterator &rhs) const
{
return (q == rhs.q) && (chunk == rhs.chunk) && (pos == rhs.pos);
}
bool operator==(const const_iterator &rhs) const
{
return (q == rhs.q) && (chunk == rhs.chunk) && (pos == rhs.pos);
}
/**
* some other operator for iterator.
*/
bool operator!=(const iterator &rhs) const { return !(*this == rhs); }
bool operator!=(const const_iterator &rhs) const { return !(*this == rhs); }
};
class const_iterator
{
// it should has similar member method as iterator.
// and it should be able to construct from an iterator.
private:
friend deque<T>;
bool is_at_the_end() { return pos == chunk->data + chunk_size; }
int distance_to_head() const
{
Chunk *ptr = q->head;
int chunk_cnt = 0;
while (ptr != chunk)
{
++chunk_cnt;
ptr = ptr->next;
}
int head_offset = q->chunk_head - q->head->data;
int tail_offset = pos - chunk->data;
return tail_offset + chunk_cnt * chunk_size - head_offset;
};
void move_chunk(int i)
{
if (i == 0)
return;
int offset = pos - chunk->data;
while (i > 0)
{
--i;
if (!chunk->next)
{
if (chunk == q->tail && offset == 0)
{
offset = chunk_size;
}
else
throw index_out_of_bound();
}
else
chunk = chunk->next;
}
while (i < 0)
{
++i;
if (!chunk->prev)
{
throw index_out_of_bound();
}
chunk = chunk->prev;
}
pos = chunk->data + offset;
}
void move_forward(int i)
{
if (i == 0)
return;
while (i < 0)
{
++i;
if (pos == chunk->data)
{
if (!chunk->prev)
throw index_out_of_bound();
chunk = chunk->prev;
pos = chunk->data + chunk_size;
}
--pos;
if (chunk == q->head && pos < q->chunk_head)
throw index_out_of_bound();
}
while (i > 0)
{
--i;
++pos;
if (pos == chunk->data + chunk_size)
{
if (chunk != q->tail)
{
if (!chunk->next)
throw index_out_of_bound();
chunk = chunk->next;
pos = chunk->data;
}
}
if (chunk == q->tail && pos > q->chunk_tail)
throw index_out_of_bound();
}
}
protected:
const deque *q;
const Chunk *chunk;
const T *pos;
const_iterator(const deque *q, const Chunk *chunk, const T *pos) : q(q), chunk(chunk), pos(pos) {}
public:
const_iterator(const const_iterator &other) : const_iterator(other.q, other.chunk, other.pos) {}
const_iterator(const iterator &other) : const_iterator(other.q, other.chunk.other.pos) {}
const_iterator() {}
/**
* return a new iterator which pointer n-next elements
* even if there are not enough elements, the behaviour is **undefined**.
* as well as operator-
*/
const_iterator operator+(const int &n) const
{
const_iterator that(*this);
if (n > 0)
{
that.move_forward(n % chunk_size);
that.move_chunk(n / chunk_size);
}
else if (n < 0)
{
/* TODO: is chunk_size is 1, then move_forward is always called with 0, which may cause exception */
that.move_forward(-(int)((-n) % chunk_size));
that.move_chunk(-(int)((-n) / chunk_size));
}
return that;
}
const_iterator operator-(const int &n) const { return this->operator+(-n); }
// return th distance between two iterator,
// if these two iterators points to different vectors, throw invaild_iterator.
int operator-(const const_iterator &rhs) const
{
if (rhs.q != this->q)
throw invalid_iterator();
return this->distance_to_head() - rhs.distance_to_head();
}
/**
* *it
*/
const T &operator*() const
{
if (chunk == q->tail && pos == q->chunk_tail)
throw index_out_of_bound();
return *pos;
}
/**
* it->field
*/
const T *operator->() const noexcept { return this->pos; }
bool operator==(const iterator &rhs) const
{
return (q == rhs.q) && (chunk == rhs.chunk) && (pos == rhs.pos);
}
bool operator==(const const_iterator &rhs) const
{
return (q == rhs.q) && (chunk == rhs.chunk) && (pos == rhs.pos);
}
/**
* some other operator for iterator.
*/
bool operator!=(const iterator &rhs) const { return !(*this == rhs); }
bool operator!=(const const_iterator &rhs) const { return !(*this == rhs); }
const_iterator operator+=(const int &n)
{
this->move_forward(n);
return *this;
}
const_iterator operator-=(const int &n)
{
this->move_forward(-n);
return *this;
}
/**
* iter++
*/
const_iterator operator++(int)
{
iterator that(*this);
this->move_forward(1);
return that;
}
/**
* ++iter
*/
const_iterator &operator++()
{
this->move_forward(1);
return *this;
}
/**
* iter--
*/
const_iterator operator--(int)
{
iterator that(*this);
this->move_forward(-1);
return that;
}
/**
* --iter
*/
const_iterator &operator--()
{
this->move_forward(-1);
return *this;
}
};
/**
* constructors
*/
deque() { create_new(); }
deque(const deque &other) { this->copy_from(other); }
/**
* deconstructor
*/
~deque() { this->destruct(); }
/**
* assignment operator
*/
deque &operator=(const deque &other)
{
if (this == &other)
return *this;
this->destruct();
this->copy_from(other);
return *this;
}
/**
* access specified element with bounds checking
* throw index_out_of_bound if out of bound.
*/
T &at(const size_t &pos) { return *(begin() + pos); }
const T &at(const size_t &pos) const { return *(cbegin() + pos); }
T &operator[](const size_t &pos) { return this->at(pos); }
const T &operator[](const size_t &pos) const { return this->at(pos); }
/**
* access the first element
* throw container_is_empty when the container is empty.
*/
const T &front() const
{
throw_when_empty();
return *chunk_head;
}
/**
* access the last element
* throw container_is_empty when the container is empty.
*/
const T &back() const
{
throw_when_empty();
return *(chunk_tail - 1);
}
/**
* returns an iterator to the beginning.
*/
iterator begin() { return iterator(this, head, chunk_head); }
const_iterator cbegin() const { return const_iterator(this, head, chunk_head); }
/**
* returns an iterator to the end.
*/
iterator end() { return iterator(this, tail, chunk_tail); }
const_iterator cend() const { return const_iterator(this, tail, chunk_tail); }
/**
* checks whether the container is empty.
*/
bool empty() const
{
if (head == tail && chunk_head == chunk_tail)
return true;
else
return false;
}
/**
* returns the number of elements
*/
size_t size() const { return cend() - cbegin(); }
/**
* clears the contents
*/
void clear()
{
this->destruct();
this->create_new();
}
/**
* inserts elements at the specified locat on in the container.
* inserts value before pos
* returns an iterator pointing to the inserted value
* throw if the iterator is invalid or it point to a wrong place.
*/
iterator insert(iterator pos, const T &value)
{
if (pos == end())
{
push_back(value);
return end() - 1;
}
push_back(value);
iterator cur = end();
--cur;
while (cur != pos)
{
iterator next = cur;
--cur;
std::swap_ranges(next.pos, next.pos + 1, cur.pos);
}
return pos;
}
/**
* removes specified element at pos.
* removes the element at pos.
* returns an iterator pointing to the following element, if pos pointing to the last element, end() will be returned.
* throw if the container is empty, the iterator is invalid or it points to a wrong place.
*/
iterator erase(iterator pos)
{
iterator lst = end();
--lst;
iterator cur = pos;
while (cur != lst)
{
iterator prev = cur;
++cur;
std::swap_ranges(prev.pos, prev.pos + 1, cur.pos);
}
pop_back();
return pos;
}
/**
* adds an element to the end
*/
void push_back(const T &value)
{
if (chunk_tail - tail->data == chunk_size)
{
append_chunk();
chunk_tail = tail->data;
}
tail->allocator.construct(chunk_tail, value);
++chunk_tail;
}
/**
* removes the last element
* throw when the container is empty.
*/
void pop_back()
{
throw_when_empty();
--chunk_tail;
tail->allocator.destroy(chunk_tail);
if (chunk_tail - tail->data == 0)
{
if (head != tail)
{
shrink_tail_chunk();
chunk_tail = tail->data + chunk_size;
}
}
}
/**
* inserts an element to the beginning.
*/
void push_front(const T &value)
{
if (chunk_head - head->data == 0)
{
prepend_chunk();
chunk_head = head->data + chunk_size;
}
--chunk_head;
head->allocator.construct(chunk_head, value);
}
/**
* removes the first element.
* throw when the container is empty.
*/
void pop_front()
{
throw_when_empty();
head->allocator.destroy(chunk_head);
++chunk_head;
if (chunk_head - head->data == chunk_size)
{
if (head == tail)
{
chunk_head = chunk_tail = head->data;
}
else
{
shrink_head_chunk();
chunk_head = head->data;
}
}
}
void debug()
{
Chunk *ptr = head;
while (ptr)
{
for (int i = 0; i < chunk_size; i++)
fprintf(stderr, "%d ", ptr->data[i]);
ptr = ptr->next;
}
fprintf(stderr, "\n");
}
};
} // namespace sjtu
#endif