(file) Return to ArrayIterator.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 karl  1.3 //%2006////////////////////////////////////////////////////////////////////////
  2 mike  1.1 //
  3           // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
  4           // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
  5           // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
  6           // IBM Corp.; EMC Corporation, The Open Group.
  7           // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8           // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9           // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10           // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11 karl  1.3 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 12           // EMC Corporation; Symantec Corporation; The Open Group.
 13 mike  1.1 //
 14           // Permission is hereby granted, free of charge, to any person obtaining a copy
 15           // of this software and associated documentation files (the "Software"), to
 16           // deal in the Software without restriction, including without limitation the
 17           // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 18           // sell copies of the Software, and to permit persons to whom the Software is
 19           // furnished to do so, subject to the following conditions:
 20           // 
 21           // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 22           // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 23           // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 24           // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 25           // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 26           // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 27           // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 28           // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 29           //
 30           //==============================================================================
 31           //
 32           // Author: Michael E. Brasher, Inova Europe (mike-brasher@austin.rr.com)
 33           //
 34 mike  1.1 //%/////////////////////////////////////////////////////////////////////////////
 35           
 36           #ifndef Pegasus_ArrayIterator_h
 37           #define Pegasus_ArrayIterator_h
 38           
 39           #include <Pegasus/Common/Array.h>
 40           
 41           PEGASUS_NAMESPACE_BEGIN
 42           
 43           //
 44           // This class provides a faster way of iterating arrays. Due to the expense
 45           // of calling operator[], iteration is slower than necessary. Consider this 
 46           // example:
 47           //
 48           //     Array<Uint32> array;
 49           //     Uint32 sum = 0;
 50           //
 51           //     for (Uint32 i = 0, n = array.size(); i < n; i++)
 52           //         sum += array[i];
 53           //
 54           // Every use of array[i] results in a function call that checks for a bounds
 55 mike  1.1 // violation. We can see that an array bounds violation is impossible in this
 56           // case. We can use the ConstArrayIterator class to eliminate this overhead.
 57           // 
 58           //     Array<Uint32> array;
 59           //     Uint32 sum = 0;
 60           //
 61           //     ConstArrayIterator<Uint32> iterator(array);
 62           //
 63           //     for (Uint32 i = 0; i < iterator.size(); i++)
 64           //         sum += iterator[i];
 65           //
 66           // The constructor pre-saves the array size and data before entering the loop
 67           // so that iterator.size() and iterator[i] are trivial inline functions that
 68           // access these members.
 69           //
 70           template<class T>
 71           class ConstArrayIterator
 72           {
 73           public:
 74           
 75               ConstArrayIterator(const Array<T>& x) : _data(x.getData()), _size(x.size()) 
 76 mike  1.1     { 
 77               }
 78           
 79               Uint32 size() const 
 80               { 
 81           	return _size; 
 82               }
 83           
 84               const T& operator[](Uint32 i)  const 
 85               { 
 86           	PEGASUS_DEBUG_ASSERT(i < _size);
 87           	return _data[i]; 
 88               }
 89           
 90           private:
 91           
 92               ConstArrayIterator& operator=(const ConstArrayIterator<T>& x);
 93               ConstArrayIterator(const ConstArrayIterator<T>& x);
 94           
 95               const T* _data;
 96               Uint32 _size;
 97 mike  1.1 };
 98           
 99           //
100           // This class is similar to ConstArrayIterator except it provides a non-const
101           // version of operator[].
102           //
103           template<class T>
104           class ArrayIterator
105           {
106           public:
107           
108               ArrayIterator(Array<T>& x) : _data((T*)x.getData()), _size(x.size()) 
109               { 
110               }
111           
112               Uint32 size() const 
113               { 
114           	return _size; 
115               }
116           
117               const T& operator[](Uint32 i)  const 
118 mike  1.1     { 
119           	PEGASUS_DEBUG_ASSERT(i < _size);
120           	return _data[i]; 
121               }
122           
123               T& operator[](Uint32 i)
124               {
125           	PEGASUS_DEBUG_ASSERT(i < _size);
126           	return _data[i]; 
127               }
128           
129 r.kieninger 1.2     //
130                     // The reset() methode set the Iterators object members to new 
131                     // data pointer and size of a given Array<T>
132                     //
133                     void reset(Array<T>& x)
134                     { 
135                         _data = (T*)x.getData();
136                         _size = x.size();
137                     }
138                 
139 mike        1.1 private:
140                 
141                     ArrayIterator& operator=(const ArrayIterator<T>& x);
142                     ArrayIterator(const ArrayIterator<T>& x);
143                 
144                     T* _data;
145                     Uint32 _size;
146                 };
147                 
148                 PEGASUS_NAMESPACE_END
149                 
150                 #endif /* Pegasus_ArrayIterator_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2