📄 vector.hpp
字号:
/********************************************************************************************MiraXT -- Copyright (c) 2007, Tobias Schubert, Matthew Lewis, Natalia Kalinnik, Bernd Becker Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies orsubstantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE ANDNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.********************************************************************************************/// Definition of automatically resizable arrays (vectors).template<class T> class vec { private: T* data; int sz; int cap; void grow(int min_cap, bool pushing); public: // Constructors / Destructor. vec(void) : data(NULL) , sz(0) , cap(0) { } vec(int size) : data(NULL) , sz(0) , cap(0) { growTo(size); } ~vec(void) { clear(true); } // Ownership of underlying array. operator T*(void) { return data; } // Operations. int size(void) const { return sz; } int memAlloc(void) const { return cap; } void pop(void) { sz--, data[sz].~T(); data[sz]=0; } void popto(int size) { while(sz > size) sz--, data[sz].~T(), data[sz]=0; } void getMem(int size) { grow(size, false); } void clearFast(void) { sz = 0; } int containsEven(T x) { int i = 0; while(i < sz && data[i] != x) i += 2; return i; } int containsOdd(T x) { int i = 1; while(i < sz && data[i] != x) i += 2; return i; } void growTo(int size); void growTo(int size, const T& pad); void clear(bool dealloc = false); inline void rem(int index) { sz--; data[index].~T(); data[index] = data[sz]; } inline void push(const T& elem) { if (sz == cap) grow(sz+1, true); new (&data[sz]) T(elem); sz++; } T& last(void) { return data[sz-1]; }};template<class T>void vec<T>::grow(int min_cap, bool pushing) { if (min_cap <= cap) { return; } cap = min_cap; if (cap < 4) { cap = 4; } if (pushing) { cap = cap + (cap >> 2); } cap = (cap + 4) & (0xFFFFFFFC); data = (T*) realloc((void*) data, cap * sizeof(T)); // Not enough physical memory to allocate the desired area? if (data == 0) { fprintf(stdout,"c Vector Reallocation Error: Not enough memory\n"); fprintf(stdout,"s UNKNOWN\n"); // Error code 0: UNKNOWN. exit(0); }} template<class T>void vec<T>::growTo(int size, const T& pad) { if (sz >= size) return; grow(size, false); for (int i=sz; i<size; i++) { new (&data[i]) T(pad); } sz = size; } template<class T>void vec<T>::growTo(int size) { if (sz >= size) { return; } grow(size, false); for (int i=sz; i<size; i++) { new (&data[i]) T(); } sz = size; } template<class T>inline void vec<T>::clear(bool dealloc) { if (data != NULL) { for (int i=0; i<sz; i++) { data[i].~T(); } sz = 0; if (dealloc) { if (data != NULL) free((void*) data); data = NULL; cap = 0; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -