Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

itemtables.h

Go to the documentation of this file.
00001 // -*- mode: C++; c-file-style: "stroustrup"; c-basic-offset: 4; indent-tabs-mode: nil; -*-
00002 ////////////////////////////////////////////////////////////////////
00003 //
00004 // Filename : itemtables.h (hash)
00005 //
00006 // This file is a part of the UPPAAL toolkit.
00007 // Copyright (c) 1995 - 2003, Uppsala University and Aalborg University.
00008 // All right reserved.
00009 //
00010 // $Id: itemtables.h,v 1.3 2005/04/21 13:39:04 behrmann Exp $
00011 //
00012 ///////////////////////////////////////////////////////////////////
00013 
00014 #ifndef INCLUDE_HASH_ITEMTABLES_H
00015 #define INCLUDE_HASH_ITEMTABLES_H
00016 
00017 #include "hash/tables.h"
00018 
00019 /**
00020  * @file
00021  * Hash tables for items of known sizes, right now only ItemTableSingle.
00022  */
00023     
00024 namespace hash
00025 {
00026     /** Bucket for a specific item type. Singly linked.
00027      */
00028     template<class ItemType>
00029     struct ItemBucketSingle :
00030         public TableSingle<ItemBucketSingle<ItemType> >::Bucket_t
00031     {
00032         ItemType item;
00033     };
00034 
00035 
00036     /** Hash table for singly linked buckets with items
00037      * of fixed size. info is used to store the hash value.
00038      */
00039     template<class ItemType>
00040     class ItemTableSingle :
00041         public TableSingle<ItemBucketSingle<ItemType> >
00042     {
00043     public:
00044 
00045         /** @see TableSingle
00046          */
00047         ItemTableSingle(uint32_t sizePower2, bool aggressive)
00048             : TableSingle<ItemBucketSingle<ItemType> >
00049               (sizePower2, aggressive)
00050             {}
00051 
00052 
00053         /** @return true if the hash table has a given
00054          * item. ItemType needs the == operator.
00055          * @param item: item to test
00056          * @param hashValue: hash value for the item.
00057          */
00058         bool hasItem(const ItemType& item, uint32_t hashValue)
00059         {
00060             ItemBucketSingle<ItemType> **entry = TableSingle<ItemBucketSingle<ItemType> >::getAtBucket(hashValue);
00061             ItemBucketSingle<ItemType> *bucket = *entry;
00062             while(bucket)
00063             {
00064                 if (bucket->info == hashValue &&
00065                     bucket->item == item)
00066                 {
00067                     return true;
00068                 }
00069                 bucket = bucket->getNext();
00070             }
00071             return false;
00072         }
00073         
00074 
00075         /** Add an item to the hash table.
00076          * @return added bucket or existing bucket that
00077          * contains this item. ItemType needs the ==
00078          * and the = operators.
00079          * @param item: item to add to the hash table.
00080          * @param hashValue: hash associated to the item.
00081          * @param alloc: an allocator having a allocate(uint)
00082          * to allocate in int units.
00083          */
00084         template<class Alloc>
00085         ItemBucketSingle<ItemType>* addItem(const ItemType& item, uint32_t hashValue,
00086                                             Alloc *alloc)
00087         {
00088             ItemBucketSingle<ItemType> **entry = TableSingle<ItemBucketSingle<ItemType> >::getAtBucket(hashValue);
00089             ItemBucketSingle<ItemType> *bucket = *entry;
00090             while(bucket)
00091             {
00092                 if (bucket->info == hashValue &&
00093                     bucket->item == item)
00094                 {
00095                     return bucket;
00096                 }
00097                 bucket = bucket->getNext();
00098             }
00099             bucket = (ItemBucketSingle<ItemType>*)
00100                 alloc->allocate(intsizeof(ItemBucketSingle<ItemType>));
00101             bucket->link(entry);
00102             bucket->info = hashValue;
00103             bucket->item = item;
00104             TableSingle<ItemBucketSingle<ItemType> >::incBuckets();
00105             return bucket;
00106         }
00107 
00108 
00109         /** Add an item to the hash table.
00110          * @return added bucket or existing bucket that
00111          * contains this item. ItemType needs the ==
00112          * and the = operators.
00113          * @param item: item to add to the hash table.
00114          * @param hashValue: hash associated to the item.
00115          * NOTE: bucket is allocated with new so you need
00116          * to call resetDelete() to deallocate all properly.
00117          */
00118         ItemBucketSingle<ItemType>* addNewItem(const ItemType& item, uint32_t hashValue)
00119         {
00120             ItemBucketSingle<ItemType> **entry = TableSingle<ItemBucketSingle<ItemType> >::getAtBucket(hashValue);
00121             ItemBucketSingle<ItemType> *bucket = *entry;
00122             while(bucket)
00123             {
00124                 if (bucket->info == hashValue &&
00125                     bucket->item == item)
00126                 {
00127                     return bucket;
00128                 }
00129                 bucket = bucket->getNext();
00130             }
00131             bucket = new ItemBucketSingle<ItemType>;
00132             bucket->link(entry);
00133             bucket->info = hashValue;
00134             bucket->item = item;
00135             TableSingle<ItemBucketSingle<ItemType> >::incBuckets();
00136             return bucket;
00137         }
00138 
00139     };
00140 
00141 } // namespace hash
00142 
00143 #endif // INCLUDE_HASH_ITEMTABLES_H

Generated on Fri Jun 30 00:03:00 2006 for Module hash by  doxygen 1.4.2