35 template <
class T_KEY,
class T_ITEM,
class U_POOL = ArrayPoolDefault,
class TGrowBy = AkGrowByPolicy_DEFAULT,
class TMovePolicy = AkAssignmentMovePolicy<MapStruct<T_KEY, T_ITEM> > >
36 class CAkKeyArray :
public AkArray< MapStruct<T_KEY, T_ITEM>, const MapStruct<T_KEY, T_ITEM>&, U_POOL, TGrowBy, TMovePolicy>
46 return (it != this->
End()) ? &(it.pItem->item) :
NULL;
54 T_ITEM *
Set(T_KEY in_Key,
const T_ITEM & in_Item)
56 T_ITEM* pSearchedItem =
Exists(in_Key);
59 *pSearchedItem = in_Item;
66 pStruct->
key = in_Key;
67 pStruct->
item = in_Item;
68 pSearchedItem = &(pStruct->
item);
74 T_ITEM *
SetFirst(T_KEY in_Key,
const T_ITEM & in_Item)
76 T_ITEM* pSearchedItem =
Exists(in_Key);
79 *pSearchedItem = in_Item;
86 pStruct->
key = in_Key;
87 pStruct->
item = in_Item;
88 pSearchedItem = &(pStruct->
item);
94 T_ITEM *
Set(T_KEY in_Key)
96 T_ITEM* pSearchedItem =
Exists(in_Key);
102 pStruct->
key = in_Key;
103 pSearchedItem = &(pStruct->
item);
106 return pSearchedItem;
117 for (; it != itEnd; ++it)
119 if ((*it).key == in_Item)
133 if (it != this->
End())
146 if (it != this->
End())
168 return in_item.Key();
176 template <
class T_KEY>
187 template<
class THIS_CLASS>
193 template<
class THIS_CLASS>
202 template <
class T_KEY,
class T_ITEM,
class U_POOL = ArrayPoolDefault,
class U_KEY = AkGetArrayKey< T_KEY, T_ITEM >,
class TGrowBy = AkGrowByPolicy_DEFAULT,
class TMovePolicy = AkAssignmentMovePolicy<T_ITEM>,
class TComparePolicy = AkDefaultSortedKeyCompare<T_KEY> >
211 return TComparePolicy::Lesser((
void*)
this, a, b);
216 return TComparePolicy::Equal((
void*)
this, a, b);
224 if ((this->
m_pItems + index) == in_pItem)
234 return bFound ? pItem :
NULL;
239 T_ITEM *
Add(T_KEY in_key)
245 U_KEY::Get(*pItem) = in_key;
263 unsigned int uIdx = (
unsigned int)(pItem - this->
m_pItems);
264 pItem = this->
Insert(uIdx);
276 T_ITEM *
Set(T_KEY in_key)
279 return Set(in_key, bFound);
282 T_ITEM *
Set(T_KEY in_key,
bool & out_bExists)
289 unsigned int uIdx = (
unsigned int)(pItem - this->
m_pItems);
290 pItem = this->
Insert(uIdx);
298 U_KEY::Get(*pItem) = in_key;
306 T_ITEM * pItem =
Exists(in_key);
324 template <
typename T_UPDATE>
327 static const T_KEY&
Get(
const T_UPDATE& in) {
return in; }
332 typename U_UPDATEKEY = GetUpdateKey<T_UPDATE>,
333 typename FN_EXISTS = void (*)(T_ITEM&,
const T_UPDATE&),
334 typename FN_NEW =
void (*)(T_ITEM&,
const T_UPDATE&),
335 typename FN_OLD =
void (*)(T_ITEM&)
337 bool SortedUpdate(
AkUInt32 in_numUpdates,
const T_UPDATE* in_pUpdates, FN_EXISTS in_fnExists, FN_NEW in_fnNew, FN_OLD in_fnOld)
341 for (
AkUInt32 i = 1; i < in_numUpdates; ++i)
343 const T_KEY& a = U_UPDATEKEY::Get(in_pUpdates[i-1]);
344 const T_KEY& b = U_UPDATEKEY::Get(in_pUpdates[i]);
347 AKASSERT(!
"AkSortedKeyArray::SortedUpdate, input not sorted!");
355 T_KEY& a = U_KEY::Get(this->
m_pItems[i-1]);
356 T_KEY& b = U_KEY::Get(this->
m_pItems[i]);
359 AKASSERT(!
"AkSortedKeyArray::SortedUpdate, array not sorted!");
369 while (oldIdx < this->
Length() && newIdx < in_numUpdates)
371 T_ITEM& oldItem = this->
m_pItems[oldIdx];
372 T_KEY& oldKey = U_KEY::Get(oldItem);
374 const T_UPDATE& newItem = in_pUpdates[newIdx];
375 const T_KEY& newKey = U_UPDATEKEY::Get(newItem);
377 if (
Equal(oldKey, newKey))
380 in_fnExists(oldItem, newItem);
384 else if (
Lesser(oldKey, newKey))
387 if (in_fnOld(oldItem))
397 T_ITEM* pNew = this->
Insert(oldIdx);
400 U_KEY::Get(*pNew) = newKey;
401 in_fnNew(*pNew, newItem);
412 while (oldIdx < this->
Length())
414 if (in_fnOld(this->
m_pItems[oldIdx]))
421 for (; newIdx < in_numUpdates; ++newIdx)
423 const T_UPDATE& newItem = in_pUpdates[newIdx];
424 const T_KEY& newKey = U_UPDATEKEY::Get(newItem);
426 T_ITEM* pNew = this->
AddLast();
429 U_KEY::Get(*pNew) = newKey;
430 in_fnNew(*pNew, newItem);
441 void Reorder(T_KEY in_OldKey, T_KEY in_NewKey,
const T_ITEM & in_item)
449 unsigned int uIdx = (
unsigned int)(pItem - this->
m_pItems);
450 unsigned int uLastIdx = this->
Length() - 1;
454 bool bNeedReordering =
false;
457 T_ITEM * pPrevItem = this->
m_pItems + (uIdx - 1);
458 if (
Lesser(in_NewKey, U_KEY::Get(*pPrevItem)))
463 T_ITEM * pSecondPrevItem = this->
m_pItems + (uIdx - 2);
464 if (
Lesser(U_KEY::Get(*pSecondPrevItem), in_NewKey))
466 return Swap(pPrevItem, pItem);
470 bNeedReordering =
true;
475 return Swap(pPrevItem, pItem);
479 if (!bNeedReordering && uIdx < uLastIdx)
481 T_ITEM * pNextItem = this->
m_pItems + (uIdx + 1);
482 if (
Lesser(U_KEY::Get(*pNextItem), in_NewKey))
485 if (uIdx < (uLastIdx - 1))
487 T_ITEM * pSecondNextItem = this->
m_pItems + (uIdx + 2);
488 if (
Lesser(in_NewKey, U_KEY::Get(*pSecondNextItem)))
490 return Swap(pNextItem, pItem);
494 bNeedReordering =
true;
499 return Swap(pNextItem, pItem);
509 unsigned int uIdxToInsert;
513 uIdxToInsert = (
unsigned int)(pTargetItem - this->
m_pItems);
514 if (uIdxToInsert > uIdx)
521 uIdxToInsert = uLastIdx;
524 T_ITEM * pStartItem = this->
m_pItems + uIdx;
525 T_ITEM * pEndItem = this->
m_pItems + uIdxToInsert;
526 if (uIdxToInsert < uIdx)
529 while (pStartItem != pEndItem)
532 pStartItem[1] = pStartItem[0];
538 while (pStartItem != pEndItem)
540 pStartItem[0] = pStartItem[1];
544 pEndItem[0] = in_item;
554 if (NumItemsToReInsert != 0)
558 T_ITEM * pReinsertionItem = this->
m_pItems;
560 for (
AkInt32 idx = 0; idx < NumItemsToReInsert; ++idx)
562 T_ITEM ItemtoReinsert = pReinsertionItem[idx];
564 T_KEY keyToReinsert = U_KEY::Get(ItemtoReinsert);
566 T_ITEM* pInsertionEmplacement =
AddNoSetKey(keyToReinsert);
569 *pInsertionEmplacement = ItemtoReinsert;
581 while ( uNumToSearch > 0 )
583 iPivot = iBase + ( uNumToSearch >> 1 );
584 T_KEY pivotKey = U_KEY::Get( this->
m_pItems[ iPivot ] );
585 if (
Equal( pivotKey, in_key ) )
591 if (
Lesser( pivotKey, in_key ) )
612 AKASSERT(in_to.pItem >= in_from.pItem);
617 while (uNumToSearch > 0)
619 uPivot = uBase + (
AkUInt32)(uNumToSearch >> 1);
620 T_KEY pivotKey = U_KEY::Get(this->
m_pItems[uPivot]);
621 if (
Lesser(pivotKey, in_key))
634 T_ITEM ItemTemp = *in_ItemA;
635 *in_ItemA = *in_ItemB;
636 *in_ItemB = ItemTemp;
641 #endif //_KEYARRAY_H_