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 //%/////////////////////////////////////////////////////////////////////////////
33
34 mike 1.1 #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 kumpf 1.4 // of calling operator[], iteration is slower than necessary. Consider this
|
44 mike 1.1 // 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 kumpf 1.4 //
|
56 mike 1.1 // 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 // 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 kumpf 1.4 ConstArrayIterator(const Array<T>& x) : _data(x.getData()), _size(x.size())
74 {
|
75 mike 1.1 }
76
|
77 kumpf 1.4 Uint32 size() const
78 {
79 return _size;
|
80 mike 1.1 }
81
|
82 kumpf 1.4 const T& operator[](Uint32 i) const
83 {
84 PEGASUS_DEBUG_ASSERT(i < _size);
85 return _data[i];
|
86 mike 1.1 }
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 kumpf 1.4 ArrayIterator(Array<T>& x) : _data((T*)x.getData()), _size(x.size())
107 {
|
108 mike 1.1 }
109
|
110 kumpf 1.4 Uint32 size() const
111 {
112 return _size;
|
113 mike 1.1 }
114
|
115 kumpf 1.4 const T& operator[](Uint32 i) const
116 {
117 PEGASUS_DEBUG_ASSERT(i < _size);
118 return _data[i];
|
119 mike 1.1 }
120
121 T& operator[](Uint32 i)
122 {
|
123 kumpf 1.4 PEGASUS_DEBUG_ASSERT(i < _size);
124 return _data[i];
|
125 mike 1.1 }
126
|
127 r.kieninger 1.2 //
|
128 kumpf 1.4 // The reset() method sets the Iterators object members to new
|
129 r.kieninger 1.2 // data pointer and size of a given Array<T>
130 //
131 void reset(Array<T>& x)
|
132 kumpf 1.4 {
|
133 r.kieninger 1.2 _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 */
|