00001 // hash.h 00002 00003 #ifndef HASH_HEADER 00004 #define HASH_HEADER 00005 00006 // Clipper 00007 #include <clipper/clipper.h> 00008 using clipper::Message; 00009 using clipper::Message_fatal; 00010 00011 class hash_table 00012 // Simple hash table for pairs of ints 00013 // Nfind "index" number stored with Nstore 00014 // Nstore number to be used to retrieve Nfind (must be > 0) 00015 // 00016 // Typically Nstore is a randomly large-range positive integer 00017 // (eg batch number) and Nfind is a running index (eg 0->n-1) 00018 // hash_table.add(Nstore, Nfind) adds a pair to the list 00019 // hash_table.lookup(Nstore) retrieves the index number 00020 // belonging to Nstore 00021 // 00022 // 00023 // Code translated from Fortran functions ccp4_hash_setup/lookup/zeroit 00024 // This is a simple-minded hash function suitable for batch-number hashing 00025 // but not necessarily for other purposes 00026 // 00027 00028 // hash_table.add(Nstore, Nfind) sets up a value so that when 00029 // hash_table.lookup(Nstore) is later evaluated it will return Nfind. This 00030 // function will allow the efficient retrieval of an identifier for a 00031 // large range variable (such as a batch number). The values of the 00032 // function hash_table.lookup(Nstore) are stored in the arrays Nstore_list, 00033 // Nfind_list of length table_size, which is the prime number used to 00034 // generate the function. 00035 // 00036 // NOTES: A hash table is a way of storing information so that it easily 00037 // be retrieved without the need for indexing or long searches. Nstore is 00038 // referred to as the "key", which is "hashed" (computer- science speak 00039 // for "messed up") by the hashing function (in this case (Nstore % 00040 // table_size) to determine where the value pair will be stored. The 00041 // function lookup can then search on the same basis when supplied with 00042 // the key, to retreive the pair in (at most) 3 calculations. Note that 00043 // the table size MUST BE A PRIME in order for this method to work. 00044 // 00045 00046 00047 { 00048 public: 00049 00050 // Constructor:- 00051 // hash_table() dummy, must be followed by a 00052 // call to hash_table.set_size 00053 // hash_table(const int& size) the table will be the next biggest prime 00054 // larger than size 00055 hash_table() {table_size = 0;} 00056 hash_table(const int& size); 00057 00058 void set_size(const int& size); 00059 // get size 00060 int Size() const {return table_size;} 00061 00062 // Add pair to list 00063 // Typically Nstore is a randomly large-range positive integer 00064 // (eg batch number) and Nfind is a running index (eg 0->n-1) 00065 void add(const int& Nstore, const int& Nfind); 00066 00067 // Return index Nstore for stored number 00068 // -1 if not found 00069 int lookup(const int& Nstore) const; 00070 // Returns actual stored number for index 00071 // = -1 if out of range 00072 int number(const int& index) const; 00073 00074 // Returns smallest prime <= argument 00075 static int prime(const int& min_value); 00076 00077 void Clear() {setup();} 00078 00079 private: 00080 static const int MinPrime; // this hash method doesn't work well 00081 // on small numbers so give a minimum value 00082 00083 int table_size; 00084 std::vector<int> Nstore_list; 00085 std::vector<int> Nfind_list; 00086 00087 void setup(); 00088 }; 00089 00090 #endif
1.6.3