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

hash.H

Go to the documentation of this file.
00001 /*
00002  * Copyright 1997, Brown University, Providence, RI.
00003  * 
00004  *                         All Rights Reserved
00005  * 
00006  * Permission to use, copy, modify, and distribute this software and its
00007  * documentation for any purpose other than its incorporation into a
00008  * commercial product is hereby granted without fee, provided that the
00009  * above copyright notice appear in all copies and that both that
00010  * copyright notice and this permission notice appear in supporting
00011  * documentation, and that the name of Brown University not be used in
00012  * advertising or publicity pertaining to distribution of the software
00013  * without specific, written prior permission.
00014  * 
00015  * BROWN UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
00016  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
00017  * PARTICULAR PURPOSE.  IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE FOR
00018  * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00019  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00020  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00021  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00022  */
00023 #ifndef HASH_NODE_INCLUDED
00024 #define HASH_NODE_INCLUDED
00025 #include "std/dllimpexp.H"
00026 #include "std/thread_mutex.H"
00027 #include "std/config.H"
00028 
00029 template <class T>
00030 class hash_node {
00031     private:
00032     long          _key;
00033     T             _data;
00034     hash_node<T> *_next;
00035     public:
00036         hash_node(long key, T value, hash_node<T> *next)
00037         {_key = key; _data = value; _next = next;}
00038     hash_node(const hash_node<T> &old)
00039         {_key = old._key; _data = old._data;
00040          _next = old._next ? new hash_node<T>(*old._next) : 0;}
00041         hash_node() {_key = 0; _data = T(); _next = 0;}
00042 
00043         hash_node<T> *next() const          { return _next; }
00044         T             data() const          { return _data; }
00045         long          key()  const          { return _key; }
00046 
00047         void          next(hash_node<T> *n) { _next = n; }
00048         void          data(T data)          { _data = data; }
00049         T            &data_ptr()            { return _data; }
00050 };
00051 
00052 #define cHASH const HASH
00053 template <class T>
00054 class ARRAY;
00055 
00056 template <class T>
00057 class HASH {
00058     private:
00059     int            _size;
00060         hash_node<T> **_table;
00061     int            _mask;
00062     int            _seq_val;
00063     int            _lastval;
00064     hash_node<T>  **table()              const { return _table; }
00065     int             hash(const long key) const;
00066     long            hash(const char *)   const;
00067     int             next_seq(long &key, T &data, hash_node<T> *&seq_elt,
00068                         int &seq_val)        const; // Returns 0 if done
00069         ThreadMutex mutex;
00070 
00071     public:
00072         HASH(int size);
00073     HASH(const HASH<T> &hash_table);
00074     ~HASH();
00075     void clear();
00076     // Add - returns 0 if doesn't already exist, 1 if it does
00077     int    add(long key, T data);
00078     int    add(const char *key, T data) {char *tmp; return add(key, data, tmp, 0);}
00079     int    add(const char *key, T data, char *&loc, int create_new=1);
00080     int    del(long key);
00081     int    del(char *key);
00082     T      find(long key) const {T *data = find_addr(key); return data ? *data : T();}
00083     T      find(char *key) const {T *data = find_addr(key); return data ? *data : T();};
00084     T     *find_addr(long key) const;
00085     T     *find_addr(const char *key) const;
00086     int    bfind(long key, T &data) const;
00087 
00088         void   get_items(ARRAY<long> &keys, ARRAY<T> &data) const;
00089 };
00090 #ifdef GLUE_NEEDS_TEMPLATES_IN_H_FILE
00091 #include "hash.C"
00092 #endif
00093 #endif

Generated on Mon Sep 15 16:25:56 2003 for gluebase by doxygen1.2.18