1 // PROJECT: ReactOS ATL Simple Collection
2 // LICENSE: Public Domain
3 // PURPOSE: Provides compatibility to Microsoft ATL
4 // PROGRAMMERS: Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
6 #ifndef __ATLSIMPCOLL_H__
7 #define __ATLSIMPCOLL_H__
11 #include "atlcore.h" // for ATL Core
16 class CSimpleArrayEqualHelper
19 static bool IsEqual(const T
& t1
, const T
& t2
)
25 // This class exists for the element types of no comparison.
27 class CSimpleArrayEqualHelperFalse
30 static bool IsEqual(const T
&, const T
&)
37 template <typename T
, typename TEqual
= CSimpleArrayEqualHelper
<T
> >
41 typedef T _ArrayElementType
;
43 CSimpleArray() : m_pData(NULL
), m_nCount(0), m_nCapacity(0)
47 CSimpleArray(const CSimpleArray
<T
, TEqual
>& src
) :
48 m_pData(NULL
), m_nCount(0), m_nCapacity(0)
60 // is the capacity enough?
61 if (m_nCapacity
< m_nCount
+ 1)
63 // allocate extra capacity for optimization
64 const int nNewCapacity
= (m_nCount
+ 1) + c_nGrow
;
65 T
*pNewData
= (T
*)realloc(m_pData
, nNewCapacity
* sizeof(T
));
67 return FALSE
; // failure
70 m_nCapacity
= nNewCapacity
;
74 ConstructItemInPlace(m_nCount
, t
);
82 int Find(const T
& t
) const
84 for (int nIndex
= 0; nIndex
< m_nCount
; ++nIndex
)
86 if (TEqual::IsEqual(m_pData
[nIndex
], t
))
88 return nIndex
; // success
99 const T
* GetData() const
109 BOOL
Remove(const T
& t
)
111 return RemoveAt(Find(t
));
119 const int nCount
= m_nCount
;
120 for (int nIndex
= 0; nIndex
< nCount
; ++nIndex
)
122 DestructItem(nIndex
);
132 BOOL
RemoveAt(int nIndex
)
135 if (nIndex
< 0 || m_nCount
<= nIndex
)
136 return FALSE
; // failure
139 DestructItem(nIndex
);
141 // move range [nIndex + 1, m_nCount) to nIndex
142 const int nRightCount
= m_nCount
- (nIndex
+ 1);
143 const int nRightSize
= nRightCount
* sizeof(T
);
144 memmove(&m_pData
[nIndex
], &m_pData
[nIndex
+ 1], nRightSize
);
152 BOOL
SetAtIndex(int nIndex
, const T
& t
)
155 if (nIndex
< 0 || m_nCount
<= nIndex
)
156 return FALSE
; // failure
163 T
& operator[](int nIndex
)
165 ATLASSERT(0 <= nIndex
&& nIndex
< m_nCount
);
166 return m_pData
[nIndex
];
169 const T
& operator[](int nIndex
) const
171 ATLASSERT(0 <= nIndex
&& nIndex
< m_nCount
);
172 return m_pData
[nIndex
];
175 CSimpleArray
<T
, TEqual
>& operator=(const CSimpleArray
<T
, TEqual
>& src
)
177 // don't copy if two objects are same
181 if (src
.GetSize() != GetSize())
185 int nNewCount
= src
.GetSize();
187 T
*pNewData
= (T
*)realloc(m_pData
, nNewCount
* sizeof(T
));
189 if (pNewData
== NULL
)
190 return *this; // failure
194 m_nCount
= nNewCount
;
195 m_nCapacity
= nNewCount
;
199 for (int nIndex
= 0; nIndex
< m_nCount
; ++nIndex
)
201 DestructItem(nIndex
);
205 ATLASSERT(GetSize() == src
.GetSize());
206 for (int nIndex
= 0; nIndex
< src
.GetSize(); ++nIndex
)
208 ConstructItemInPlace(nIndex
, src
[nIndex
]);
215 T
* m_pData
; // malloc'ed
216 int m_nCount
; // # of items of type T
217 int m_nCapacity
; // for optimization
218 static const int c_nGrow
= 8; // for optimization
220 // NOTE: Range m_pData[0] .. m_pData[m_nCapacity - 1] are accessible.
221 // NOTE: Range [0, m_nCount) are constructed.
222 // NOTE: Range [m_nCount, m_nCapacity) are not constructed.
223 // NOTE: 0 <= m_nCount && m_nCount <= m_nCapacity.
225 // call constructor at nIndex
226 void ConstructItemInPlace(int nIndex
, const T
& src
)
228 new(&m_pData
[nIndex
]) ConstructImpl(src
);
231 // call destructor at nIndex
232 void DestructItem(int nIndex
)
234 m_pData
[nIndex
].~T();
241 ConstructImpl(const T
& obj
)
242 :m_ConstructHelper(obj
)
246 static void *operator new(size_t, void *ptr
)
251 static void operator delete(void *p
, void* )
260 template <typename TKey
, typename TVal
>
261 class CSimpleMapEqualHelper
264 static bool IsEqualKey(const TKey
& k1
, const TKey
& k2
)
269 static bool IsEqualValue(const TVal
& v1
, const TVal
& v2
)
275 // This class exists for the keys and the values of no comparison.
276 template <typename TKey
, typename TVal
>
277 class CSimpleMapEqualHelperFalse
280 static bool IsEqualKey(const TKey
& k1
, const TKey
& k2
)
286 static bool IsEqualValue(const TVal
& v1
, const TVal
& v2
)
293 template <typename TKey
, typename TVal
,
294 typename TEqual
= CSimpleMapEqualHelper
<TKey
, TVal
> >
298 typedef TKey _ArrayKeyType
;
299 typedef TVal _ArrayElementType
;
309 BOOL
Add(const TKey
& key
, const TVal
& val
)
312 return m_Pairs
.Add(pair
);
315 int FindKey(const TKey
& key
) const
317 const int nCount
= GetSize();
318 for (int nIndex
= 0; nIndex
< nCount
; ++nIndex
)
320 if (TEqual::IsEqualKey(m_Pairs
[nIndex
].key
, key
))
322 return nIndex
; // success
325 return -1; // failure
328 int FindVal(const TVal
& val
) const
330 const int nCount
= GetSize();
331 for (int nIndex
= 0; nIndex
< nCount
; ++nIndex
)
333 if (TEqual::IsEqualValue(m_Pairs
[nIndex
].val
, val
))
335 return nIndex
; // success
338 return -1; // failure
341 TKey
& GetKeyAt(int nIndex
)
343 ATLASSERT(0 <= nIndex
&& nIndex
< GetSize());
344 return m_Pairs
[nIndex
].key
;
347 const TKey
& GetKeyAt(int nIndex
) const
349 ATLASSERT(0 <= nIndex
&& nIndex
< GetSize());
350 return m_Pairs
[nIndex
].key
;
355 return m_Pairs
.GetSize();
358 TVal
& GetValueAt(int nIndex
)
360 ATLASSERT(0 <= nIndex
&& nIndex
< GetSize());
361 return m_Pairs
[nIndex
].val
;
364 const TVal
& GetValueAt(int nIndex
) const
366 ATLASSERT(0 <= nIndex
&& nIndex
< GetSize());
367 return m_Pairs
[nIndex
].val
;
370 TVal
Lookup(const TKey
& key
) const
372 int nIndex
= FindKey(key
);
375 return m_Pairs
[nIndex
].val
;
378 BOOL
Remove(const TKey
& key
)
380 int nIndex
= FindKey(key
);
381 return RemoveAt(nIndex
);
389 BOOL
RemoveAt(int nIndex
)
391 return m_Pairs
.RemoveAt(nIndex
);
394 TKey
ReverseLookup(const TVal
& val
) const
396 int nIndex
= FindVal(val
);
399 return m_Pairs
[nIndex
].key
;
402 BOOL
SetAt(const TKey
& key
, const TVal
& val
)
404 int nIndex
= FindKey(key
);
406 return Add(key
, val
);
408 m_Pairs
[nIndex
].val
= val
;
412 BOOL
SetAtIndex(int nIndex
, const TKey
& key
, const TVal
& val
)
415 if (nIndex
< 0 || GetSize() <= nIndex
)
418 m_Pairs
[nIndex
].key
= key
;
419 m_Pairs
[nIndex
].val
= val
;
433 Pair(const TKey
& k
, const TVal
& v
) : key(k
), val(v
)
437 Pair(const Pair
& pair
) : key(pair
.key
), val(pair
.val
)
441 Pair
& operator=(const Pair
& pair
)
449 CSimpleArray
<Pair
, CSimpleArrayEqualHelperFalse
<Pair
> > m_Pairs
;