diff options
| author | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
|---|---|---|
| committer | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
| commit | 5df79c53745fde5d6c3340a2979b1429cd5892c1 (patch) | |
| tree | 1a81af141708b826e9c61e8a04019994fcca8298 /src/mt/mvec.h | |
Initial import of htcd system 1.0
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/mt/mvec.h')
| -rw-r--r-- | src/mt/mvec.h | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/src/mt/mvec.h b/src/mt/mvec.h new file mode 100644 index 0000000..9f31c1e --- /dev/null +++ b/src/mt/mvec.h | |||
| @@ -0,0 +1,149 @@ | |||
| 1 | /************************************************************************* | ||
| 2 | * | ||
| 3 | * HTCd - Copyright (C) 1998-2006 Henrik Rydberg | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation; either version 2 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #ifndef MVECH | ||
| 21 | #define MVECH | ||
| 22 | |||
| 23 | #include <malloc.h> | ||
| 24 | #include <string.h> | ||
| 25 | #include <new> | ||
| 26 | |||
| 27 | const int mv_mincaps=16; | ||
| 28 | |||
| 29 | template <class T> class mrvec { | ||
| 30 | public: | ||
| 31 | typedef T* iterator; | ||
| 32 | typedef const T* const_iterator; | ||
| 33 | |||
| 34 | mrvec(): | ||
| 35 | map((T*)malloc(mv_mincaps*sizeof(T))),dim(0),caps(mv_mincaps) {} | ||
| 36 | mrvec(int n): | ||
| 37 | map((T*)malloc(mv_mincaps*sizeof(T))),dim(0),caps(mv_mincaps) { resize(n); } | ||
| 38 | ~mrvec() { free(map); } | ||
| 39 | |||
| 40 | int size() const { return dim; } | ||
| 41 | int capacity() const { return caps; } | ||
| 42 | |||
| 43 | bool empty() const { return dim==0; } | ||
| 44 | bool nempty() const { return dim!=0; } | ||
| 45 | |||
| 46 | T& at(int pos) { return map[pos]; } | ||
| 47 | const T& at(int pos) const { return map[pos]; } | ||
| 48 | |||
| 49 | T& operator[](int pos) { return map[pos]; } | ||
| 50 | const T& operator[](int pos) const { return map[pos]; } | ||
| 51 | |||
| 52 | iterator begin() { return map; } | ||
| 53 | const_iterator begin() const { return map; } | ||
| 54 | |||
| 55 | iterator end() { return map+dim; } | ||
| 56 | const_iterator end() const { return map+dim; } | ||
| 57 | |||
| 58 | T& front() { return map[0]; } | ||
| 59 | const T& front() const { return map[0]; } | ||
| 60 | |||
| 61 | T& back() { return map[dim?dim-1:0]; } | ||
| 62 | const T& back() const { return map[dim?dim-1:0]; } | ||
| 63 | |||
| 64 | //////////////////////////////////////////////////////////////// | ||
| 65 | |||
| 66 | void clear() { dim=0; } | ||
| 67 | |||
| 68 | iterator insert(iterator it,const T& s) { | ||
| 69 | int pos=it-map; ins(pos,1); return &(map[pos]=s); | ||
| 70 | } | ||
| 71 | void insert(iterator it,int n,const T& s) { | ||
| 72 | int pos=it-map; ins(pos,n); while(n--) map[pos++]=s; | ||
| 73 | } | ||
| 74 | void insert(iterator it,const_iterator p,const_iterator e) { | ||
| 75 | int pos=it-map; ins(pos,e-p); while(p<e) map[pos++]=*p++; | ||
| 76 | } | ||
| 77 | iterator erase(iterator it) { | ||
| 78 | int pos=it-map; ins(pos+1,-1); return map+pos; | ||
| 79 | } | ||
| 80 | |||
| 81 | void push_back(const T& s) { reserve(dim+1); map[dim++]=s; } | ||
| 82 | void pop_back(const T& s) { if(dim) --dim; } | ||
| 83 | |||
| 84 | //////////////////////////////////////////////////////////////// | ||
| 85 | |||
| 86 | void reserve(int n) { | ||
| 87 | if(caps<n) { while(caps<n) caps<<=1; map=(T*)realloc(map,caps*sizeof(T)); } | ||
| 88 | } | ||
| 89 | void resize(int n) { reserve(n); dim=n; } | ||
| 90 | |||
| 91 | //////////////////////////////////////////////////////////////// | ||
| 92 | |||
| 93 | protected: | ||
| 94 | mrvec(const mrvec&) {} | ||
| 95 | void operator=(const mrvec&) {} | ||
| 96 | |||
| 97 | void ins(int pos,int n) { | ||
| 98 | if(n) { reserve(dim+n); memmove(map+pos+n,map+pos,(dim-pos)*sizeof(T)); dim+=n; } | ||
| 99 | } | ||
| 100 | |||
| 101 | T* map; | ||
| 102 | int dim,caps; | ||
| 103 | }; | ||
| 104 | |||
| 105 | template <class T> class msvec : public mrvec<T> { | ||
| 106 | public: | ||
| 107 | using mrvec<T>::map; | ||
| 108 | using mrvec<T>::dim; | ||
| 109 | typedef typename mrvec<T>::iterator iterator; | ||
| 110 | typedef typename mrvec<T>::const_iterator const_iterator; | ||
| 111 | |||
| 112 | msvec() : mrvec<T>() {} | ||
| 113 | msvec(int n) : mrvec<T>(n) { for(int i=0;i<dim;i++) new (map+i) T(); } | ||
| 114 | ~msvec() { while(dim) map[--dim].~T(); } | ||
| 115 | |||
| 116 | //////////////////////////////////////////////////////////////// | ||
| 117 | |||
| 118 | void clear() { while(dim) map[--dim].~T(); } | ||
| 119 | |||
| 120 | iterator insert(iterator it,const T& s) { | ||
| 121 | int pos=it-map; mrvec<T>::ins(pos,1); return new(map+pos) T(s); | ||
| 122 | } | ||
| 123 | void insert(iterator it,int n,const T& s) { | ||
| 124 | int pos=it-map; mrvec<T>::ins(pos,n); while(n--) new(map+pos++) T(s); | ||
| 125 | } | ||
| 126 | void insert(iterator it,const_iterator p,const_iterator e) { | ||
| 127 | int pos=it-map; mrvec<T>::ins(pos,e-p); while(p<e) new(map+pos++) T(*p++); | ||
| 128 | } | ||
| 129 | iterator erase(iterator it) { | ||
| 130 | it->~T(); int pos=it-map; mrvec<T>::ins(pos+1,-1); return map+pos; | ||
| 131 | } | ||
| 132 | |||
| 133 | void push_back(const T& s) { reserve(dim+1); new(map+dim++) T(s); } | ||
| 134 | void pop_back(const T& s) { if(dim) map[--dim].~T(); } | ||
| 135 | |||
| 136 | //////////////////////////////////////////////////////////////// | ||
| 137 | |||
| 138 | void resize(int n) { | ||
| 139 | while(dim>n) map[--dim].~T(); | ||
| 140 | mrvec<T>::reserve(n); | ||
| 141 | while(dim<n) new (map+dim++) T(); | ||
| 142 | } | ||
| 143 | |||
| 144 | protected: | ||
| 145 | msvec(const msvec&) {} | ||
| 146 | void operator=(const msvec&) {} | ||
| 147 | }; | ||
| 148 | |||
| 149 | #endif | ||
