00001 #ifndef KDE__DICT_H
00002 #define KDE__DICT_H
00003
00004 #include <faith/list.h>
00005
00006 namespace Faith
00007 {
00008
00009 template<class TypeKey, class TypeValue>
00010 class FAITH_TOOLS_EXPORT Dict
00011 {
00012 struct Map
00013 {
00014 TypeKey key;
00015 TypeValue *value;
00016 };
00017
00018 mutable VList list;
00019
00020 public:
00021 Dict() {}
00022 ~Dict() {}
00023
00024 TypeValue *& insert(const TypeKey &key, TypeValue *value)
00025 {
00026 Map *map=new Map;
00027 map->key = key;
00028 map->value = value;
00029 list.append(map);
00030 return map->value;
00031 }
00032
00033 bool contains(const TypeKey &key) const
00034 {
00035 return find(key);
00036 }
00037
00038 TypeKey *keyOf(const TypeValue *value)
00039 {
00040 VList::Iterator i(list);
00041 while (*i)
00042 {
00043 if (((Map*)(*i))->value == value)
00044 return &((Map*)(*i))->key;
00045 ++i;
00046 }
00047 return 0;
00048 }
00049
00050 TypeValue *operator [](const TypeKey &key)
00051 {
00052 VList::Iterator i(find(key));
00053 if (*i) return ((Map*)*i)->value;
00054 return 0;
00055 }
00056
00057 void remove(const TypeKey &key)
00058 {
00059 VList::Iterator i(find(key));
00060 if (*i)
00061 {
00062 delete (Map*)*i;
00063 i.remove();
00064 }
00065 }
00066
00067 List<const TypeKey> keys() const
00068 {
00069 List<const TypeKey> k;
00070 VList::Iterator i(list);
00071 while (*i)
00072 {
00073 k.append(&((Map*)(*i)->key));
00074 }
00075 return k;
00076 }
00077
00078 List<TypeKey> keys()
00079 {
00080 List<TypeKey> k;
00081 VList::Iterator i(list);
00082 while (*i)
00083 {
00084 k.append(&((Map*)(*i)->key));
00085 }
00086 return k;
00087 }
00088
00089 private:
00090 VList::Iterator find(const TypeKey &key) const
00091 {
00092 VList::Iterator i(list);
00093 while (*i)
00094 {
00095 if (((Map*)*i)->key == key)
00096 break;
00097 ++i;
00098 }
00099 return i;
00100 }
00101
00102 };
00103
00104 }
00105
00106 #endif