Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

Fl_Map.h

00001 /*
00002  * $Id: Fl_Map.h,v 1.6 2003/07/03 20:36:35 laza2000 Exp $
00003  *
00004  * Extended Fast Light Toolkit (EFLTK)
00005  * Copyright (C) 2002-2003 by EDE-Team
00006  * WWW: http://www.sourceforge.net/projects/ede
00007  *
00008  * Fast Light Toolkit (FLTK)
00009  * Copyright (C) 1998-2003 by Bill Spitzak and others.
00010  * WWW: http://www.fltk.org
00011  *
00012  * This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00013  * version 2. See COPYING for details.
00014  *
00015  * Author : Mikko Lahteenmaki
00016  * Email  : mikko@fltk.net
00017  *
00018  * Please report all bugs and problems to "efltk-bugs@fltk.net"
00019  *
00020  */
00021 
00022 #ifndef _FL_MAP_H_
00023 #define _FL_MAP_H_
00024 
00025 #include "Fl_String.h"
00026 #include "Fl_Ptr_List.h"
00027 
00028 #define HASH_LOOKUP_SMALL  17
00029 #define HASH_LOOKUP_MEDIUM 101
00030 #define HASH_LOOKUP_BIG    1007
00031 #define HASH_LOOKUP_HUGE   10017
00032 
00033 #define HASH_LOOKUP_DEFAULT HASH_LOOKUP_SMALL
00034 
00035 class Fl_Map;
00036 
00038 
00039 class Fl_Map_Iterator {
00040     friend class Fl_Map;
00041 public:
00042     Fl_Map_Iterator(const Fl_Map &map);
00043     Fl_Map_Iterator(const Fl_Map_Iterator &i) { m_map = i.m_map; m_x = i.m_x; m_y = i.m_y; }
00044 
00045     bool is_valid();
00046 
00047     bool operator == (const Fl_Map_Iterator &i) const { return (get_pair()==i.get_pair()); }
00048     bool operator != (const Fl_Map_Iterator &i) const { return (get_pair()!=i.get_pair()); }
00049 
00050     Fl_Map_Iterator &first();
00051     Fl_Map_Iterator &last();
00052     Fl_Map_Iterator &next();
00053     Fl_Map_Iterator &prior();
00054 
00055 protected:
00056     void *get_pair() const;
00057 
00058     const Fl_Map *m_map;
00059     int m_x, m_y;
00060 };
00061 
00063 class FL_API Fl_Map {
00064     friend class Fl_Map_Iterator;
00065 public:
00066     static unsigned hash_string( const char *key, int table_size );
00067     static unsigned hash_int( int key, int table_size );
00068 
00069     virtual void clear();
00070 
00071     unsigned hash_size() const { return m_lists_size; }
00072     unsigned size() const { return m_items; }
00073 
00074 protected:
00075     Fl_Map(int hash_size);
00076     virtual ~Fl_Map() { }
00077 
00078     unsigned insert_pair(int Y, void *pair);
00079     bool remove_pair(int Y, void *pair);
00080 
00081     virtual void free_pair(void *item) const = 0;
00082 
00083     unsigned m_items;     
00084     uint m_lists_size;    
00085     Fl_Ptr_List *m_lists; 
00086 };
00087 
00091 
00092 class Fl_String_String_Iterator;
00093 
00095 class FL_API Fl_String_String_Map : public Fl_Map {
00096 public:
00097     struct Pair { Fl_String id; Fl_String val; };
00098     typedef Fl_String_String_Iterator Iterator;
00099 
00100     static Pair *pair(const char *id, const char *val);
00101     static Pair *pair(const char *id, const Fl_String &val);
00102 
00103     Fl_String_String_Map(int hash_size=HASH_LOOKUP_DEFAULT) : Fl_Map(hash_size) { }
00104     virtual ~Fl_String_String_Map() { clear(); }
00105 
00106     bool contains(const char *id) const;
00107 
00108     bool insert(const char *id, const char *val);
00109     bool insert(const char *id, const Fl_String &val);
00110     bool remove(const char *id);
00111 
00112     void set_value(const char *id, const char *val);
00113     void set_value(const char *id, const Fl_String &val);
00114     Fl_String &get_value(const char *id) const;
00115 
00116     Fl_String &operator [](const char *id);
00117     const Fl_String &operator [](const char *id) const;
00118 
00119 protected:
00120     virtual void free_pair(void *item) const { delete (Pair*)(item); }
00121 
00122 private:
00123     Pair *find_pair(int Y, const char *id) const;
00124 };
00125 
00126 class Fl_String_String_Iterator : public Fl_Map_Iterator
00127 {
00128 public:
00129     Fl_String_String_Iterator(const Fl_Map &map) : Fl_Map_Iterator(map) { }
00130     Fl_String_String_Iterator(const Fl_Map_Iterator &i) : Fl_Map_Iterator(i) { }
00131 
00132     const Fl_String &id() const { Fl_String_String_Map::Pair *p=current_pair(); return (p ? p->id : Fl_String::null_object); }
00133     Fl_String &value() const    { Fl_String_String_Map::Pair *p=current_pair(); return (p ? p->val : Fl_String::null_object); }
00134 
00135     Fl_String_String_Map::Pair *operator*() const { return current_pair(); }
00136     Fl_String_String_Map::Pair *current()   const { return current_pair(); }
00137 
00138     Fl_String_String_Iterator& operator++()    { next(); return *this; }
00139     Fl_String_String_Iterator operator++(int) { Fl_String_String_Iterator tmp=*this; next(); return tmp; }
00140     Fl_String_String_Iterator& operator--()    { prior(); return *this; }
00141     Fl_String_String_Iterator operator--(int) { Fl_String_String_Iterator tmp=*this; prior(); return tmp; }
00142 
00143 private:
00144     Fl_String_String_Map::Pair *current_pair() const { return (Fl_String_String_Map::Pair*)get_pair(); }
00145 };
00146 
00150 
00151 class Fl_Int_String_Iterator;
00152 
00154 class Fl_Int_String_Map : public Fl_Map {
00155 public:
00156     struct Pair { int id; Fl_String val; };
00157     typedef Fl_Int_String_Iterator Iterator;
00158 
00159     static Pair *pair(int id, const char *val);
00160     static Pair *pair(int id, const Fl_String &val);
00161 
00162     Fl_Int_String_Map(int hash_size=HASH_LOOKUP_DEFAULT) : Fl_Map(hash_size) { }
00163     virtual ~Fl_Int_String_Map() { clear(); }
00164 
00165     bool contains(int id) const;
00166 
00167     bool insert(int id, const char *val);
00168     bool insert(int id, const Fl_String &val);
00169     bool remove(int id);
00170 
00171     void set_value(int id, const char *val);
00172     void set_value(int id, const Fl_String &val);
00173     Fl_String &get_value(int id) const;
00174 
00175     Fl_String &operator [](int id);
00176     const Fl_String &operator [](int id) const;
00177 
00178 protected:
00179     virtual void free_pair(void *item) const { delete (Pair*)(item); }
00180 
00181 private:
00182     Pair *find_pair(int Y, int id) const;
00183 };
00184 
00185 class Fl_Int_String_Iterator : public Fl_Map_Iterator
00186 {
00187 public:
00188     Fl_Int_String_Iterator(const Fl_Map &map) : Fl_Map_Iterator(map) { }
00189     Fl_Int_String_Iterator(const Fl_Map_Iterator &i) : Fl_Map_Iterator(i) { }
00190 
00191     int id() const { Fl_Int_String_Map::Pair *p=current_pair(); return (p ? p->id : -1); }
00192     Fl_String &value() const { Fl_Int_String_Map::Pair *p=current_pair(); return (p ? p->val : Fl_String::null_object); }
00193 
00194     Fl_Int_String_Map::Pair *operator*() const { return current_pair(); }
00195     Fl_Int_String_Map::Pair *current()   const { return current_pair(); }
00196 
00197     Fl_Int_String_Iterator& operator++()    { next(); return *this; }
00198     Fl_Int_String_Iterator& operator++(int) { next(); return *this; }
00199     Fl_Int_String_Iterator& operator--()    { prior(); return *this; }
00200     Fl_Int_String_Iterator& operator--(int) { prior(); return *this; }
00201 
00202 private:
00203     Fl_Int_String_Map::Pair *current_pair() const { return (Fl_Int_String_Map::Pair*)get_pair(); }
00204 };
00205 
00209 
00210 class Fl_String_Ptr_Iterator;
00211 
00213 class Fl_String_Ptr_Map : public Fl_Map {
00214 public:
00215     struct Pair { Fl_String id; void *val; };
00216     typedef Fl_String_Ptr_Iterator Iterator;
00217 
00218     static Pair *pair(const char *id, void *val);
00219 
00220     Fl_String_Ptr_Map(int hash_size=HASH_LOOKUP_DEFAULT) : Fl_Map(hash_size) { }
00221     virtual ~Fl_String_Ptr_Map() { clear(); }
00222 
00223     bool contains(const char *id) const;
00224 
00225     bool insert(const char *id, void *val);
00226     bool remove(const char *id);
00227 
00228     void set_value(const char *id, void *val);
00229     void *get_value(const char *id) const;
00230 
00231     void *operator [](const char *id);
00232     const void *operator [](const char *id) const;
00233 
00234 protected:
00235     virtual void free_pair(void *item) const { delete (Pair *)(item); }
00236 
00237 private:
00238     Pair *find_pair(int Y, const char *id) const;
00239 };
00240 
00241 class Fl_String_Ptr_Iterator : public Fl_Map_Iterator
00242 {
00243 public:
00244     Fl_String_Ptr_Iterator(const Fl_Map &map) : Fl_Map_Iterator(map) { }
00245     Fl_String_Ptr_Iterator(const Fl_Map_Iterator &i) : Fl_Map_Iterator(i) { }
00246 
00247     const Fl_String &id() const { Fl_String_Ptr_Map::Pair *p=current_pair(); return (p ? p->id : Fl_String::null_object); }
00248     void *value() const { Fl_String_Ptr_Map::Pair *p=current_pair(); return (p ? p->val : 0); }
00249 
00250     Fl_String_Ptr_Map::Pair    *operator*() const { return current_pair(); }
00251     Fl_String_Ptr_Map::Pair    *current()   const { return current_pair(); }
00252 
00253     Fl_String_Ptr_Iterator& operator++()    { next(); return *this; }
00254     Fl_String_Ptr_Iterator& operator++(int) { next(); return *this; }
00255     Fl_String_Ptr_Iterator& operator--()    { prior(); return *this; }
00256     Fl_String_Ptr_Iterator& operator--(int) { prior(); return *this; }
00257 private:
00258     Fl_String_Ptr_Map::Pair *current_pair() const { return (Fl_String_Ptr_Map::Pair*)get_pair(); }
00259 };
00260 
00264 
00265 class Fl_Int_Ptr_Iterator;
00266 
00268 class Fl_Int_Ptr_Map : public Fl_Map {
00269 public:
00270     struct Pair { int id; void *val; };
00271     typedef Fl_Int_Ptr_Iterator Iterator;
00272 
00273     static Pair *pair(int id, void *val);
00274 
00275     Fl_Int_Ptr_Map(int hash_size=HASH_LOOKUP_DEFAULT) : Fl_Map(hash_size) { }
00276     virtual ~Fl_Int_Ptr_Map() { clear(); }
00277 
00278     bool contains(int id) const;
00279 
00280     bool insert(int id, void *val);
00281     bool remove(int id);
00282 
00283     void set_value(int id, void *val);
00284     void *get_value(int id) const;
00285 
00286     void *operator [](int id);
00287     const void *operator [](int id) const;
00288 
00289 protected:
00290     virtual void free_pair(void *item) const { delete (Pair *)(item); }
00291 
00292 private:
00293     Pair *find_pair(int Y, int id) const;
00294 };
00295 
00296 class Fl_Int_Ptr_Iterator : public Fl_Map_Iterator
00297 {
00298 public:
00299     Fl_Int_Ptr_Iterator(const Fl_Map &map) : Fl_Map_Iterator(map) { }
00300     Fl_Int_Ptr_Iterator(const Fl_Map_Iterator &i) : Fl_Map_Iterator(i) { }
00301 
00302     int id() const { Fl_Int_Ptr_Map::Pair *p=current_pair(); return (p ? p->id : -1); }
00303     void *value() const { Fl_Int_Ptr_Map::Pair *p=current_pair(); return (p ? p->val : 0); }
00304 
00305     Fl_Int_Ptr_Map::Pair    *operator*() const { return current_pair(); }
00306     Fl_Int_Ptr_Map::Pair    *current()   const { return current_pair(); }
00307 
00308     Fl_Int_Ptr_Iterator& operator++()    { next(); return *this; }
00309     Fl_Int_Ptr_Iterator& operator++(int) { next(); return *this; }
00310     Fl_Int_Ptr_Iterator& operator--()    { prior(); return *this; }
00311     Fl_Int_Ptr_Iterator& operator--(int) { prior(); return *this; }
00312 private:
00313     Fl_Int_Ptr_Map::Pair *current_pair() const { return (Fl_Int_Ptr_Map::Pair*)get_pair(); }
00314 };
00315 
00316 #endif

Generated on Thu Jul 31 15:33:44 2003 for eFLTK by doxygen1.2.15