Mercurial > vba-linux
diff src/win32/7zip/7z/CPP/Common/MyVector.h @ 1:f9f4f1b99eed
importing src directory
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 10:31:27 -0600 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/win32/7zip/7z/CPP/Common/MyVector.h Sat Mar 03 10:31:27 2012 -0600 1.3 @@ -0,0 +1,256 @@ 1.4 +// Common/Vector.h 1.5 + 1.6 +#ifndef __COMMON_VECTOR_H 1.7 +#define __COMMON_VECTOR_H 1.8 + 1.9 +#include "Defs.h" 1.10 + 1.11 +class CBaseRecordVector 1.12 +{ 1.13 + void MoveItems(int destIndex, int srcIndex); 1.14 +protected: 1.15 + int _capacity; 1.16 + int _size; 1.17 + void *_items; 1.18 + size_t _itemSize; 1.19 + 1.20 + void ReserveOnePosition(); 1.21 + void InsertOneItem(int index); 1.22 + void TestIndexAndCorrectNum(int index, int &num) const 1.23 + { if (index + num > _size) num = _size - index; } 1.24 +public: 1.25 + CBaseRecordVector(size_t itemSize): _capacity(0), _size(0), _items(0), _itemSize(itemSize) {} 1.26 + virtual ~CBaseRecordVector(); 1.27 + void ClearAndFree(); 1.28 + int Size() const { return _size; } 1.29 + bool IsEmpty() const { return (_size == 0); } 1.30 + void Reserve(int newCapacity); 1.31 + void ReserveDown(); 1.32 + virtual void Delete(int index, int num = 1); 1.33 + void Clear(); 1.34 + void DeleteFrom(int index); 1.35 + void DeleteBack(); 1.36 +}; 1.37 + 1.38 +template <class T> 1.39 +class CRecordVector: public CBaseRecordVector 1.40 +{ 1.41 +public: 1.42 + CRecordVector(): CBaseRecordVector(sizeof(T)){}; 1.43 + CRecordVector(const CRecordVector &v): CBaseRecordVector(sizeof(T)) { *this = v; } 1.44 + CRecordVector& operator=(const CRecordVector &v) 1.45 + { 1.46 + Clear(); 1.47 + return (*this += v); 1.48 + } 1.49 + CRecordVector& operator+=(const CRecordVector &v) 1.50 + { 1.51 + int size = v.Size(); 1.52 + Reserve(Size() + size); 1.53 + for (int i = 0; i < size; i++) 1.54 + Add(v[i]); 1.55 + return *this; 1.56 + } 1.57 + int Add(T item) 1.58 + { 1.59 + ReserveOnePosition(); 1.60 + ((T *)_items)[_size] = item; 1.61 + return _size++; 1.62 + } 1.63 + void Insert(int index, T item) 1.64 + { 1.65 + InsertOneItem(index); 1.66 + ((T *)_items)[index] = item; 1.67 + } 1.68 + // T* GetPointer() const { return (T*)_items; } 1.69 + // operator const T *() const { return _items; }; 1.70 + const T& operator[](int index) const { return ((T *)_items)[index]; } 1.71 + T& operator[](int index) { return ((T *)_items)[index]; } 1.72 + const T& Front() const { return operator[](0); } 1.73 + T& Front() { return operator[](0); } 1.74 + const T& Back() const { return operator[](_size - 1); } 1.75 + T& Back() { return operator[](_size - 1); } 1.76 + 1.77 + void Swap(int i, int j) 1.78 + { 1.79 + T temp = operator[](i); 1.80 + operator[](i) = operator[](j); 1.81 + operator[](j) = temp; 1.82 + } 1.83 + 1.84 + int FindInSorted(const T& item) const 1.85 + { 1.86 + int left = 0, right = Size(); 1.87 + while (left != right) 1.88 + { 1.89 + int mid = (left + right) / 2; 1.90 + const T& midValue = (*this)[mid]; 1.91 + if (item == midValue) 1.92 + return mid; 1.93 + if (item < midValue) 1.94 + right = mid; 1.95 + else 1.96 + left = mid + 1; 1.97 + } 1.98 + return -1; 1.99 + } 1.100 + 1.101 + int AddToUniqueSorted(const T& item) 1.102 + { 1.103 + int left = 0, right = Size(); 1.104 + while (left != right) 1.105 + { 1.106 + int mid = (left + right) / 2; 1.107 + const T& midValue = (*this)[mid]; 1.108 + if (item == midValue) 1.109 + return mid; 1.110 + if (item < midValue) 1.111 + right = mid; 1.112 + else 1.113 + left = mid + 1; 1.114 + } 1.115 + Insert(right, item); 1.116 + return right; 1.117 + } 1.118 + 1.119 + static void SortRefDown(T* p, int k, int size, int (*compare)(const T*, const T*, void *), void *param) 1.120 + { 1.121 + T temp = p[k]; 1.122 + for (;;) 1.123 + { 1.124 + int s = (k << 1); 1.125 + if (s > size) 1.126 + break; 1.127 + if (s < size && compare(p + s + 1, p + s, param) > 0) 1.128 + s++; 1.129 + if (compare(&temp, p + s, param) >= 0) 1.130 + break; 1.131 + p[k] = p[s]; 1.132 + k = s; 1.133 + } 1.134 + p[k] = temp; 1.135 + } 1.136 + 1.137 + void Sort(int (*compare)(const T*, const T*, void *), void *param) 1.138 + { 1.139 + int size = _size; 1.140 + if (size <= 1) 1.141 + return; 1.142 + T* p = (&Front()) - 1; 1.143 + { 1.144 + int i = size / 2; 1.145 + do 1.146 + SortRefDown(p, i, size, compare, param); 1.147 + while (--i != 0); 1.148 + } 1.149 + do 1.150 + { 1.151 + T temp = p[size]; 1.152 + p[size--] = p[1]; 1.153 + p[1] = temp; 1.154 + SortRefDown(p, 1, size, compare, param); 1.155 + } 1.156 + while (size > 1); 1.157 + } 1.158 +}; 1.159 + 1.160 +typedef CRecordVector<int> CIntVector; 1.161 +typedef CRecordVector<unsigned int> CUIntVector; 1.162 +typedef CRecordVector<bool> CBoolVector; 1.163 +typedef CRecordVector<unsigned char> CByteVector; 1.164 +typedef CRecordVector<void *> CPointerVector; 1.165 + 1.166 +template <class T> 1.167 +class CObjectVector: public CPointerVector 1.168 +{ 1.169 +public: 1.170 + CObjectVector() {}; 1.171 + ~CObjectVector() { Clear(); }; 1.172 + CObjectVector(const CObjectVector &v) { *this = v; } 1.173 + CObjectVector& operator=(const CObjectVector &v) 1.174 + { 1.175 + Clear(); 1.176 + return (*this += v); 1.177 + } 1.178 + CObjectVector& operator+=(const CObjectVector &v) 1.179 + { 1.180 + int size = v.Size(); 1.181 + Reserve(Size() + size); 1.182 + for (int i = 0; i < size; i++) 1.183 + Add(v[i]); 1.184 + return *this; 1.185 + } 1.186 + const T& operator[](int index) const 1.187 + { 1.188 + return *((T *)CPointerVector::operator[](index)); 1.189 + } 1.190 + T& operator[](int index) 1.191 + { 1.192 + return *((T *)CPointerVector::operator[](index)); 1.193 + } 1.194 + T& Front() { return operator[](0); } 1.195 + const T& Front() const { return operator[](0); } 1.196 + T& Back() { return operator[](_size - 1); } 1.197 + const T& Back() const { return operator[](_size - 1); } 1.198 + int Add(const T& item) { return CPointerVector::Add(new T(item)); } 1.199 + void Insert(int index, const T& item) { CPointerVector::Insert(index, new T(item)); } 1.200 + virtual void Delete(int index, int num = 1) 1.201 + { 1.202 + TestIndexAndCorrectNum(index, num); 1.203 + for (int i = 0; i < num; i++) 1.204 + delete (T *)(((void **)_items)[index + i]); 1.205 + CPointerVector::Delete(index, num); 1.206 + } 1.207 + int Find(const T& item) const 1.208 + { 1.209 + for (int i = 0; i < Size(); i++) 1.210 + if (item == (*this)[i]) 1.211 + return i; 1.212 + return -1; 1.213 + } 1.214 + int FindInSorted(const T& item) const 1.215 + { 1.216 + int left = 0, right = Size(); 1.217 + while (left != right) 1.218 + { 1.219 + int mid = (left + right) / 2; 1.220 + const T& midValue = (*this)[mid]; 1.221 + if (item == midValue) 1.222 + return mid; 1.223 + if (item < midValue) 1.224 + right = mid; 1.225 + else 1.226 + left = mid + 1; 1.227 + } 1.228 + return -1; 1.229 + } 1.230 + int AddToSorted(const T& item) 1.231 + { 1.232 + int left = 0, right = Size(); 1.233 + while (left != right) 1.234 + { 1.235 + int mid = (left + right) / 2; 1.236 + const T& midValue = (*this)[mid]; 1.237 + if (item == midValue) 1.238 + { 1.239 + right = mid + 1; 1.240 + break; 1.241 + } 1.242 + if (item < midValue) 1.243 + right = mid; 1.244 + else 1.245 + left = mid + 1; 1.246 + } 1.247 + Insert(right, item); 1.248 + return right; 1.249 + } 1.250 + 1.251 + void Sort(int (*compare)(void *const *, void *const *, void *), void *param) 1.252 + { CPointerVector::Sort(compare, param); } 1.253 + 1.254 + static int CompareObjectItems(void *const *a1, void *const *a2, void * /* param */) 1.255 + { return MyCompare(*(*((const T **)a1)), *(*((const T **)a2))); } 1.256 + void Sort() { CPointerVector::Sort(CompareObjectItems, 0); } 1.257 +}; 1.258 + 1.259 +#endif