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

StreamHasher.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 : StreamHasher.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: StreamHasher.h,v 1.3 2004/02/16 14:09:12 adavid Exp $
00011 //
00012 ///////////////////////////////////////////////////////////////////
00013 
00014 #ifndef INCLUDE_HASH_STREAMHASHER_H
00015 #define INCLUDE_HASH_STREAMHASHER_H
00016 
00017 #include "base/inttypes.h"
00018 
00019 namespace hash
00020 {
00021 
00022     /** Hasher class to hash on generic streams of data.
00023      * WARNING: approx 5x slower than the hash_compute functions.
00024      */
00025     class StreamHasher
00026     {
00027     public:
00028 
00029         /** Constructor.
00030          * @param initVal: initial value
00031          * for the hasher.
00032          */
00033         StreamHasher(uint32_t initVal)
00034             : hashValue(initVal)
00035         {}
00036 
00037 
00038         /** Add a value to the hash.
00039          * @param value: value to hash.
00040          */
00041         void addValue(uint32_t value)
00042         {
00043             hashValue = (hashValue ^ value) +
00044                 ((hashValue << 26) + (hashValue >> 6));
00045         }
00046 
00047         
00048         /** Wrapper for addValue.
00049          * @param value: int to wrap to uint.
00050          */
00051         void addValue(int32_t value)
00052         {
00053             addValue((uint32_t)value);
00054         }
00055 
00056 
00057         /** Computes hash.
00058          * @return computed hash value.
00059          */
00060         uint32_t hash()
00061         {
00062             return hashValue;
00063         }
00064 
00065 
00066     private:
00067         uint32_t hashValue; /**< current hash value */
00068     };
00069 
00070 } // namespace hash
00071 
00072 #endif // INCLUDE_HASH_STREAMHASHER_H

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