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

  1 martin 1.5 //%LICENSE////////////////////////////////////////////////////////////////
  2 martin 1.6 //
  3 martin 1.5 // Licensed to The Open Group (TOG) under one or more contributor license
  4            // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
  5            // this work for additional information regarding copyright ownership.
  6            // Each contributor licenses this file to you under the OpenPegasus Open
  7            // Source License; you may not use this file except in compliance with the
  8            // License.
  9 martin 1.6 //
 10 martin 1.5 // Permission is hereby granted, free of charge, to any person obtaining a
 11            // copy of this software and associated documentation files (the "Software"),
 12            // to deal in the Software without restriction, including without limitation
 13            // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 14            // and/or sell copies of the Software, and to permit persons to whom the
 15            // Software is furnished to do so, subject to the following conditions:
 16 martin 1.6 //
 17 martin 1.5 // The above copyright notice and this permission notice shall be included
 18            // in all copies or substantial portions of the Software.
 19 martin 1.6 //
 20 martin 1.5 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 21 martin 1.6 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 22 martin 1.5 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 23            // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 24            // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 25            // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 26            // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27 martin 1.6 //
 28 martin 1.5 //////////////////////////////////////////////////////////////////////////
 29 mike   1.1 //
 30            //%/////////////////////////////////////////////////////////////////////////////
 31            
 32            #ifndef Pegasus_ArrayIterator_h
 33            #define Pegasus_ArrayIterator_h
 34            
 35            #include <Pegasus/Common/Array.h>
 36            
 37            PEGASUS_NAMESPACE_BEGIN
 38            
 39            //
 40            // This class provides a faster way of iterating arrays. Due to the expense
 41 kumpf  1.4 // of calling operator[], iteration is slower than necessary. Consider this
 42 mike   1.1 // example:
 43            //
 44            //     Array<Uint32> array;
 45            //     Uint32 sum = 0;
 46            //
 47            //     for (Uint32 i = 0, n = array.size(); i < n; i++)
 48            //         sum += array[i];
 49            //
 50            // Every use of array[i] results in a function call that checks for a bounds
 51            // violation. We can see that an array bounds violation is impossible in this
 52            // case. We can use the ConstArrayIterator class to eliminate this overhead.
 53 kumpf  1.4 //
 54 mike   1.1 //     Array<Uint32> array;
 55            //     Uint32 sum = 0;
 56            //
 57            //     ConstArrayIterator<Uint32> iterator(array);
 58            //
 59            //     for (Uint32 i = 0; i < iterator.size(); i++)
 60            //         sum += iterator[i];
 61            //
 62            // The constructor pre-saves the array size and data before entering the loop
 63            // so that iterator.size() and iterator[i] are trivial inline functions that
 64            // access these members.
 65            //
 66            template<class T>
 67            class ConstArrayIterator
 68            {
 69            public:
 70            
 71 kumpf  1.4     ConstArrayIterator(const Array<T>& x) : _data(x.getData()), _size(x.size())
 72                {
 73 mike   1.1     }
 74            
 75 kumpf  1.4     Uint32 size() const
 76                {
 77                    return _size;
 78 mike   1.1     }
 79            
 80 kumpf  1.4     const T& operator[](Uint32 i)  const
 81                {
 82                    PEGASUS_DEBUG_ASSERT(i < _size);
 83                    return _data[i];
 84 mike   1.1     }
 85            
 86            private:
 87            
 88                ConstArrayIterator& operator=(const ConstArrayIterator<T>& x);
 89                ConstArrayIterator(const ConstArrayIterator<T>& x);
 90            
 91                const T* _data;
 92                Uint32 _size;
 93            };
 94            
 95            //
 96            // This class is similar to ConstArrayIterator except it provides a non-const
 97            // version of operator[].
 98            //
 99            template<class T>
100            class ArrayIterator
101            {
102            public:
103            
104 kumpf  1.4     ArrayIterator(Array<T>& x) : _data((T*)x.getData()), _size(x.size())
105                {
106 mike   1.1     }
107            
108 kumpf  1.4     Uint32 size() const
109                {
110                    return _size;
111 mike   1.1     }
112            
113 kumpf  1.4     const T& operator[](Uint32 i)  const
114                {
115                    PEGASUS_DEBUG_ASSERT(i < _size);
116                    return _data[i];
117 mike   1.1     }
118            
119                T& operator[](Uint32 i)
120                {
121 kumpf  1.4         PEGASUS_DEBUG_ASSERT(i < _size);
122                    return _data[i];
123 mike   1.1     }
124            
125 r.kieninger 1.2     //
126 kumpf       1.4     // The reset() method sets the Iterators object members to new
127 r.kieninger 1.2     // data pointer and size of a given Array<T>
128                     //
129                     void reset(Array<T>& x)
130 kumpf       1.4     {
131 r.kieninger 1.2         _data = (T*)x.getData();
132                         _size = x.size();
133                     }
134                 
135 mike        1.1 private:
136                 
137                     ArrayIterator& operator=(const ArrayIterator<T>& x);
138                     ArrayIterator(const ArrayIterator<T>& x);
139                 
140                     T* _data;
141                     Uint32 _size;
142                 };
143                 
144                 PEGASUS_NAMESPACE_END
145                 
146                 #endif /* Pegasus_ArrayIterator_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2