# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

path_mesh_alloc.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #ifndef NL_PATH_MESH_ALLOC_H
00027 #define NL_PATH_MESH_ALLOC_H
00028 
00029 #include "nel/misc/types_nl.h"
00030 #include <vector>
00031 #include <list>
00032 
00033 template <class T>
00034 class CArrayElement
00035 {
00036 public:
00037         CArrayElement (uint defaultSize)
00038         {
00039                 _Array.reserve (defaultSize);
00040                 _Allocated=false;
00041         }
00042         bool                                                                    _Allocated;
00043         std::vector<T>                                                  _Array;
00044 };
00045 
00052 template <class T>
00053 class CPathMeshAlloc
00054 {
00055 public:
00056 
00058         CPathMeshAlloc(uint defaultSize)
00059         {
00060                 _DefaultSize=defaultSize;
00061                 _BlockAllocated=0;
00062         }
00063 
00064         // Allocate a vector
00065         std::vector<T>* allocate ()
00066         {
00067                 _BlockAllocated++;
00068 
00069                 // Some trace
00070                 nldebug ("Allocate, %d blocks %d max\n", _BlockAllocated, _ArrayList.size());
00071 
00072                 // Look for a free element
00073                 ListArray::iterator     ite=_ArrayList.begin();
00074 
00075                 // for each element
00076                 while (ite!=_ArrayList.end())
00077                 {
00078                         // Find one ?
00079                         if (!ite->get()->_Allocated)
00080                         {
00081                                 ite->get()->_Allocated=true;
00082                                 return &ite->get()->_Array;
00083                         }
00084                         ite++;
00085                 }
00086 
00087                 // ** Not find, add an entry
00088                 
00089                 // Create
00090                 CArrayElement<T>        *pElement=new CArrayElement<T> (_DefaultSize);
00091 
00092                 // Push back the enrty
00093                 _ArrayList.push_back (std::auto_ptr<CArrayElement<T> > (pElement));
00094                 ite=_ArrayList.end();
00095                 ite--;
00096                 ite->get()->_Allocated=true;
00097                 return &ite->get()->_Array;
00098         }
00099 
00100         // Free a vector
00101         void free (std::vector<T>* ptr)
00102         {
00103                 _BlockAllocated--;
00104 
00105                 // Some trace
00106                 nldebug ("Allocate, %d blocks %d max\n", _BlockAllocated, _ArrayList.size());
00107 
00108                 // Look for the good array
00109                 ListArray::iterator     ite=_ArrayList.begin();
00110 
00111                 // for each element
00112                 while (ite!=_ArrayList.end())
00113                 {
00114                         // Find one ?
00115                         if (&ite->get()->_Array==ptr)
00116                         {
00117                                 ite->get()->_Allocated=false;
00118                                 return;
00119                         }
00120                         ite++;
00121                 }
00122 
00123                 // No, should be somewhere
00124                 nlassert (0);           // no!
00125         }
00126 
00127 private:
00128         // Typedef 
00129 typedef std::list< std::auto_ptr<CArrayElement<T> > > ListArray;
00130 
00131         uint                                                                            _DefaultSize;
00132         uint                                                                            _BlockAllocated;
00133         ListArray                                                                       _ArrayList;
00134 };
00135 
00136 #endif // NL_PATH_MESH_ALLOC_H
00137 
00138 /* End of path_mesh_alloc.h */