4#ifndef CASSOCIATIONMAP_CLASS_H
5#define CASSOCIATIONMAP_CLASS_H
11#include "Collection.h"
13BEGIN_MOOTOOLS_NAMESPACE
22template<
class MAPTYPE,
class ASSOCTYPE,
class SIZETYPE>
48 inline void FreeKeys()
53 inline void FreeEntries()
63 inline CollectorEntry *GetEntry(
int i)
68 inline int GetKeyCount()
70 return map.GetCount();
73 inline int FindKey(
MAPTYPE& key,
unsigned int& count)
75 return map.Find(key, count);
78 inline void RemoveKey(
MAPTYPE& key)
105 unsigned int *count = map.Find(key);
118 items.SetAtGrow(size++, entry);
121 void Add(
const CollectorEntry& entry)
123 unsigned int *count = map.Find(entry.key);
132 map.Insert(entry.key, 1);
135 items.SetAtGrow(size++, entry);
162template<
class MAPTYPE,
class ASSOCTYPE,
class SIZETYPE>
180 inline void FreeKeys()
185 inline int GetKeyCount()
187 return valuesCounts.GetCount();
190 inline int FindKey(
const MAPTYPE& key,
unsigned int& count)
192 return valuesCounts.Find(key, count);
197 return entries.GetNextPtr(pos);
200 inline HashPos GetFirstEntry()
202 return entries.GetFirst();
205 inline void FreeEntries()
222 if (entries.Insert(entry) ==
false)
225 unsigned int *count = valuesCounts.Find(entry.key);
229 valuesCounts.Insert(entry.key, 1);
238template<
class MAPTYPE,
class ASSOCTYPE,
class SIZETYPE,
template<
typename T1,
typename T2>
class COLLTYPE>
263 map.InitHashTable(entries);
271 if (map.Find(entry, index))
272 return assocs.GetEntry(index, size);
281 return (map.IsFound(entry) != 0);
288 if (map.Find(entry, index))
290 assocs.SetEntrySize(index, 0);
300 if (!map.Find(
from, index))
304 map.Insert(
to, index);
320 HashPos pos = map.GetFirst();
324 while (pos != HashEnd)
326 map.GetNext(pos, entry, index);
328 ASSOCTYPE *assoc = assocs.GetEntry(index, size);
342 for (
i=0;
i<size;
i++)
360 XTRACE(
_T(
"CAssociationMap: WARNING %d duplicated entry has been found in the map.\nCSingleAssociationCollector should be used instead CAssociationCollector.\n"), count);
372 HashPos pos = map.GetFirst();
375 while (pos != HashEnd)
377 map.GetNext(pos, entry, index);
379 ASSOCTYPE *assoc = assocs.GetEntry(index, size);
384 if (assoc[0] == assoc[1])
386 assocs.RemoveElementInEntry(index, 1);
394 if (assoc[0] == assoc[1])
396 assocs.RemoveElementInEntry(index, 1);
399 else if (assoc[0] == assoc[2])
401 assocs.RemoveElementInEntry(index, 2);
404 else if (assoc[1] == assoc[2])
406 assocs.RemoveElementInEntry(index, 2);
418 for (
i=0;
i<size;
i++)
422 assocs.RemoveElementInEntry(index,
i);
449 if (!map.Find(entry, index))
451 index = assocs.AddEntry(
FALSE);
452 map.Insert(entry, index);
455 assocs.AddElementInEntry(index,
element);
472 HashPos pos = map.GetFirst();
473 while (pos != HashEnd)
475 map.GetNext(pos, index);
482 HashPos GetFirst()
const
484 XASSERT(assocs.GetEntriesSize() == map.GetCount());
485 return map.GetFirst();
490 XASSERT(pos != HashEnd);
492 map.GetNext(pos, entry, index);
493 return assocs.GetEntry(index, size);
498 XASSERT(pos != HashEnd);
499 unsigned int *index =
NULL;
500 map.GetNext(pos, entry, index);
501 return assocs.GetEntry(*index, size);
507 XASSERT(assocs.GetEntriesSize() == map.GetCount());
508 return map.GetCount();
513 XASSERT(assocs.GetEntriesSize() == map.GetCount());
556 unsigned int *
sizes = xAllocateArray(
unsigned int,
keynbr);
557 unsigned int count, index = 0;
564 for (
i=0;
i<size;
i++)
567 if (
collector.FindKey(entry->key, count))
569 sizes[index++] = count;
577 assocs.SetEntriesSize(
keynbr, 0, COL_EMPTY_NEW_ENTRIES|COL_EXACTSIZE_ENTRIES,
sizes);
578 xDeallocateArray(
sizes);
580 map.InitHashTable(
keynbr);
583 unsigned int entryIndex = 0,
entryNbr = 0;
584 for (
i=0;
i<size;
i++)
587 if (!map.Find(entry->key, entryIndex))
593 assocs.AddElementInEntry(entryIndex, entry->assoc);
597 MOOTOOLS_PRIVATE_TRACE_IF(FreeExtra(),
_T(
"CAssociationMap::Convert(CAssociationCollector: FreeExtra performs some reallocation. It should not\n"));
613 map.InitHashTable(
keynbr);
615 unsigned int *
sizes = xAllocateArray(
unsigned int,
keynbr);
616 unsigned int index = 0;
622 unsigned int entryIndex = 0,
entryNbr = 0, count;
624 while (pos != HashEnd)
627 if (map.Find(entry->key, entryIndex))
630 if (
collector.FindKey(entry->key, count))
641 assocs.SetEntriesSize(
keynbr, 0, COL_EMPTY_NEW_ENTRIES|COL_EXACTSIZE_ENTRIES,
sizes);
643 xDeallocateArray(
sizes);
647 while (pos != HashEnd)
650 if (!map.Find(entry->key, entryIndex))
656 assocs.AddElementInEntry(entryIndex, entry->assoc);
662 MOOTOOLS_PRIVATE_TRACE_IF(FreeExtra(),
_T(
"CAssociationMap::Convert(CSingleAssociationCollector: FreeExtra performs some reallocation. It should not\n"));
666#define GROUPMAP_NOINDEX (-1)
674template<
class MAPTYPE,
class SIZETYPE,
template<
class T1,
class T2>
class COLLTYPE>
694 indexToGroup.Minimize();
695 groupAssoc.FreeExtra();
706 unsigned int firstgroup = groupAssoc.GetEntriesSize();
711 bool RemoveGroup(
unsigned int group)
713 if (
group >= groupAssoc.GetEntriesSize())
718 for (
i=0;
i<size;
i++)
719 indexToGroup.Remove(assoc[
i]);
721 groupAssoc.SetEntrySize(
group, 0);
728 if (
dstgroup >= groupAssoc.GetEntriesSize())
731 if (
srcgroup >= groupAssoc.GetEntriesSize())
748 groupAssoc.SetEntrySize(
srcgroup, 0);
756 XASSERT(
group < groupAssoc.GetEntriesSize());
763 if (
group == GROUPMAP_NOINDEX)
781 if (
group == GROUPMAP_NOINDEX)
788 for (
i = 0;
i<size;
i++)
792 groupAssoc.RemoveElementInEntry(
group,
i);
801 unsigned int GetGroupCount()
const
803 return groupAssoc.GetEntriesSize();
808 unsigned int group = GROUPMAP_NOINDEX;
815 if (
groupIndex >= groupAssoc.GetEntriesSize())
824 unsigned int GetGroupSize(
unsigned int groupIndex)
const
826 if (
groupIndex >= groupAssoc.GetEntriesSize())
838 this->indexToGroup =
copyFrom.indexToGroup;
839 this->groupAssoc.Copy(
copyFrom.groupAssoc);
846END_MOOTOOLS_NAMESPACE
The class defines an x, y, z 3D point which can use int, float or double.
Definition 3DPoint.h:27
This collector is to used when you know that the collecting process does not involves duplicate pairs...
Definition AssociationMap.h:24
This class handles an association between a key and its associated values.
Definition AssociationMap.h:240
ASSOCTYPE * Lookup(const MAPTYPE &entry, int &size) const
Giving a key return an array with the associates data and the number of element in that array.
Definition AssociationMap.h:268
void Convert(CSingleAssociationCollector< MAPTYPE, ASSOCTYPE, SIZETYPE > &collector)
Init the association map from a CSingleAssociationCollector.
Definition AssociationMap.h:601
bool MoveTo(const MAPTYPE &from, const MAPTYPE &to)
Move a from association key to a destination key and associates the from values to the destination ke...
Definition AssociationMap.h:297
bool RemoveAssociation(const MAPTYPE &refentry)
Definition AssociationMap.h:460
bool IsFound(const MAPTYPE &entry) const
Returns true if a key is found in the association map.
Definition AssociationMap.h:279
void AddAssociation(const MAPTYPE &entry, const ASSOCTYPE &element)
Definition AssociationMap.h:446
void Convert(CAssociationCollector< MAPTYPE, ASSOCTYPE, SIZETYPE > &collector)
Init the association map from a CAssociationCollector.
Definition AssociationMap.h:544
unsigned int GetSize() const
return the number of key values in the association map
Definition AssociationMap.h:505
void Remove(const MAPTYPE &entry)
Remove a key and its association from the map.
Definition AssociationMap.h:285
This class is useful to gather items together and retrieve the elements gathered.
Definition AssociationMap.h:676
CSingleAssociationCollector this collector ensures to collect single pairs in the association map.
Definition AssociationMap.h:164
@ GROW_DEFAULT
A default grow mode which increase the array size by 1024 bytes each times its needed....
Definition XTemplate.h:40
@ GROW_DOUBLE_SIZE
Double size the array for fast element growing (required when the final element number is not known,...
Definition XTemplate.h:42
void SetSize(int newCount, int newGrowCount=GROW_KEEP_MODE)
The buffer always grows unless FreeExtra / RemoveAll(false) is called.
Definition XTemplate.h:249
void RemoveAll(bool keepBuffer=false)
Remove all data and set size to 0.
Definition XTemplate.h:119
Definition AssociationMap.h:29
Definition AssociationMap.h:157