PLearn 0.1
ArrayAllocatorTrivial.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // ArrayAllocatorTrivial.h
00004 //
00005 // Copyright (C) 2000 Nicolas Chapados
00006 // 
00007 // Redistribution and use in source and binary forms, with or without
00008 //  modification, are permitted provided that the following conditions are met:
00009 // 
00010 //    1. Redistributions of source code must retain the above copyright
00011 //       notice, this list of conditions and the following disclaimer.
00012 //     
00013 //    2. Redistributions in binary form must reproduce the above copyright
00014 //       notice, this list of conditions and the following disclaimer in the
00015 //       documentation and/or other materials provided with the distribution.
00016 //       
00017 //    3. The name of the authors may not be used to endorse or promote
00018 //       products derived from this software without specific prior written
00019 //      permission. 
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 //
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 
00036 /* *******************************************************
00037  * * $Id: ArrayAllocatorTrivial.h 3994 2005-08-25 13:35:03Z chapados $
00038  * ******************************************************* */
00039 
00040 
00041 #ifndef ARRAYALLOCATORTRIVIAL_H
00042 #define ARRAYALLOCATORTRIVIAL_H
00043 
00044 
00045 #include <vector>
00046 #include <algorithm>     
00047 #include "general.h"
00048 #include "ArrayAllocatorIndex.h"
00049 
00050 namespace PLearn {
00051 using namespace std;
00052 
00053 
00054 
00055 //######################  CLASS  ARRAYALLOCATORTRIVIAL  #######################
00058 
00059 template <class T, unsigned SizeBits>
00060 class ArrayAllocatorTrivial
00061 {
00062 public:
00063     typedef ArrayAllocatorTrivial<T,SizeBits> self;
00064     typedef T value_type;
00065     typedef size_t size_type;
00066     typedef ptrdiff_t difference_type;
00067 
00068     typedef T* pointer;
00069     typedef const T* const_pointer;
00070     typedef T& reference;
00071     typedef const T& const_reference;
00072 
00073     typedef unsigned index_base;
00074     typedef ArrayAllocatorIndex<index_base, SizeBits> index_type;
00075 
00076 public:
00077     ArrayAllocatorTrivial(unsigned numberObjects);
00079 
00081     pointer allocate(size_t n);
00082 
00084     void deallocate(pointer p, size_type n) {}
00085     void deallocate(index_type) {}
00086 
00088     void resize(size_type newMaxObjs);
00089     size_type max_size() const {
00090         return arr.size();
00091     }
00092 
00094     void swap(self&);
00095 
00096 public:
00098     inline index_type toIndex(pointer p, size_type n);
00099     inline pointer toPointer(index_type);
00100     
00101 private:
00102     vector<T> arr;
00103     index_base free_point;
00104 };
00105 
00106 
00107 
00108 //#####  Implementation  ######################################################
00109 
00110 template <class T, unsigned SizeBits>
00111 ArrayAllocatorTrivial<T,SizeBits>::ArrayAllocatorTrivial(unsigned numberObjects)
00112     : arr(numberObjects), free_point(1)
00113 {}
00114 
00115 
00116 template <class T, unsigned SizeBits>
00117 void ArrayAllocatorTrivial<T,SizeBits>::resize(size_type newMaxObjs)
00118 {
00119     arr.resize(newMaxObjs);
00120     free_point = max(1, min(free_point, arr.size()));
00121 }
00122 
00123 
00124 template <class T, unsigned SizeBits>
00125 T* ArrayAllocatorTrivial<T,SizeBits>::allocate(size_t n)
00126 {
00127     if(free_point + n > arr.size()) {
00128         PLERROR("Size of memory pool exceeded (%d objects)", arr.size());
00129         return 0;
00130     }
00131     T* p = &arr[free_point];
00132     free_point += n;
00133     return p;
00134 }
00135 
00136 
00137 template <class T, unsigned SizeBits>
00138 void ArrayAllocatorTrivial<T,SizeBits>::swap(self& other)
00139 {
00140     arr.swap(other);
00141     swap(free_point, other.free_point);
00142 }
00143 
00144 
00145 template <class T, unsigned SizeBits>
00146 inline
00147 typename ArrayAllocatorTrivial<T,SizeBits>::index_type
00148 ArrayAllocatorTrivial<T,SizeBits>::toIndex(pointer p, size_type n)
00149 {
00150     if (p)
00151         return index_type(p-&arr[0], n);
00152     else
00153         return index_type(0,0);
00154 }
00155 
00156 
00157 template <class T, unsigned SizeBits>
00158 inline
00159 typename ArrayAllocatorTrivial<T,SizeBits>::pointer
00160 ArrayAllocatorTrivial<T,SizeBits>::toPointer(index_type i)
00161 {
00162     if (i.isNull())
00163         return 0;
00164     else
00165         return &arr[0] + i.index;
00166 }
00167 
00168 } // end of namespace PLearn
00169 
00170 
00171 #endif
00172 
00173 
00174 /*
00175   Local Variables:
00176   mode:c++
00177   c-basic-offset:4
00178   c-file-style:"stroustrup"
00179   c-file-offsets:((innamespace . 0)(inline-open . 0))
00180   indent-tabs-mode:nil
00181   fill-column:79
00182   End:
00183 */
00184 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines