18
18
#include < stdexcept> // out_of_range
19
19
#include < utility> // !=, <=, >, >=
20
20
21
+
22
+ #define AWIDTH 20
23
+
24
+
21
25
// -----
22
26
// using
23
27
// -----
@@ -74,27 +78,28 @@ BI uninitialized_fill (A& a, BI b, BI e, const U& v) {
74
78
// -------
75
79
// my_deque
76
80
// -------
77
-
78
81
template < typename T, typename A = std::allocator<T> >
79
82
class my_deque {
80
83
public:
81
84
// --------
82
85
// typedefs
83
86
// --------
84
87
85
- typedef A allocator_type;
86
- typedef typename allocator_type::value_type value_type;
88
+ typedef A allocator_type; // inner?
89
+ typedef typename allocator_type::value_type value_type;
87
90
88
- typedef typename allocator_type::size_type size_type;
89
- typedef typename allocator_type::difference_type difference_type;
91
+ typedef typename allocator_type::size_type size_type;
92
+ typedef typename allocator_type::difference_type difference_type;
90
93
91
- typedef typename allocator_type::pointer pointer;
92
- typedef typename allocator_type::const_pointer const_pointer;
94
+ typedef typename allocator_type::pointer pointer;
95
+ typedef typename allocator_type::const_pointer const_pointer;
93
96
94
- typedef typename allocator_type::reference reference;
95
- typedef typename allocator_type::const_reference const_reference;
97
+ typedef typename allocator_type::reference reference;
98
+ typedef typename allocator_type::const_reference const_reference;
99
+
100
+ typedef typename A::template rebind<pointer>::other B; // outer array
101
+ typedef typename B::pointer b_pointer;
96
102
97
- typename A::template rebind<pointer>::other B;
98
103
99
104
public:
100
105
// -----------
@@ -105,7 +110,9 @@ class my_deque {
105
110
* <your documentation>
106
111
*/
107
112
friend bool operator == (const my_deque& lhs, const my_deque& rhs) {
108
- // <your code>
113
+ typename my_deque::iterator b1 = lhs.begin ();// <your code>
114
+ typename my_deque::iterator b2 = rhs.begin ();// <your code>
115
+ typename my_deque::iterator e = lhs.end ();// <your code>
109
116
// you must use std::equal()
110
117
return true ;}
111
118
@@ -125,9 +132,17 @@ class my_deque {
125
132
// ----
126
133
// data
127
134
// ----
128
-
129
- allocator_type _a;
130
-
135
+ // AWIDTH for inner array size
136
+ allocator_type _a; // allocator for inner arrays
137
+ B _outter; // allocator for outer array?
138
+ b_pointer _top; // points to first container
139
+ size_type _e; // end of size
140
+ size_type _b; // begining
141
+ // pointer_l; //for capacity of current
142
+ difference_type _top_size;
143
+ difference_type _offset;
144
+ size_type _size;
145
+
131
146
// <your data>
132
147
133
148
private:
@@ -138,7 +153,8 @@ class my_deque {
138
153
bool valid () const {
139
154
// <your code>
140
155
return true ;}
141
-
156
+ pointer get_last (){
157
+ return _e[((_size)-(AWIDTH-_offset))%AWIDTH];}
142
158
public:
143
159
// --------
144
160
// iterator
@@ -501,15 +517,40 @@ class my_deque {
501
517
/* *
502
518
* <your documentation>
503
519
*/
504
- explicit my_deque (const allocator_type& a = allocator_type()) {
505
- // <your code>
520
+ explicit my_deque (const allocator_type& a = allocator_type()):_a(a) {
521
+
506
522
assert (valid ());}
507
523
508
524
/* *
509
525
* <your documentation>
526
+ * allocator_type _a; // allocator for inner arrays
527
+ B _b; //allocator for outer array?
528
+ b_pointer _top; //points to first container
529
+ pointer _e; //end of size
530
+ pointer _b; // begining
531
+ //pointer_l; //for capacity of current
532
+ difference_type _top_size;
533
+ difference_type offset;
534
+ size_type size;
535
+
510
536
*/
511
- explicit my_deque (size_type s, const_reference v = value_type(), const allocator_type& a = allocator_type()) {
512
- // <your code>
537
+ explicit my_deque (size_type s, const_reference v = value_type(), const allocator_type& a = allocator_type()): _a(a) {
538
+ _top = _outter.allocate (s/AWIDTH * 2 + 1 );
539
+ _top_size = s/AWIDTH * 2 + 1 ;
540
+ _top[1 ]=_a.allocate (AWIDTH);
541
+ _b = 1 ;
542
+ // allocate all the needed inner arrays
543
+ int i = 0 ;
544
+ for (i=1 ; i< s/ AWIDTH; i++){
545
+ _top[i+1 ] = _a.allocate (AWIDTH);
546
+ }
547
+ _e = i;
548
+ // set variables
549
+ _size=s;
550
+ _offset=0 ;
551
+
552
+ // do the copy stuff
553
+ uninitialized_fill (_a, begin (), end (), v);
513
554
assert (valid ());}
514
555
515
556
/* *
@@ -527,7 +568,7 @@ class my_deque {
527
568
* <your documentation>
528
569
*/
529
570
~my_deque () {
530
- // <your code>
571
+ // call _a.destroy(); and then deallocate each array, then deallocate outside array
531
572
assert (valid ());}
532
573
533
574
// ----------
@@ -553,6 +594,11 @@ class my_deque {
553
594
// <your code>
554
595
// dummy is just to be able to compile the skeleton, remove it
555
596
static value_type dummy;
597
+ if (index < AWIDTH-_offset){
598
+ dummy = _top[_b][(index +_offset)%AWIDTH];
599
+ }
600
+ index -= AWIDTH-_offset;
601
+ dummy = _top[_b+1 +index /AWIDTH][index %AWIDTH];
556
602
return dummy;}
557
603
558
604
/* *
@@ -755,7 +801,7 @@ class my_deque {
755
801
*/
756
802
size_type size () const {
757
803
// <your code>
758
- return 0 ;}
804
+ return _size ;}
759
805
760
806
// ----
761
807
// swap
0 commit comments