UserDataHT.h

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021    Author: Eric Viara <viara@sysra.com>
00022 */
00023 
00024 
00025 #ifndef _EYEDB_USER_DATA_HT_H
00026 #define _EYEDB_USER_DATA_HT_H
00027 
00028 namespace eyedb {
00029 
00030   class UserDataHT {
00031 
00032     unsigned int nkeys;
00033     unsigned int mask;
00034     LinkedList **lists;
00035 
00036     int get_key(const char *s) {
00037       int len = strlen(s);
00038       int k = 0;
00039 
00040       for (int i = 0; i < len; i++)
00041         k += *s++;
00042 
00043       return k & mask;
00044     }
00045 
00046     struct Link {
00047       char *s;
00048       void *x;
00049 
00050       Link(const char *_s, void *_x) : s(strdup(_s)), x(_x) {}
00051 
00052       ~Link() {
00053         free(s);
00054       }
00055     };
00056 
00057   public:
00058     UserDataHT() {
00059       nkeys = 64;
00060       mask = nkeys - 1;
00061 
00062       lists = (LinkedList **)malloc(sizeof(LinkedList *) * nkeys);
00063       memset(lists, 0, sizeof(LinkedList *)*nkeys);
00064     }
00065 
00066     void insert(const char *s, void *x) {
00067       Link *l = new Link(s, x);
00068       int k = get_key(s);
00069   
00070       if (!lists[k])
00071         lists[k] = new LinkedList();
00072 
00073       lists[k]->insertObjectLast(l);
00074     }
00075 
00076     void getall(LinkedList *&key_list, LinkedList *&data_list) {
00077       key_list = new LinkedList();
00078       data_list = new LinkedList();
00079       for (int i = 0; i < nkeys; i++) {
00080         LinkedList *list = lists[i];
00081         if (list) {
00082           Link *l;
00083           LinkedListCursor c(list);
00084           while (c.getNext((void *&)l)) {
00085             key_list->insertObjectLast(l->s);
00086             data_list->insertObjectLast(l->x);
00087           }
00088         }
00089       }
00090     }
00091 
00092     void display(FILE *fd = stdout) {
00093       fprintf(fd, "HashTable KEYS %d\n", nkeys);
00094       for (int i = 0; i < nkeys; i++)
00095         if (lists[i])
00096           fprintf(fd, "lists[%d] = %d\n", i, lists[i]->getCount());
00097     }
00098 
00099     void *get(const char *s) {
00100       LinkedList *list;
00101 
00102       if (list = lists[get_key(s)])
00103         {
00104           Link *l;
00105           LinkedListCursor c(list);
00106           while (c.getNext((void *&)l))
00107             if (!strcmp(l->s, s))
00108               return l->x;
00109         }
00110     
00111       return 0;
00112     }
00113 
00114     ~UserDataHT() {
00115       for (int i = 0; i < nkeys; i++)
00116         {
00117           LinkedList *list = lists[i];
00118           if (list)
00119             {
00120               Link *l;
00121               LinkedListCursor c(list);
00122               while (c.getNext((void *&)l))
00123                 delete l;
00124             
00125               delete list;
00126             }
00127         }
00128     
00129       free(lists);
00130     }
00131   };
00132 
00133 }
00134 
00135 #endif

Generated on Mon Dec 22 18:16:11 2008 for eyedb by  doxygen 1.5.3