00001 /* 00002 00003 Copyright (c) 2004, Cornell University 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions are met: 00008 00009 - Redistributions of source code must retain the above copyright notice, 00010 this list of conditions and the following disclaimer. 00011 - Redistributions in binary form must reproduce the above copyright 00012 notice, this list of conditions and the following disclaimer in the 00013 documentation and/or other materials provided with the distribution. 00014 - Neither the name of Cornell University nor the names of its 00015 contributors may be used to endorse or promote products derived from 00016 this software without specific prior written permission. 00017 00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00019 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00021 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00022 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00023 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00024 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00025 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00026 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 00028 THE POSSIBILITY OF SUCH DAMAGE. 00029 00030 */ 00031 00032 00033 ///////////////////////////////////////////////////////////////////// 00034 /// 00035 /// ResizableArray.h 00036 /// 00037 ///////////////////////////////////////////////////////////////////// 00038 #ifndef __ResizableArray_H__ 00039 #define __ResizableArray_H__ 00040 00041 ///////////////////////////////////////////////////////////////////// 00042 /// @addtogroup GlobalFunctions 00043 /** @{ */ 00044 00045 ///////////////////////////////////////////////////////////////////// 00046 /// A data structure that provides a resizable array 00047 /// This is used within Spam to avoid reading input data files multiple times, 00048 /// without having to use any STL library classes 00049 ///////////////////////////////////////////////////////////////////// 00050 class ResizableArray 00051 { 00052 public: 00053 ///////////////////////////////////////////////////////////////////// 00054 /// Allocate the memory for the initial size of the array 00055 /// 00056 /// @param initialSize 00057 ///////////////////////////////////////////////////////////////////// 00058 ResizableArray(int initialSize) 00059 { 00060 _memory = new int[initialSize]; 00061 memSize = initialSize; 00062 length = 0; 00063 }; 00064 00065 ///////////////////////////////////////////////////////////////////// 00066 /// Allocate the memory for the default initial size of the array 00067 /// 00068 ///////////////////////////////////////////////////////////////////// 00069 ResizableArray() 00070 { 00071 ResizableArray(DEFAULT_INITIAL_SIZE); 00072 }; 00073 00074 ///////////////////////////////////////////////////////////////////// 00075 /// Destructor 00076 ///////////////////////////////////////////////////////////////////// 00077 ~ResizableArray() 00078 { 00079 delete [] _memory; 00080 }; 00081 00082 ///////////////////////////////////////////////////////////////////// 00083 /// Push an item onto the end of the array, resizing 00084 /// the local copy in memory if necessary 00085 /// 00086 /// @param item The item to add 00087 ///////////////////////////////////////////////////////////////////// 00088 void Add(int item) 00089 { 00090 if (length < memSize) 00091 { 00092 _memory[length] = item; 00093 } 00094 else 00095 { 00096 // Double the size of our local array 00097 00098 // Allocate a temp array with double the size 00099 int * newMem = new int[memSize*2]; 00100 memcpy(newMem, _memory, sizeof(int)*memSize); 00101 delete [] _memory; 00102 00103 // Set _memory to temp 00104 _memory = newMem; 00105 _memory[length] = item; 00106 memSize = memSize*2; 00107 } 00108 length++; 00109 }; 00110 00111 ///////////////////////////////////////////////////////////////////// 00112 /// Return a copy of the data in the ResizableArray 00113 /// 00114 /// @param arr The memory location to copy the array 00115 /// @param len The number of elements in the array 00116 ///////////////////////////////////////////////////////////////////// 00117 void ToArray(int *&arr, int &len) 00118 { 00119 len = length; 00120 arr = new int[len]; 00121 memcpy(arr, _memory, sizeof(int)*len); 00122 }; 00123 00124 private: 00125 static const int DEFAULT_INITIAL_SIZE = 16; 00126 00127 // --- private variables 00128 int * _memory; // The internal array memory 00129 int memSize; // The actual size of memory that is allocated 00130 int length; // The number of items the user has added 00131 } 00132 ; // class ResizableArray 00133 00134 /** @} */ 00135 #endif 00136