Changeset 385c130 for src/examples/gc_no_raii/src/vector.h
 Timestamp:
 Apr 19, 2016, 1:19:25 PM (9 years ago)
 Branches:
 ADT, aaronthesis, armeh, astexperimental, cleanupdtors, ctor, deferred_resn, demangler, enum, forallpointerdecay, gc_noraii, jacob/cs343translation, jenkinssandbox, master, memory, newast, newastuniqueexpr, newenv, no_list, persistentindexer, pthreademulation, qualifiedEnum, resolvnew, with_gc
 Children:
 9026b4b
 Parents:
 356bb95
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

src/examples/gc_no_raii/src/vector.h
r356bb95 r385c130 8 8 // 9 9 //Declaration 10 context allocator_c(type T, type allocator_t) { 10 trait allocator_c(otype T, otype allocator_t) { 11 void ctor(allocator_t*); 12 void dtor(allocator_t*); 11 13 void realloc(allocator_t*, size_t); 12 14 T* data(allocator_t*); 13 15 }; 14 16 15 forall( type T,type allocator_t  allocator_c(T, allocator_t))17 forall(otype T, otype allocator_t  allocator_c(T, allocator_t)) 16 18 struct vector 17 19 { … … 21 23 22 24 // 25 //Initialization 26 forall(otype T, otype allocator_t  allocator_c(T, allocator_t)) 27 void vector_ctor(vector(T, allocator_t) *const this); 28 29 forall(otype T, otype allocator_t  allocator_c(T, allocator_t)) 30 void dtor(vector(T, allocator_t) *const this); 31 32 // 23 33 //Capacity 24 forall( type T,type allocator_t  allocator_c(T, allocator_t))25 bool empty(vector(T, allocator_t)*this)34 forall(otype T, otype allocator_t  allocator_c(T, allocator_t)) 35 inline bool empty(vector(T, allocator_t) *const this) 26 36 { 27 37 return this>size == 0; 28 38 } 29 39 30 forall( type T,type allocator_t  allocator_c(T, allocator_t))31 bool size(vector(T, allocator_t)*this)40 forall(otype T, otype allocator_t  allocator_c(T, allocator_t)) 41 inline bool size(vector(T, allocator_t) *const this) 32 42 { 33 43 return this>size; 34 44 } 35 45 36 forall( type T,type allocator_t  allocator_c(T, allocator_t))37 void reserve(vector(T, allocator_t)*this, size_t size)46 forall(otype T, otype allocator_t  allocator_c(T, allocator_t)) 47 inline void reserve(vector(T, allocator_t) *const this, size_t size) 38 48 { 39 49 realloc(&this>storage, this>size+1); … … 42 52 // 43 53 //Element access 44 forall( type T,type allocator_t  allocator_c(T, allocator_t))45 T at(vector(T, allocator_t)*this, size_t index)54 forall(otype T, otype allocator_t  allocator_c(T, allocator_t)) 55 inline T at(vector(T, allocator_t) *const this, size_t index) 46 56 { 47 // assert(index < this>size);57 // assert(index < this>size); 48 58 return data(&this>storage)[index]; 49 59 } 50 60 51 forall( type T,type allocator_t  allocator_c(T, allocator_t))52 T ?[?](vector(T, allocator_t)*this, size_t index)61 forall(otype T, otype allocator_t  allocator_c(T, allocator_t)) 62 inline T ?[?](vector(T, allocator_t) *const this, size_t index) 53 63 { 54 64 return data(&this>storage)[index]; 55 65 } 56 66 57 forall( type T,type allocator_t  allocator_c(T, allocator_t))58 T front(vector(T, allocator_t)*this)67 forall(otype T, otype allocator_t  allocator_c(T, allocator_t)) 68 inline T front(vector(T, allocator_t) *const this) 59 69 { 60 70 return data(&this>storage)[0]; 61 71 } 62 72 63 forall( type T,type allocator_t  allocator_c(T, allocator_t))64 T back(vector(T, allocator_t)*this, size_t index)73 forall(otype T, otype allocator_t  allocator_c(T, allocator_t)) 74 inline T back(vector(T, allocator_t) *const this, size_t index) 65 75 { 66 76 return data(&this>storage)[this>size  1]; … … 69 79 // 70 80 //Modifiers 71 forall(type T, type allocator_t  allocator_c(T, allocator_t)) 72 void push_back(vector(T, allocator_t)* this, T value) 73 { 74 realloc(&this>storage, this>size+1); 75 data(&this>storage)[this>size] = value; 76 this>size++; 77 } 81 forall(otype T, otype allocator_t  allocator_c(T, allocator_t)) 82 void push_back(vector(T, allocator_t) *const this, T value); 78 83 79 forall(type T, type allocator_t  allocator_c(T, allocator_t)) 80 void pop_back(vector(T, allocator_t)* this) 81 { 82 this>size; 83 DESTROY(data(&this>storage)[this>size]); 84 } 84 forall(otype T, otype allocator_t  allocator_c(T, allocator_t)) 85 void pop_back(vector(T, allocator_t) *const this); 85 86 86 forall(type T, type allocator_t  allocator_c(T, allocator_t)) 87 void clear(vector(T, allocator_t)* this) 88 { 89 for(size_t i = 0; i < this>size; i++) 90 { 91 DESTROY(data(&this>storage)[this>size]); 92 } 93 this>size = 0; 94 } 87 forall(otype T, otype allocator_t  allocator_c(T, allocator_t)) 88 void clear(vector(T, allocator_t) *const this); 95 89 96 90 // 97 91 //Allocator 98 forall( type T)92 forall(otype T) 99 93 struct heap_allocator 100 94 { … … 103 97 }; 104 98 105 forall(type T) 106 void realloc(heap_allocator(T)* this, size_t size) 107 { 108 static const size_t GROWTH_RATE = 2; 109 if(size > this>capacity) 110 { 111 this>capacity = GROWTH_RATE * size; 112 this>storage = (T*)realloc((void*)this>storage, this>capacity * sizeof(T)); 113 } 114 } 99 forall(otype T) 100 void ctor(heap_allocator(T) *const this); 115 101 116 forall(type T) 117 T* data(heap_allocator(T)* this) 102 forall(otype T) 103 void dtor(heap_allocator(T) *const this); 104 105 forall(otype T) 106 void realloc(heap_allocator(T) *const this, size_t size); 107 108 forall(otype T) 109 inline T* data(heap_allocator(T) *const this) 118 110 { 119 111 return this>storage;
Note: See TracChangeset
for help on using the changeset viewer.