PBRT
|
00001 00002 // 00003 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas 00004 // Digital Ltd. LLC 00005 // 00006 // All rights reserved. 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are 00010 // met: 00011 // * Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // * Redistributions in binary form must reproduce the above 00014 // copyright notice, this list of conditions and the following disclaimer 00015 // in the documentation and/or other materials provided with the 00016 // distribution. 00017 // * Neither the name of Industrial Light & Magic nor the names of 00018 // its contributors may be used to endorse or promote products derived 00019 // from this software without specific prior written permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00024 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00025 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00027 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00028 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00029 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00030 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00031 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00034 00035 00036 00037 #ifndef INCLUDED_IMF_ARRAY_H 00038 #define INCLUDED_IMF_ARRAY_H 00039 00040 //------------------------------------------------------------------------- 00041 // 00042 // class Array 00043 // class Array2D 00044 // 00045 // "Arrays of T" whose sizes are not known at compile time. 00046 // When an array goes out of scope, its elements are automatically 00047 // deleted. 00048 // 00049 // Usage example: 00050 // 00051 // struct C 00052 // { 00053 // C () {std::cout << "C::C (" << this << ")\n";}; 00054 // virtual ~C () {std::cout << "C::~C (" << this << ")\n";}; 00055 // }; 00056 // 00057 // int 00058 // main () 00059 // { 00060 // Array <C> a(3); 00061 // 00062 // C &b = a[1]; 00063 // const C &c = a[1]; 00064 // C *d = a + 2; 00065 // const C *e = a; 00066 // 00067 // return 0; 00068 // } 00069 // 00070 //------------------------------------------------------------------------- 00071 00072 namespace Imf { 00073 00074 00075 template <class T> 00076 class Array 00077 { 00078 public: 00079 00080 //----------------------------- 00081 // Constructors and destructors 00082 //----------------------------- 00083 00084 Array () {_data = 0;} 00085 Array (long size) {_data = new T[size];} 00086 ~Array () {delete [] _data;} 00087 00088 00089 //----------------------------- 00090 // Access to the array elements 00091 //----------------------------- 00092 00093 operator T * () {return _data;} 00094 operator const T * () const {return _data;} 00095 00096 00097 //------------------------------------------------------ 00098 // Resize and clear the array (the contents of the array 00099 // are not preserved across the resize operation). 00100 // 00101 // resizeEraseUnsafe() is more memory efficient than 00102 // resizeErase() because it deletes the old memory block 00103 // before allocating a new one, but if allocating the 00104 // new block throws an exception, resizeEraseUnsafe() 00105 // leaves the array in an unusable state. 00106 // 00107 //------------------------------------------------------ 00108 00109 void resizeErase (long size); 00110 void resizeEraseUnsafe (long size); 00111 00112 00113 private: 00114 00115 Array (const Array &); // Copying and assignment 00116 Array & operator = (const Array &); // are not implemented 00117 00118 T * _data; 00119 }; 00120 00121 00122 template <class T> 00123 class Array2D 00124 { 00125 public: 00126 00127 //----------------------------- 00128 // Constructors and destructors 00129 //----------------------------- 00130 00131 Array2D (); // empty array, 0 by 0 elements 00132 Array2D (long sizeX, long sizeY); // sizeX by sizeY elements 00133 ~Array2D (); 00134 00135 00136 //----------------------------- 00137 // Access to the array elements 00138 //----------------------------- 00139 00140 T * operator [] (long x); 00141 const T * operator [] (long x) const; 00142 00143 00144 //------------------------------------------------------ 00145 // Resize and clear the array (the contents of the array 00146 // are not preserved across the resize operation). 00147 // 00148 // resizeEraseUnsafe() is more memory efficient than 00149 // resizeErase() because it deletes the old memory block 00150 // before allocating a new one, but if allocating the 00151 // new block throws an exception, resizeEraseUnsafe() 00152 // leaves the array in an unusable state. 00153 // 00154 //------------------------------------------------------ 00155 00156 void resizeErase (long sizeX, long sizeY); 00157 void resizeEraseUnsafe (long sizeX, long sizeY); 00158 00159 00160 private: 00161 00162 Array2D (const Array2D &); // Copying and assignment 00163 Array2D & operator = (const Array2D &); // are not implemented 00164 00165 long _sizeY; 00166 T * _data; 00167 }; 00168 00169 00170 //--------------- 00171 // Implementation 00172 //--------------- 00173 00174 template <class T> 00175 inline void 00176 Array<T>::resizeErase (long size) 00177 { 00178 T *tmp = new T[size]; 00179 delete [] _data; 00180 _data = tmp; 00181 } 00182 00183 00184 template <class T> 00185 inline void 00186 Array<T>::resizeEraseUnsafe (long size) 00187 { 00188 delete [] _data; 00189 _data = 0; 00190 _data = new T[size]; 00191 } 00192 00193 00194 template <class T> 00195 inline 00196 Array2D<T>::Array2D (): 00197 _sizeY (0), _data (0) 00198 { 00199 // emtpy 00200 } 00201 00202 00203 template <class T> 00204 inline 00205 Array2D<T>::Array2D (long sizeX, long sizeY): 00206 _sizeY (sizeY), _data (new T[sizeX * sizeY]) 00207 { 00208 // emtpy 00209 } 00210 00211 00212 template <class T> 00213 inline 00214 Array2D<T>::~Array2D () 00215 { 00216 delete [] _data; 00217 } 00218 00219 00220 template <class T> 00221 inline T * 00222 Array2D<T>::operator [] (long x) 00223 { 00224 return _data + x * _sizeY; 00225 } 00226 00227 00228 template <class T> 00229 inline const T * 00230 Array2D<T>::operator [] (long x) const 00231 { 00232 return _data + x * _sizeY; 00233 } 00234 00235 00236 template <class T> 00237 inline void 00238 Array2D<T>::resizeErase (long sizeX, long sizeY) 00239 { 00240 T *tmp = new T[sizeX * sizeY]; 00241 delete [] _data; 00242 _sizeY = sizeY; 00243 _data = tmp; 00244 } 00245 00246 00247 template <class T> 00248 inline void 00249 Array2D<T>::resizeEraseUnsafe (long sizeX, long sizeY) 00250 { 00251 delete [] _data; 00252 _data = 0; 00253 _sizeY = 0; 00254 _data = new T[sizeX * sizeY]; 00255 _sizeY = sizeY; 00256 } 00257 00258 00259 } // namespace Imf 00260 00261 #endif