00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
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
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
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 }
00169
00170
00171
#endif