File: google_hash.cpp

package info (click to toggle)
freecell-solver 5.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,256 kB
  • sloc: ansic: 28,700; perl: 10,050; xml: 5,600; python: 1,339; sh: 533; cpp: 275; makefile: 20; javascript: 8
file content (221 lines) | stat: -rw-r--r-- 6,248 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * This file is part of Freecell Solver. It is subject to the license terms in
 * the COPYING.txt file found in the top-level directory of this distribution
 * and at http://fc-solve.shlomifish.org/docs/distro/COPYING.html . No part of
 * Freecell Solver, including this file, may be copied, modified, propagated,
 * or distributed except according to the terms contained in the COPYING file.
 *
 * Copyright (c) 2010 Shlomi Fish
 */
// google_hash.cpp - module file for Google's dense_hash_map as adapted
// for Freecell Solver.
#include "google_hash.h"

#if ((FCS_WHICH_STATES_GOOGLE_HASH == FCS_WHICH_STATES_GOOGLE_HASH__DENSE) ||  \
     (FCS_WHICH_COLUMNS_GOOGLE_HASH == FCS_WHICH_COLUMNS_GOOGLE_HASH__DENSE))
#include <google/dense_hash_set>
#endif

#if ((FCS_WHICH_STATES_GOOGLE_HASH == FCS_WHICH_STATES_GOOGLE_HASH__SPARSE) || \
     (FCS_WHICH_COLUMNS_GOOGLE_HASH == FCS_WHICH_COLUMNS_GOOGLE_HASH__SPARSE))
#include <google/sparse_hash_set>
#endif

#include "state.h"

#if ((FCS_WHICH_STATES_GOOGLE_HASH == FCS_WHICH_STATES_GOOGLE_HASH__DENSE) ||  \
     (FCS_WHICH_COLUMNS_GOOGLE_HASH == FCS_WHICH_COLUMNS_GOOGLE_HASH__DENSE))
using google::dense_hash_set; // namespace where class lives by default
#endif

#if ((FCS_WHICH_STATES_GOOGLE_HASH == FCS_WHICH_STATES_GOOGLE_HASH__SPARSE) || \
     (FCS_WHICH_COLUMNS_GOOGLE_HASH == FCS_WHICH_COLUMNS_GOOGLE_HASH__SPARSE))
using google::sparse_hash_set; // namespace where class lives by default
#endif

typedef unsigned long int ub4; /* unsigned 4-byte quantities */
typedef unsigned char ub1;

static inline ub4 perl_hash_function(register const ub1 *s_ptr, /* the key */
    register const ub4 length /* the length of the key */
)
{
    register ub4 hash_value_int = 0;
    register const ub1 *s_end = s_ptr + length;

    while (s_ptr < s_end)
    {
        hash_value_int += (hash_value_int << 5) + *(s_ptr++);
    }
    hash_value_int += (hash_value_int >> 5);

    return hash_value_int;
}

#if (FCS_STATE_STORAGE == FCS_STATE_STORAGE_GOOGLE_DENSE_HASH)

struct state_equality
{
    bool operator()(const char *s1, const char *s2) const
    {
        return (s1 == s2) ||
               (s1 && s2 && (fc_solve_state_compare(s1, s2) == 0));
    }
};

struct state_hash
{
    int operator()(const char *s1) const
    {
        return perl_hash_function((const ub1 *)s1, sizeof(fcs_state));
    }
};

#if (FCS_WHICH_STATES_GOOGLE_HASH == FCS_WHICH_STATES_GOOGLE_HASH__SPARSE)
typedef sparse_hash_set<char *, state_hash, state_equality> StatesGoogleHash;
#else
typedef dense_hash_set<char *, state_hash, state_equality> StatesGoogleHash;
#endif

extern "C" fcs_states_google_hash_handle fc_solve_states_google_hash_new()
{
    StatesGoogleHash *ret = new StatesGoogleHash;

#if (FCS_WHICH_STATES_GOOGLE_HASH == FCS_WHICH_STATES_GOOGLE_HASH__DENSE)
    ret->set_empty_key(NULL);
#endif

    return (fcs_states_google_hash_handle)(ret);
}

