1 ///////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
6 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ///////////////////////////////////////////////////////////////////////////
37 #ifndef INCLUDED_IMF_ARRAY_H
38 #define INCLUDED_IMF_ARRAY_H
40 //-------------------------------------------------------------------------
45 // "Arrays of T" whose sizes are not known at compile time.
46 // When an array goes out of scope, its elements are automatically
53 // C () {std::cout << "C::C (" << this << ")\n";};
54 // virtual ~C () {std::cout << "C::~C (" << this << ")\n";};
70 //-------------------------------------------------------------------------
80 //-----------------------------
81 // Constructors and destructors
82 //-----------------------------
85 Array (long size) {_data = new T[size];}
86 ~Array () {delete [] _data;}
89 //-----------------------------
90 // Access to the array elements
91 //-----------------------------
93 operator T * () {return _data;}
94 operator const T * () const {return _data;}
97 //------------------------------------------------------
98 // Resize and clear the array (the contents of the array
99 // are not preserved across the resize operation).
101 // resizeEraseUnsafe() is more memory efficient than
102 // resizeErase() because it deletes the old memory block
103 // before allocating a new one, but if allocating the
104 // new block throws an exception, resizeEraseUnsafe()
105 // leaves the array in an unusable state.
107 //------------------------------------------------------
109 void resizeErase (long size);
110 void resizeEraseUnsafe (long size);
115 Array (const Array &); // Copying and assignment
116 Array & operator = (const Array &); // are not implemented
127 //-----------------------------
128 // Constructors and destructors
129 //-----------------------------
131 Array2D (); // empty array, 0 by 0 elements
132 Array2D (long sizeX, long sizeY); // sizeX by sizeY elements
136 //-----------------------------
137 // Access to the array elements
138 //-----------------------------
140 T * operator [] (long x);
141 const T * operator [] (long x) const;
144 //------------------------------------------------------
145 // Resize and clear the array (the contents of the array
146 // are not preserved across the resize operation).
148 // resizeEraseUnsafe() is more memory efficient than
149 // resizeErase() because it deletes the old memory block
150 // before allocating a new one, but if allocating the
151 // new block throws an exception, resizeEraseUnsafe()
152 // leaves the array in an unusable state.
154 //------------------------------------------------------
156 void resizeErase (long sizeX, long sizeY);
157 void resizeEraseUnsafe (long sizeX, long sizeY);
162 Array2D (const Array2D &); // Copying and assignment
163 Array2D & operator = (const Array2D &); // are not implemented
176 Array<T>::resizeErase (long size)
178 T *tmp = new T[size];
186 Array<T>::resizeEraseUnsafe (long size)
196 Array2D<T>::Array2D ():
197 _sizeY (0), _data (0)
205 Array2D<T>::Array2D (long sizeX, long sizeY):
206 _sizeY (sizeY), _data (new T[sizeX * sizeY])
214 Array2D<T>::~Array2D ()
222 Array2D<T>::operator [] (long x)
224 return _data + x * _sizeY;
230 Array2D<T>::operator [] (long x) const
232 return _data + x * _sizeY;
238 Array2D<T>::resizeErase (long sizeX, long sizeY)
240 T *tmp = new T[sizeX * sizeY];
249 Array2D<T>::resizeEraseUnsafe (long sizeX, long sizeY)
254 _data = new T[sizeX * sizeY];