| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2005-2015 Dmitry Tsarkov and The University of Manchester |
| 3 | Copyright (C) 2015-2017 Dmitry Tsarkov |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library 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 GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #ifndef GROWINGARRAYP_H |
| 21 | #define GROWINGARRAYP_H |
| 22 | |
| 23 | #include <vector> |
| 24 | |
| 25 | using std::size_t; |
| 26 | |
| 27 | /** |
| 28 | * Generic class for structures which creates elements (by pointers) and re-use |
| 29 | * them (does not delete things). Derived types may add operations. |
| 30 | */ |
| 31 | template <typename T> |
| 32 | class growingArrayP |
| 33 | { |
| 34 | protected: // typedefs |
| 35 | /// type of the heap |
| 36 | typedef std::vector<T*> baseType; |
| 37 | |
| 38 | public: // typedefs |
| 39 | /// heap's iterator |
| 40 | typedef typename baseType::iterator iterator; |
| 41 | |
| 42 | protected: // members |
| 43 | /// heap itself |
| 44 | baseType Base; |
| 45 | /// index of the next unallocated entry |
| 46 | size_t last = 0; |
| 47 | |
| 48 | protected: // methods |
| 49 | /// tunable method for creating new object |
| 50 | virtual T* createNew ( void ) { return new T; } |
| 51 | /// init vector [B,E) with new objects T |
| 52 | void initArray ( iterator b, iterator e ) |
| 53 | { |
| 54 | for ( iterator p = b; p != e; ++p ) |
| 55 | *p = createNew(); |
| 56 | } |
| 57 | /// increase heap size |
| 58 | void grow ( void ) |
| 59 | { |
| 60 | size_t size = Base.size(); |
| 61 | Base.resize(size?size*2:1); |
| 62 | initArray ( Base.begin()+(long)size, Base.end() ); |
| 63 | } |
| 64 | /// ensure that size of vector is enough to fit the last element |
| 65 | void ensureHeapSize ( void ) |
| 66 | { |
| 67 | if ( last >= Base.size() ) |
| 68 | grow(); |
| 69 | } |
| 70 | /// ensure that size of vector is enough to keep N elements |
| 71 | void ensureHeapSize ( size_t n ) |
| 72 | { |
| 73 | while ( n >= Base.size() ) |
| 74 | grow(); |
| 75 | } |
| 76 | |
| 77 | public: // interface |
| 78 | /// c'tor: make SIZE objects |
| 79 | explicit growingArrayP ( size_t size = 0 ) : Base(size) |
| 80 | { |
| 81 | initArray ( Base.begin(), Base.end() ); |
| 82 | } |
| 83 | /// no copy c'tor |
| 84 | growingArrayP ( const growingArrayP& ) = delete; |
| 85 | /// no assignment |
| 86 | growingArrayP& operator= ( const growingArrayP& ) = delete; |
| 87 | /// d'tor: delete all allocated objects |
| 88 | virtual ~growingArrayP() |
| 89 | { |
| 90 | for ( auto p = Base.rbegin(), p_end = Base.rend(); p != p_end; ++p ) |
| 91 | delete *p; |
| 92 | } |
| 93 | |
| 94 | /// resize an array |
| 95 | void resize ( size_t n ) { ensureHeapSize(n); last = n; } |
| 96 | /// get the number of elements |
| 97 | size_t size ( void ) const { return last; } |
| 98 | /// check if heap is empty |
| 99 | bool empty ( void ) const { return last == 0; } |
| 100 | /// mark all array elements as unused |
| 101 | virtual void clear ( void ) { last = 0; } |
| 102 | }; // growingArrayP |
| 103 | |
| 104 | #endif |