Skip to content

Commit d02ee39

Browse files
committed
finished a constructor
1 parent 9662379 commit d02ee39

File tree

3 files changed

+86
-32
lines changed

3 files changed

+86
-32
lines changed

Deque.h

+67-21
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
#include <stdexcept> // out_of_range
1919
#include <utility> // !=, <=, >, >=
2020

21+
22+
#define AWIDTH 20
23+
24+
2125
// -----
2226
// using
2327
// -----
@@ -74,27 +78,28 @@ BI uninitialized_fill (A& a, BI b, BI e, const U& v) {
7478
// -------
7579
// my_deque
7680
// -------
77-
7881
template < typename T, typename A = std::allocator<T> >
7982
class my_deque {
8083
public:
8184
// --------
8285
// typedefs
8386
// --------
8487

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;
8790

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;
9093

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;
9396

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;
96102

97-
typename A::template rebind<pointer>::other B;
98103

99104
public:
100105
// -----------
@@ -105,7 +110,9 @@ class my_deque {
105110
* <your documentation>
106111
*/
107112
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>
109116
// you must use std::equal()
110117
return true;}
111118

@@ -125,9 +132,17 @@ class my_deque {
125132
// ----
126133
// data
127134
// ----
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+
131146
// <your data>
132147

133148
private:
@@ -138,7 +153,8 @@ class my_deque {
138153
bool valid () const {
139154
// <your code>
140155
return true;}
141-
156+
pointer get_last(){
157+
return _e[((_size)-(AWIDTH-_offset))%AWIDTH];}
142158
public:
143159
// --------
144160
// iterator
@@ -501,15 +517,40 @@ class my_deque {
501517
/**
502518
* <your documentation>
503519
*/
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+
506522
assert(valid());}
507523

508524
/**
509525
* <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+
510536
*/
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);
513554
assert(valid());}
514555

515556
/**
@@ -527,7 +568,7 @@ class my_deque {
527568
* <your documentation>
528569
*/
529570
~my_deque () {
530-
// <your code>
571+
//call _a.destroy(); and then deallocate each array, then deallocate outside array
531572
assert(valid());}
532573

533574
// ----------
@@ -553,6 +594,11 @@ class my_deque {
553594
// <your code>
554595
// dummy is just to be able to compile the skeleton, remove it
555596
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];
556602
return dummy;}
557603

558604
/**
@@ -755,7 +801,7 @@ class my_deque {
755801
*/
756802
size_type size () const {
757803
// <your code>
758-
return 0;}
804+
return _size;}
759805

760806
// ----
761807
// swap

TestDeque.c++

+13-5
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,37 @@ struct Deque_Fixture : Test {
3636
typedef typename deque_type::value_type value_type;};
3737

3838
typedef Types<
39-
deque<int>
40-
//, my_deque<int>
39+
//deque<int>
40+
my_deque<int>
4141
>
4242
deque_types;
4343

4444
TYPED_TEST_CASE(Deque_Fixture, deque_types);
4545

46+
47+
TYPED_TEST(Deque_Fixture, test_3) {
48+
typedef typename TestFixture::deque_type deque_type;
49+
50+
deque_type x (9);
51+
ASSERT_EQ(x.size(),9);
52+
ASSERT_EQ(x[0],0);}
4653
// -----
4754
// Tests
4855
// -----
49-
56+
/*
5057
TYPED_TEST(Deque_Fixture, test_1) {
5158
typedef typename TestFixture::deque_type deque_type;
5259
5360
const deque_type x;
5461
ASSERT_TRUE(x.empty());
5562
ASSERT_EQ(x.size(),0);}
56-
57-
63+
*/
64+
/*
5865
TYPED_TEST(Deque_Fixture, test_2) {
5966
typedef typename TestFixture::deque_type deque_type;
6067
6168
deque_type x;
6269
x.push_back(5);
6370
ASSERT_TRUE(x.back()==5);
6471
ASSERT_EQ(x.size(),1);}
72+
*/

makefile

+6-6
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ sync:
6363

6464
test: TestDeque.out
6565

66-
html: Doxyfile Deque.h TestInteger.c++
66+
html: Doxyfile Deque.h TestDeque.c++
6767
doxygen Doxyfile
6868

6969
Deque.log:
@@ -96,10 +96,10 @@ endif
9696
@echo
9797
doxygen --version
9898

99-
TestDeque: Integer.h TestInteger.c++
100-
$(CXX) $(COVFLAGS) $(CXXFLAGS) TestDeque.c++ -o TestInteger $(LDFLAGS)
99+
TestDeque: Deque.h TestDeque.c++
100+
$(CXX) $(COVFLAGS) $(CXXFLAGS) TestDeque.c++ -o TestDeque $(LDFLAGS)
101101

102-
TestDeque.out: TestInteger
103-
$(VALGRIND) ./TestDeque > TestInteger.out 2>&1
104-
$(GCOV) -b TestDeque.c++ >> TestInteger.out
102+
TestDeque.out: TestDeque
103+
$(VALGRIND) ./TestDeque > TestDeque.out 2>&1
104+
$(GCOV) -b TestDeque.c++ >> TestDeque.out
105105
cat TestDeque.out

0 commit comments

Comments
 (0)