QGpCoreTools/IncreaseStorage.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of QGpCoreTools.
00004 **
00005 **  This library is free software; you can redistribute it and/or
00006 **  modify it under the terms of the GNU Lesser General Public
00007 **  License as published by the Free Software Foundation; either
00008 **  version 2.1 of the License, or (at your option) any later version.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00013 **  License for more details.
00014 **
00015 **  You should have received a copy of the GNU Lesser General Public
00016 **  License along with this library; if not, write to the Free Software
00017 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 **
00019 **  See http://www.geopsy.org for more information.
00020 **
00021 **  Created : 2009-05-13
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (LGIT, Grenoble, France)
00025 **
00026 ***************************************************************************/
00027 
00028 #ifndef INCREASESTORAGE_H
00029 #define INCREASESTORAGE_H
00030 
00031 #include <stdlib.h>
00032 
00033 #include "QGpCoreToolsDLLExport.h"
00034 
00035 namespace QGpCoreTools {
00036 
00037 class QGPCORETOOLS_EXPORT IncreaseStorage
00038 {
00039 public:
00040   inline IncreaseStorage(int defaultCapacity);
00041   inline IncreaseStorage(const IncreaseStorage& o);
00042   virtual ~IncreaseStorage() {}
00043 
00044   inline void reserve(int n);
00045   int size() const {return _size;}
00046   inline void add(int n=1);
00047   inline void clear();
00048   void downSize(int s) {_size=s;}
00049 protected:
00050   virtual void reallocate()=0;
00051   inline void * allocateVector(int itemSize);
00052   inline void * reallocateVector(void * vect, int itemSize);
00053 private:
00054   int _defaultCapacity;
00055   int _capacity;
00056   volatile int _size;
00057 };
00058 
00059 inline IncreaseStorage::IncreaseStorage(int defaultCapacity)
00060 {
00061   _defaultCapacity=defaultCapacity;
00062   clear();
00063 }
00064 
00065 inline IncreaseStorage::IncreaseStorage(const IncreaseStorage& o)
00066 {
00067   _defaultCapacity=o._defaultCapacity;
00068   _capacity=o._capacity;
00069   _size=o._size;
00070 }
00071 
00072 inline void * IncreaseStorage::allocateVector(int itemSize)
00073 {
00074   return malloc(_capacity * itemSize);
00075 }
00076 
00077 inline void * IncreaseStorage::reallocateVector(void * vect, int itemSize)
00078 {
00079   return realloc(vect, _capacity * itemSize);
00080 }
00081 
00082 inline void IncreaseStorage::reserve(int n)
00083 {
00084   if(_size+n >= _capacity) {
00085     while(_size+n >= _capacity) {
00086       _capacity=_capacity << 1;
00087     }
00088     reallocate();
00089   }
00090 }
00091 
00092 inline void IncreaseStorage::add(int n)
00093 {
00094   _size+=n;
00095   if(_size>=_capacity) {
00096     do {
00097       _capacity=_capacity << 1;
00098     } while(_size>=_capacity);
00099     reallocate();
00100   }
00101 }
00102 
00103 inline void IncreaseStorage::clear()
00104 {
00105   _capacity=_defaultCapacity;
00106   _size=0;
00107 }
00108 
00109 } // namespace QGpCoreTools
00110 
00111 #endif // INCREASESTORAGE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines