Debian Codegraph @

source/unix/odesrc/OPCODE/Ice/IceContainer.h

raw
1 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 /**
3 * Contains a simple container class.
4 * \file IceContainer.h
5 * \author Pierre Terdiman
6 * \date February, 5, 2000
7 */
8 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
9
10 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
11 // Include Guard
12 #ifndef __ICECONTAINER_H__
13 #define __ICECONTAINER_H__
14
15 // #define CONTAINER_STATS
16
17 enum FindMode
18 {
19 FIND_CLAMP,
20 FIND_WRAP,
21
22 FIND_FORCE_DWORD = 0x7fffffff
23 };
24
25 class ICECORE_API Container
26 {
27 public:
28 // Constructor / Destructor
29 Container();
30 Container(const Container& object);
31 Container(udword size, float growth_factor);
32 ~Container();
33 // Management
34 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
35 /**
36 * A O(1) method to add a value in the container. The container is automatically resized if needed.
37 * The method is inline, not the resize. The call overhead happens on resizes only, which is not a problem since the resizing operation
38 * costs a lot more than the call overhead...
39 *
40 * \param entry [in] a udword to store in the container
41 * \see Add(float entry)
42 * \see Empty()
43 * \see Contains(udword entry)
44 * \return Self-Reference
45 */
46 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
47 inline_ Container& Add(udword entry)
48 {
49 // Resize if needed
50 if (mCurNbEntries == mMaxNbEntries
51 && !Resize())
52 {
53 IceAbort();
54 }
55
56 // Add new entry
57 mEntries[mCurNbEntries++] = entry;
58 return *this;
59 }
60
61 inline_ Container& Add(const uword* entries, udword nb)
62 {
63 // Resize if needed
64 if (mCurNbEntries + nb > mMaxNbEntries
65 && !Resize(nb))
66 {
67 IceAbort();
68 }
69
70 // Add new entry
71 CopyMemory(&mEntries[mCurNbEntries], entries, nb*sizeof(uword));
72 mCurNbEntries+=nb;
73 return *this;
74 }
75
76 inline_ Container& Add(const udword* entries, udword nb)
77 {
78 // Resize if needed
79 if (mCurNbEntries + nb > mMaxNbEntries
80 && !Resize(nb))
81 {
82 IceAbort();
83 }
84
85 // Add new entry
86 CopyMemory(&mEntries[mCurNbEntries], entries, nb*sizeof(udword));
87 mCurNbEntries+=nb;
88 return *this;
89 }
90
91 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
92 /**
93 * A O(1) method to add a value in the container. The container is automatically resized if needed.
94 * The method is inline, not the resize. The call overhead happens on resizes only, which is not a problem since the resizing operation
95 * costs a lot more than the call overhead...
96 *
97 * \param entry [in] a float to store in the container
98 * \see Add(udword entry)
99 * \see Empty()
100 * \see Contains(udword entry)
101 * \return Self-Reference
102 */
103 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
104 inline_ Container& Add(float entry)
105 {
106 // Resize if needed
107 if (mCurNbEntries == mMaxNbEntries
108 && !Resize())
109 {
110 IceAbort();
111 }
112
113 // Add new entry
114 mEntries[mCurNbEntries++] = IR(entry);
115 return *this;
116 }
117
118 inline_ Container& Add(const float* entries, udword nb)
119 {
120 // Resize if needed
121 if (mCurNbEntries + nb > mMaxNbEntries
122 && !Resize(nb))
123 {
124 IceAbort();
125 }
126
127 // Add new entry
128 CopyMemory(&mEntries[mCurNbEntries], entries, nb*sizeof(float));
129 mCurNbEntries+=nb;
130 return *this;
131 }
132
133 //! Add unique [slow]
134 inline_ Container& AddUnique(udword entry)
135 {
136 if(!Contains(entry)) Add(entry);
137 return *this;
138 }
139
140 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
141 /**
142 * Clears the container. All stored values are deleted, and it frees used ram.
143 * \see Reset()
144 * \return Self-Reference
145 */
146 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
147 Container& Empty();
148
149 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
150 /**
151 * Resets the container. Stored values are discarded but the buffer is kept so that further calls don't need resizing again.
152 * That's a kind of temporal coherence.
153 * \see Empty()
154 */
155 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
156 inline_ void Reset()
157 {
158 // Avoid the write if possible
159 // ### CMOV
160 if(mCurNbEntries) mCurNbEntries = 0;
161 }
162
163 // HANDLE WITH CARE
164 inline_ void ForceSize(udword size)
165 {
166 mCurNbEntries = size;
167 }
168
169 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
170 /**
171 * Sets the initial size of the container. If it already contains something, it's discarded.
172 * \param nb [in] Number of entries
173 * \return true if success
174 */
175 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
176 bool SetSize(udword nb);
177
178 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
179 /**
180 * Refits the container and get rid of unused bytes.
181 * \return true if success
182 */
183 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
184 bool Refit();
185
186 // Checks whether the container already contains a given value.
187 bool Contains(udword entry, udword* location=null) const;
188 // Deletes an entry - doesn't preserve insertion order.
189 bool Delete(udword entry);
190 // Deletes an entry - does preserve insertion order.
191 bool DeleteKeepingOrder(udword entry);
192 //! Deletes the very last entry.
193 inline_ void DeleteLastEntry() { if(mCurNbEntries) mCurNbEntries--; }
194 //! Deletes the entry whose index is given
195 inline_ void DeleteIndex(udword index) { ASSERT(index < mCurNbEntries); mEntries[index] = mEntries[--mCurNbEntries]; }
196
197 // Helpers
198 Container& FindNext(udword& entry, FindMode find_mode=FIND_CLAMP);
199 Container& FindPrev(udword& entry, FindMode find_mode=FIND_CLAMP);
200 // Data access.
201 inline_ udword GetNbEntries() const { return mCurNbEntries; } //!< Returns the current number of entries.
202 inline_ udword GetEntry(udword i) const { return mEntries[i]; } //!< Returns ith entry
203 inline_ udword* GetEntries() const { return mEntries; } //!< Returns the list of entries.
204
205 inline_ udword GetFirst() const { return mEntries[0]; }
206 inline_ udword GetLast() const { return mEntries[mCurNbEntries-1]; }
207
208 // Growth control
209 inline_ udword GetGrowthFactor() const { return mGrowthFactor; } //!< Returns the growth factor
210 inline_ void SetGrowthFactor(udword growth) { mGrowthFactor = growth; } //!< Sets the growth factor
211 inline_ bool IsFull() const { return mCurNbEntries==mMaxNbEntries; } //!< Checks the container is full
212 inline_ BOOL IsNotEmpty() const { return mCurNbEntries; } //!< Checks the container is empty
213
214 //! Read-access as an array
215 inline_ udword operator[](udword i) const { ASSERT(i>=0 && i<mCurNbEntries); return mEntries[i]; }
216 //! Write-access as an array
217 inline_ udword& operator[](udword i) { ASSERT(i>=0 && i<mCurNbEntries); return mEntries[i]; }
218
219 // Stats
220 udword GetUsedRam() const;
221
222 //! Operator for "Container A = Container B"
223 //void operator = (const Container& object);
224
225 #ifdef CONTAINER_STATS
226 inline_ udword GetNbContainers() const { return mNbContainers; }
227 inline_ udword GetTotalBytes() const { return mUsedRam; }
228 private:
229
230 static udword mNbContainers; //!< Number of containers around
231 static udword mUsedRam; //!< Amount of bytes used by containers in the system
232 #endif
233 private:
234 // Resizing
235 bool Resize(udword needed=1);
236 // Data
237 udword mMaxNbEntries; //!< Maximum possible number of entries
238 udword mCurNbEntries; //!< Current number of entries
239 udword* mEntries; //!< List of entries
240 udword mGrowthFactor; //!< Resize: new number of entries = old number * mGrowthFactor
241 };
242
243 #endif // __ICECONTAINER_H__