00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
#ifndef KDICT_H
00015
#define KDICT_H
00016
00017
#include <iostream>
00018
#include <kdebug.h>
00019
00020
#include "Trie.h"
00021
template <
typename type>
00022 class KDict :
private Structure::
Trie_Impl<char, QString, type*>
00023 {
00024
public:
00025 typedef Structure::Trie_Impl<char, QString, type*>
Impl;
00026
00027 KDict(
bool cs =
true) :
Impl(' '),
case_sensitive(cs),
auto_delete(false) {
00029 component = 0;
00030 }
00031 KDict(
int sz,
bool cs =
true) :
Impl(' '),
case_sensitive(cs),
auto_delete(false) {
00032
00033
depreceated(
"KDict(int sz,...)");
00034 component = 0;
00035 }
00036 KDict (
const KDict<type> & dict ) {
00037 kDebug() <<
"KDict: COPY CONSTRUCTOR NOT IMPLEMENTED!" <<
endl;
00038 component = 0;
00039 }
00040
00041 struct Delete_Pointer {
00042 void operator() (
Impl* node) {
00043
kdDebug() <<
"KDict: deleting pointer " << node->component;
00044 assert(node->component);
00045
delete node->component;
00046 }
00047 };
00048
00049 ~KDict() {
00050
if (
auto_delete) {
00051
Delete_Pointer fun;
00052 Impl::apply(fun);
00053 }
00054 }
00055
00056
00057 unsigned int count()
const {
00058
return num_components();
00059 }
00060 bool isEmpty ()
const {
00061
return size()==0;
00062 }
00063
00064 void insert (
const QString & key,
const type * item ) {
00065
QString str = key;
00066
Structure::add_prefix(str,
' ');
00067 Impl::insert(const_cast<const QString &>(str), (type*) item);
00068
00069 }
00070
00071 void replace (
const QString & key,
const type * item ) {
00072
if (
remove(key))
00073 insert(const_cast<const QString &>(key), (type*)item);
00074 }
00075
00076 bool remove (
const QString & key ) {
00077
QString str = key;
00078
Structure::add_prefix(str,
' ');
00079
return Impl::remove(const_cast<const QString &>(str))!=0;
00080 }
00081
00082 type*
find (
const QString & key ) {
00083
QString str = key;
00084
Structure::add_prefix(str,
' ');
00085
return Impl::query(const_cast<const QString &>(str))->component;
00086 }
00087
00088 type* operator[] (
const QString & key )
const {
00089
return Impl::query(key);
00090 }
00091
00092
00093
00094 type*
take (
const QString & key ) {
00095 type* elt = find(key);
00096 remove(key);
00097 }
00098 void resize ( uint newsize ) {
00099
00100
depreceated(
"resize");
00101 }
00102 uint
size ()
const {
00103
return num_components();
00104 }
00105 std::ostream&
print(std::ostream& out)
const {
return Impl::print(out); }
00106
00107 bool autoDelete ()
const {
00108
return auto_delete;
00109 }
00110 void setAutoDelete (
bool enable ) {
00111
auto_delete = enable;
00112 }
00113
00114
private:
00115 bool case_sensitive;
00116 bool auto_delete;
00117 void depreceated(
const char* f)
const {
00118
kdDebug() <<
"WARNING: KDict::" << f <<
" is depreceated!" <<
endl;
00119 }
00120
00121 };
00122
00123
template <
typename T>
00124 std::ostream& operator << (std::ostream& out, const KDict<T>& dict)
00125 {
00126
return dict.print(out);
00127 }
00128
00129
#endif