Added the ImplicitKey entry strategy, which allows the key to be calculated on the fly.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3802 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold
2003-07-02 03:17:43 +00:00
parent 4b582fd16a
commit 83744ac99d
+62 -11
View File
@@ -41,6 +41,10 @@ namespace VectorMapEntryStrategy {
// Pair // Pair
template<typename Key, typename Value, template<typename Key, typename Value,
typename KeyOrder = KernelUtilsOrder::Ascending<Key> > class Pair; typename KeyOrder = KernelUtilsOrder::Ascending<Key> > class Pair;
// ImplicitKey
template<typename Key, typename Value, typename GetKey,
typename KeyOrder = KernelUtilsOrder::Ascending<Key> >
class ImplicitKey;
} }
template<typename Entry, typename Parent, typename EntryIterator> template<typename Entry, typename Parent, typename EntryIterator>
@@ -72,12 +76,14 @@ class VectorMap {
private: private:
typedef _VECTOR_MAP_CLASS_NAME Class; typedef _VECTOR_MAP_CLASS_NAME Class;
typedef typename EntryStrategy::Entry _Entry; typedef typename EntryStrategy::Entry _Entry;
typedef typename EntryStrategy::KeyReference KeyReference;
typedef Vector<_Entry> ElementVector; typedef Vector<_Entry> ElementVector;
public: public:
typedef VectorMapEntry<Key, Value, _Entry, Class> Entry; typedef VectorMapEntry<KeyReference, Value, _Entry, Class>
typedef VectorMapEntry<Key, const Value, const _Entry, const Class> Entry;
ConstEntry; typedef VectorMapEntry<KeyReference, const Value, const _Entry,
const Class> ConstEntry;
typedef VectorMapIterator<Entry, Class, ElementVector::Iterator> typedef VectorMapIterator<Entry, Class, ElementVector::Iterator>
Iterator; Iterator;
typedef VectorMapIterator<ConstEntry, const Class, typedef VectorMapIterator<ConstEntry, const Class,
@@ -129,16 +135,17 @@ private:
// VectorMapEntry // VectorMapEntry
template<typename _Key, typename _Value, typename Entry, typename Parent> template<typename KeyReference, typename _Value, typename Entry,
typename Parent>
class VectorMapEntry { class VectorMapEntry {
private: private:
typedef VectorMapEntry<_Key, _Value, Entry, Parent> Class; typedef VectorMapEntry<KeyReference, _Value, Entry, Parent> Class;
public: public:
VectorMapEntry() VectorMapEntry()
: fParent(NULL), fEntry(NULL) {} : fParent(NULL), fEntry(NULL) {}
inline const _Key &Key() const inline KeyReference Key() const
{ {
return fParent->fEntryStrategy.GetKey(*fEntry); return fParent->fEntryStrategy.GetKey(*fEntry);
} }
@@ -171,13 +178,13 @@ private:
typedef VectorMapIterator<Entry, Parent, EntryIterator> Iterator; typedef VectorMapIterator<Entry, Parent, EntryIterator> Iterator;
public: public:
inline VectorMapIterator<Entry, Parent, EntryIterator>() inline VectorMapIterator()
: fParent(NULL), : fParent(NULL),
fIterator() fIterator()
{ {
} }
inline VectorMapIterator<Entry, Parent, EntryIterator>( inline VectorMapIterator(
const Iterator &other) const Iterator &other)
: fParent(other.fParent), : fParent(other.fParent),
fIterator(other.fIterator) fIterator(other.fIterator)
@@ -244,8 +251,7 @@ public:
// private // private
public: public:
inline VectorMapIterator<Entry, Parent, EntryIterator>(Parent *parent, inline VectorMapIterator(Parent *parent, const EntryIterator &iterator)
const EntryIterator &iterator)
: fParent(parent), : fParent(parent),
fIterator(iterator) fIterator(iterator)
{ {
@@ -690,9 +696,12 @@ _VECTOR_MAP_CLASS_NAME::_FindInsertionIndex(const Key &key,
namespace VectorMapEntryStrategy { namespace VectorMapEntryStrategy {
// Pair
template<typename Key, typename Value, typename KeyOrder> template<typename Key, typename Value, typename KeyOrder>
class Pair { class Pair {
public: public:
typedef const Key &KeyReference;
class Entry { class Entry {
public: public:
Entry(const Key &key, const Value &value) Entry(const Key &key, const Value &value)
@@ -702,7 +711,7 @@ public:
Value value; Value value;
}; };
inline const Key &GetKey(const Entry &entry) const inline KeyReference GetKey(const Entry &entry) const
{ {
return entry.key; return entry.key;
} }
@@ -736,6 +745,48 @@ private:
KeyOrder fCompare; KeyOrder fCompare;
}; };
// ImplicitKey
template<typename Key, typename Value, typename _GetKey, typename KeyOrder>
class ImplicitKey {
public:
typedef Key KeyReference;
typedef Value Entry;
inline KeyReference GetKey(const Entry &entry) const
{
return fGetKey(entry);
}
inline const Value &GetValue(const Entry &entry) const
{
return entry;
}
inline Value &GetValue(Entry &entry) const
{
return entry;
}
inline Entry MakeEntry(const Key &, const Value &value) const
{
return value;
}
inline bool AreCompatible(const Key &key, const Value &value) const
{
return (fGetKey(value) == key);
}
inline int Compare(const Key &a, const Key &b) const
{
return fCompare(a, b);
}
private:
KeyOrder fCompare;
_GetKey fGetKey;
};
} // VectorMapEntryStrategy } // VectorMapEntryStrategy
#endif // _VECTOR_MAP_H #endif // _VECTOR_MAP_H