blob: 3dc59c724f7166f474581f3bb85d5bbd409862b0 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 Copyright (c) 2005 Eliot Dresselhaus
17
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
25
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
28
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36*/
37
38#ifndef included_phash_h
39#define included_phash_h
40
Dave Barachc3799992016-08-15 11:12:27 -040041#include <vppinfra/hash.h> /* for Bob's mixing functions */
Ed Warnickecb9cada2015-12-08 15:45:58 -070042
Dave Barachc3799992016-08-15 11:12:27 -040043typedef struct
44{
Ed Warnickecb9cada2015-12-08 15:45:58 -070045 /* Maybe either pointer to vector or inline word. */
46 uword key;
47
48 /* Hash code (A, B). */
49 u32 a, b;
50} phash_key_t;
51
52/* Table indexed by B. */
Dave Barachc3799992016-08-15 11:12:27 -040053typedef struct
54{
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 /* Vector of key indices with this same value of B. */
Dave Barachc3799992016-08-15 11:12:27 -040056 u32 *keys;
Ed Warnickecb9cada2015-12-08 15:45:58 -070057
58 /* hash=a^tabb[b].val_b */
59 u32 val_b;
60
61 /* High watermark of who has visited this map node. */
62 u32 water_b;
63} phash_tabb_t;
64
65always_inline void
66phash_tabb_free (phash_tabb_t * b)
67{
68 vec_free (b->keys);
69 b->val_b = b->water_b = 0;
70}
71
72typedef struct
73{
74 /* b that currently occupies this hash */
75 u32 b_q;
76
77 /* Queue position of parent that could use this hash. */
78 u32 parent_q;
79
80 /* What to change parent tab[b] to use this hash. */
81 u32 newval_q;
82
83 /* Original value of tab[b]. */
84 u32 oldval_q;
85} phash_tabq_t;
86
Dave Barachc3799992016-08-15 11:12:27 -040087typedef struct
88{
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 u8 a_bits, b_bits, s_bits, a_shift;
90 u32 b_mask;
Dave Barachc3799992016-08-15 11:12:27 -040091 u32 *tab;
92 u32 *scramble;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093
94 /* Seed value for hash mixer. */
95 u64 hash_seed;
96
97 u32 flags;
98
99 /* Key functions want 64 bit keys.
100 Use hash_mix64 rather than hash_mix32. */
101#define PHASH_FLAG_MIX64 (1 << 0)
102#define PHASH_FLAG_MIX32 (0 << 0)
103
104 /* When b_bits is large enough (>= 12) we scramble. */
105#define PHASH_FLAG_USE_SCRAMBLE (1 << 1)
106
107 /* Slow mode gives smaller tables but at the expense of more run time. */
108#define PHASH_FLAG_SLOW_MODE (0 << 2)
109#define PHASH_FLAG_FAST_MODE (1 << 2)
110
111 /* Generate minimal perfect hash instead of perfect hash. */
112#define PHASH_FLAG_NON_MINIMAL (0 << 3)
113#define PHASH_FLAG_MINIMAL (1 << 3)
114
115 /* vec_len (keys) for minimal hash;
116 1 << s_bits for non-minimal hash. */
117 u32 hash_max;
118
119 /* Vector of keys. */
Dave Barachc3799992016-08-15 11:12:27 -0400120 phash_key_t *keys;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121
122 /* Used by callbacks to identify keys. */
Dave Barachc3799992016-08-15 11:12:27 -0400123 void *private;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124
125 /* Key comparison callback. */
Dave Barachc3799992016-08-15 11:12:27 -0400126 int (*key_is_equal) (void *private, uword key1, uword key2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127
128 /* Callback to reduce single key -> hash seeds. */
Dave Barachc3799992016-08-15 11:12:27 -0400129 void (*key_seed1) (void *private, uword key, void *seed);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130
131 /* Callback to reduce two key2 -> hash seeds. */
Dave Barachc3799992016-08-15 11:12:27 -0400132 void (*key_seed2) (void *private, uword key1, uword key2, void *seed);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133
134 /* Stuff used to compute perfect hash. */
135 u32 random_seed;
136
137 /* Stuff indexed by B. */
Dave Barachc3799992016-08-15 11:12:27 -0400138 phash_tabb_t *tabb;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139
140 /* Table of B ordered by number of keys in tabb[b]. */
Dave Barachc3799992016-08-15 11:12:27 -0400141 u32 *tabb_sort;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142
143 /* Unique key (or ~0 if none) for a given hash
144 H = A ^ scramble[tab[B].val_b]. */
Dave Barachc3799992016-08-15 11:12:27 -0400145 u32 *tabh;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
147 /* Stuff indexed by q. */
Dave Barachc3799992016-08-15 11:12:27 -0400148 phash_tabq_t *tabq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
150 /* Stats. */
151 u32 n_seed_trials, n_perfect_calls;
152} phash_main_t;
153
154always_inline void
155phash_main_free_working_memory (phash_main_t * pm)
156{
157 vec_free (pm->tabb);
158 vec_free (pm->tabq);
159 vec_free (pm->tabh);
160 vec_free (pm->tabb_sort);
Dave Barachc3799992016-08-15 11:12:27 -0400161 if (!(pm->flags & PHASH_FLAG_USE_SCRAMBLE))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162 vec_free (pm->scramble);
163}
164
165always_inline void
166phash_main_free (phash_main_t * pm)
167{
168 phash_main_free_working_memory (pm);
169 vec_free (pm->tab);
170 vec_free (pm->keys);
Dave Barachb7b92992018-10-17 10:38:51 -0400171 clib_memset (pm, 0, sizeof (pm[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172}
173
174/* Slow hash computation for general keys. */
175uword phash_hash_slow (phash_main_t * pm, uword key);
176
177/* Main routine to compute perfect hash. */
Dave Barachc3799992016-08-15 11:12:27 -0400178clib_error_t *phash_find_perfect_hash (phash_main_t * pm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179
180/* Validates that hash is indeed perfect. */
Dave Barachc3799992016-08-15 11:12:27 -0400181clib_error_t *phash_validate (phash_main_t * pm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
183/* Unit test. */
184int phash_test_main (unformat_input_t * input);
185
186#endif /* included_phash_h */
Dave Barachc3799992016-08-15 11:12:27 -0400187
188/*
189 * fd.io coding-style-patch-verification: ON
190 *
191 * Local Variables:
192 * eval: (c-set-style "gnu")
193 * End:
194 */