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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2