/*
 * Returns 0 if the key is new and the key/val pair was inserted.
 *      - in that case *existing_key / *existing_val will be set to key
 *      and val respectively.
 * Returns 1 if the key is not new and *existing_key / *existing_val
 * was set to it.
 */
extern "C" bool fc_solve_states_google_hash_insert(
    fcs_states_google_hash_handle void_hash, void *key, void **existing_key)
{
    StatesGoogleHash *hash = (StatesGoogleHash *)void_hash;
    std::pair<StatesGoogleHash::iterator, bool> result =
        hash->insert((char *)key);

    /* If an insertion took place. */
    if (result.second)
    {
        *existing_key = NULL;
        return FALSE;
    }
    else
    {
        *existing_key = (*(result.first));

        return TRUE;
    }
}

extern "C" void fc_solve_states_google_hash_free(
    fcs_states_google_hash_handle void_hash)
{
    StatesGoogleHash *hash = (StatesGoogleHash *)void_hash;

    delete hash;

    return;
}

extern void fc_solve_states_google_hash_foreach(
    fcs_states_google_hash_handle void_hash,
    bool (*should_delete_ptr)(void *key, void *context), void *context)
{
    StatesGoogleHash *hash = (StatesGoogleHash *)void_hash;

    for (StatesGoogleHash::iterator it = hash->begin(); it != hash->end(); ++it)
    {
        if (should_delete_ptr(*(it), context))
        {
            hash->erase(it);
        }
    }
}

#endif

#if (FCS_STACK_STORAGE == FCS_STACK_STORAGE_GOOGLE_DENSE_HASH)

struct column_equality
{
    bool operator()(const char *s1, const char *s2) const
    {
        return (s1 == s2) ||
               (s1 && s2 &&
                   (fc_solve_stack_compare_for_comparison(s1, s2) == 0));
    }
};

struct column_hash
{
    int operator()(const char *s1) const
    {
        return perl_hash_function((const ub1 *)s1, fcs_col_len(s1) + 1);
    }
};

#if (FCS_WHICH_COLUMNS_GOOGLE_HASH == FCS_WHICH_COLUMNS_GOOGLE_HASH__SPARSE)
typedef sparse_hash_set<char *, column_hash, column_equality> ColumnsGoogleHash;
#else
typedef dense_hash_set<char *, column_hash, column_equality> ColumnsGoogleHash;
#endif

extern "C" fcs_columns_google_hash_handle_t fc_solve_columns_google_hash_new()
{
    ColumnsGoogleHash *ret = new ColumnsGoogleHash;

#if (FCS_WHICH_STATES_GOOGLE_HASH == FCS_WHICH_COLUMNS_GOOGLE_HASH__DENSE)
    ret->set_empty_key(NULL);
#endif

    return (fcs_columns_google_hash_handle_t)(ret);
}

/*
 * Returns 0 if the key is new and the key/val pair was inserted.
 *      - in that case *existing_key / *existing_val will be set to key
 *      and val respectively.
 * Returns 1 if the key is not new and *existing_key / *existing_val
 * was set to it.
 */
extern "C" bool fc_solve_columns_google_hash_insert(
    fcs_columns_google_hash_handle_t void_hash, void *key, void **existing_key)
{
    ColumnsGoogleHash *hash = (ColumnsGoogleHash *)void_hash;
    std::pair<ColumnsGoogleHash::iterator, bool> result =
        hash->insert((char *)key);

    /* If an insertion took place. */
    if (result.second)
    {
        *existing_key = NULL;
        return FALSE;
    }
    else
    {
        *existing_key = (*(result.first));

        return TRUE;
    }
}

extern "C" void fc_solve_columns_google_hash_free(
    fcs_columns_google_hash_handle_t void_hash)
{
    ColumnsGoogleHash *hash = (ColumnsGoogleHash *)void_hash;

    delete hash;

    return;
}

#endif