/usr/include/dynamicArray.h is in libsnl-dev 0.2.1.svn.18-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | // Copyright 2005 Scott A.E. Lanham, Australia.
// --------------------------------------------
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Library General Public License for more details.
// *** Dynamically Allocated Paged Array ***
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
#define DYNAMICARRAY_DEFAULT_PAGE_SIZE 512
template < class T > class dynamicArray
{
public:
virtual ~dynamicArray();
dynamicArray();
dynamicArray ( const dynamicArray<T>& arrayToCopy );
dynamicArray ( int pageSize );
dynamicArray<T>& operator= ( const dynamicArray<T>& arrayToCopy );
void copyFrom ( const dynamicArray<T>& arrayToCopy );
int pageSize();
T* copyOfElements();
int sizeAllocated();
int maxIndex();
T& operator[] ( int index );
protected:
void grow ( int bySize );
private:
int page_size;
T** array_pages;
int num_pages;
int array_size;
int max_index; // Maximum index number that has been used.
T null_element;
};
template < class T > dynamicArray<T>::~dynamicArray()
{
if ( array_pages )
{
for ( int pageIndex = 0; pageIndex < num_pages; pageIndex ++ )
delete[] array_pages [ pageIndex ];
}
}
template < class T > dynamicArray<T>::dynamicArray()
{
page_size = DYNAMICARRAY_DEFAULT_PAGE_SIZE;
num_pages = 0;
array_pages = 0;
array_size = 0;
max_index = 0;
}
template < class T > dynamicArray<T>::dynamicArray ( const dynamicArray<T>& arrayToCopy )
{
// Copy constructor.
// -----------------
copyFrom ( arrayToCopy );
}
template < class T > dynamicArray<T>::dynamicArray ( int pageSize )
{
// Constructor.
// ------------
// initialSize: Initial number of elements in array.
// pageSize: Granularity of array size increase. Size is number of elements NOT bytes.
page_size = pageSize;
num_pages = 0;
array_pages = 0;
array_size = 0;
max_index = 0;
}
template < class T > dynamicArray<T>& dynamicArray<T>::operator= ( const dynamicArray<T>& arrayToCopy )
{
// Assignment operator.
// --------------------
if ( array_pages )
{
for ( int pageIndex = 0; pageIndex < num_pages; pageIndex ++ )
delete[] array_pages [ pageIndex ];
}
copyFrom ( arrayToCopy );
}
template < class T > void dynamicArray<T>::copyFrom ( const dynamicArray<T>& arrayToCopy )
{
// Copy contents of another dynamic array into this.
// -------------------------------------------------
// Notes: Type <T> must have an assignment operator that doesn't
// fuck things up.
page_size = arrayToCopy.page_size;
num_pages = arrayToCopy.num_pages;
array_size = arrayToCopy.array_size;
max_index = arrayToCopy.max_index;
if ( ! arrayToCopy.array_pages )
{
num_pages = 0;
array_size = 0;
max_index = 0;
array_pages = 0;
}
else
{
array_pages = new T* [ num_pages ];
for ( int page = 0; page < num_pages; page ++ )
{
T* pageToCopy = arrayToCopy.array_pages [ page ];
T* newPage = new T [ page_size ];
array_pages [ page ] = newPage;
for ( int index = 0; index < page_size; index ++ )
newPage [ index ] = pageToCopy [ index ];
}
}
}
template < class T > int dynamicArray<T>::pageSize()
{
return page_size;
}
template < class T > T* dynamicArray<T>::copyOfElements()
{
// Return pointer to copy of array elements.
// -----------------------------------------
// Notes: Caller owns returned array.
if ( ! num_pages ) return 0;
int arraySize = array_size;
T* retArray = new T [ arraySize ];
int retArrayIndex = 0;
for ( int page = 0; page < num_pages; page ++ )
{
T* currentPage = array_pages [ page ];
for ( int index = 0; index < page_size; index ++ )
retArray [ retArrayIndex ++ ] = currentPage [ index ];
}
return retArray;
}
template < class T > int dynamicArray<T>::sizeAllocated()
{
// Return size of all allocated elements.
// --------------------------------------
array_size = num_pages * page_size;
return array_size;
}
template < class T > int dynamicArray<T>::maxIndex()
{
// Return maximum index number that has been referenced.
// -----------------------------------------------------
return max_index;
}
template < class T > void dynamicArray<T>::grow ( int bySize )
{
// Grow array.
// -----------
// bySize: Grow by this many elements.
//
// Notes: Will only increase size to page boundaries.
int growNumPages = bySize / page_size;
if ( bySize % page_size > 0 )
growNumPages ++;
// Grow pages array.
T** newPageArray = new T* [ num_pages + growNumPages ];
if ( array_pages )
{
for ( int pageIndex = 0; pageIndex < num_pages; pageIndex ++ )
newPageArray [ pageIndex ] = array_pages [ pageIndex ];
delete[] array_pages;
}
// Populate pages array with new pages.
for ( int pageIndex = 0; pageIndex < growNumPages; pageIndex ++ )
{
T* newPage = new T [ page_size ];
newPageArray [ num_pages + pageIndex ] = newPage;
}
array_pages = newPageArray;
num_pages += growNumPages;
array_size = num_pages * page_size;
}
template < class T > T& dynamicArray<T>::operator[] ( int index )
{
// Return reference to array element at index.
// -------------------------------------------
// index: Array index.
if ( index < 0 ) return null_element; // No negative indices allowed.
if ( ! array_pages || index >= array_size )
grow ( index - array_size + 1 );
if ( index > max_index ) max_index = index;
int pageIndex = index / page_size;
int elementIndex = index % page_size;
return array_pages [ pageIndex ] [ elementIndex ];
}
#endif
|