~vanvugt/ubuntu/oneiric/mediatomb/fix-770964-784431

« back to all changes in this revision

Viewing changes to src/hash/dbb_hash.h

  • Committer: Bazaar Package Importer
  • Author(s): Andres Mejia
  • Date: 2008-02-02 01:42:48 UTC
  • Revision ID: james.westby@ubuntu.com-20080202014248-cjouolddb8gi2zkz
Tags: upstream-0.10.0.dfsg1
ImportĀ upstreamĀ versionĀ 0.10.0.dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*MT*
 
2
    
 
3
    MediaTomb - http://www.mediatomb.cc/
 
4
    
 
5
    dbb_hash.h - this file is part of MediaTomb.
 
6
    
 
7
    Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
 
8
                       Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
 
9
    
 
10
    Copyright (C) 2006-2007 Gena Batyan <bgeradz@mediatomb.cc>,
 
11
                            Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
 
12
                            Leonhard Wimmer <leo@mediatomb.cc>
 
13
    
 
14
    MediaTomb is free software; you can redistribute it and/or modify
 
15
    it under the terms of the GNU General Public License version 2
 
16
    as published by the Free Software Foundation.
 
17
    
 
18
    MediaTomb is distributed in the hope that it will be useful,
 
19
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
    GNU General Public License for more details.
 
22
    
 
23
    You should have received a copy of the GNU General Public License
 
24
    version 2 along with MediaTomb; if not, write to the Free Software
 
25
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 
26
    
 
27
    $Id: dbb_hash.h 1343 2007-06-04 14:06:56Z lww $
 
28
*/
 
29
 
 
30
/// \file dbb_hash.h
 
31
 
 
32
#ifndef __HASH_DBB_HASH_H__
 
33
#define __HASH_DBB_HASH_H__
 
34
 
 
35
#include "direct_hash_base.h"
 
36
 
 
37
template <typename KT, typename VT> struct dbb_hash_slot
 
38
{
 
39
    KT key;
 
40
    VT value;
 
41
};
 
42
 
 
43
/// \brief Direct hash with base type keys and base type values.
 
44
template <typename KT, typename VT>
 
45
class DBBHash : public DHashBase<KT, struct dbb_hash_slot<KT, VT> >
 
46
{
 
47
protected:
 
48
    KT emptyKey;
 
49
public:
 
50
    DBBHash(int capacity, KT emptyKey) : DHashBase<KT, struct dbb_hash_slot<KT, VT> >(capacity)
 
51
    {
 
52
        this->emptyKey = emptyKey;
 
53
        clear();
 
54
    }
 
55
    
 
56
    /* virtual methods */
 
57
    virtual int hashCode(KT key)
 
58
    {
 
59
        return this->baseTypeHashCode((unsigned int)key);
 
60
    }
 
61
    virtual bool match(KT key, struct dbb_hash_slot<KT, VT> *slot)
 
62
    {
 
63
        return (key == slot->key);
 
64
    }
 
65
    virtual bool isEmptySlot(struct dbb_hash_slot<KT, VT> *slot)
 
66
    {
 
67
        return (slot->key == emptyKey);
 
68
    }
 
69
 
 
70
    void clear()
 
71
    {
 
72
        if (! emptyKey)
 
73
            this->zero();
 
74
        else
 
75
        {
 
76
            for (int i = 0; i < this->capacity; i++)
 
77
                this->data[i].key = emptyKey;
 
78
            this->count = 0;
 
79
        }
 
80
    }
 
81
    
 
82
    /* need to use deleted key
 
83
    inline bool remove(KT key)
 
84
    {
 
85
        struct dbb_hash_slot<KT, VT> *slot;
 
86
        if (! search(key, &slot))
 
87
            return false;
 
88
        slot->key = emptyKey;
 
89
        this->count--;
 
90
        return true;
 
91
    }
 
92
    */
 
93
    
 
94
    inline void put(KT key, VT value)
 
95
    {
 
96
        struct dbb_hash_slot<KT, VT> *slot;
 
97
        bool found = search(key, &slot);
 
98
        if (! found)
 
99
        {
 
100
            slot->key = key;
 
101
            this->count++;
 
102
        }
 
103
        slot->value = value;
 
104
    }
 
105
    inline void put(KT key, hash_slot_t destSlot, VT value)
 
106
    {
 
107
        struct dbb_hash_slot<KT, VT> *slot = (struct dbb_hash_slot<KT, VT> *)destSlot;
 
108
        if (slot->key == emptyKey)
 
109
        {
 
110
            slot->key = key;
 
111
            this->count++;
 
112
        }
 
113
        slot->value = value;
 
114
    }
 
115
 
 
116
    inline bool get(KT key, VT *value)
 
117
    {
 
118
        struct dbb_hash_slot<KT, VT> *slot;
 
119
        bool found = search(key, &slot);
 
120
        if (found)
 
121
            *value = slot->value;
 
122
        return found;
 
123
    }
 
124
    bool get(KT key, hash_slot_t *destSlot, VT *value)
 
125
    {
 
126
        struct dbb_hash_slot<KT, VT> **slot = (struct dbb_hash_slot<KT, VT> **)destSlot;
 
127
        bool found = search(key, slot);
 
128
        if (found)
 
129
            *value = (*slot)->value;
 
130
        return found;
 
131
    }
 
132
    inline bool exists(KT key)
 
133
    {
 
134
        struct dbb_hash_slot<KT, VT> *slot;
 
135
        return search(key, &slot);
 
136
    }
 
137
    inline bool exists(KT key, hash_slot_t *destSlot)
 
138
    {
 
139
        return search(key, (struct dbb_hash_slot<KT, VT> **)destSlot);
 
140
    }
 
141
};
 
142
 
 
143
#endif // __HASH_DBB_HASH_H__