P4C
The P4 Compiler
Loading...
Searching...
No Matches
hashvec.h
1/*
2Copyright 2023-present Intel
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef LIB_HASHVEC_H_
18#define LIB_HASHVEC_H_
19
20#include <cstdint>
21
22#include "bitvec.h"
23#ifdef DEBUG
24#include <iostream>
25#endif
26
27namespace P4 {
28
30 struct internal;
31 internal *info;
32 union {
33 uint8_t *s1;
34 uint16_t *s2;
35 uint32_t *s3;
36 // FIXME -- add uint64_t for vectors of more than 2**32 elements?
37 } hash;
38 static uint32_t gethash_s1(const hash_vector_base *, size_t);
39 static uint32_t gethash_s2(const hash_vector_base *, size_t);
40 static uint32_t gethash_s3(const hash_vector_base *, size_t);
41 static void sethash_s1(hash_vector_base *, size_t, uint32_t);
42 static void sethash_s2(hash_vector_base *, size_t, uint32_t);
43 static void sethash_s3(hash_vector_base *, size_t, uint32_t);
44 void allochash();
45 void freehash();
46
47 size_t hashsize; /* size of the hash array - always a power-of-2-minus-1 */
48 size_t collisions, log_hashsize; /* log(base 2) of hashsize+1 */
49
50 public:
51 struct lookup_cache {
52 size_t slot;
53 size_t collisions;
54 uint32_t getidx(const hash_vector_base *);
55 const void *getkey(const hash_vector_base *);
56 void *getval(hash_vector_base *);
57 };
58#ifdef DEBUG
59 void dump(std::ostream &);
60#endif
61
62 protected:
63 size_t inuse; /* number of elements in use in the table */
64 bitvec erased; /* elements that have been erased */
65 hash_vector_base(bool ismap, bool ismulti, size_t capacity);
68 hash_vector_base &operator=(const hash_vector_base &);
70 virtual ~hash_vector_base() { freehash(); }
71 virtual size_t hashfn(const void *) const = 0;
72 virtual bool cmpfn(const void *, const void *) const = 0;
73 virtual bool cmpfn(const void *, size_t) const = 0;
74 virtual const void *getkey(uint32_t) const = 0;
75 virtual void *getval(uint32_t) = 0;
76 virtual uint32_t limit() = 0;
77 virtual void resizedata(size_t) = 0;
78 virtual void moveentry(size_t, size_t) = 0;
79
80 void clear();
81 size_t find(const void *key, lookup_cache *cache) const;
82 size_t find_next(const void *key, lookup_cache *cache) const;
83 void *lookup(const void *key, lookup_cache *cache = nullptr);
84 void *lookup_next(const void *key, lookup_cache *cache = nullptr);
85 size_t hv_insert(const void *key, lookup_cache *cache = nullptr);
86 // returns data index caller needs to store value in; if == data.size(), need emplace_back
87 // also need to clear erased[index]
88 size_t remove(const void *key, lookup_cache *cache = nullptr);
89 // returns data index caller needs to clear (if < data.size()) or -1 if nothing removed
90 void redo_hash();
91};
92
93} // namespace P4
94
95#endif /* LIB_HASHVEC_H_ */
Definition bitvec.h:120
Definition hashvec.h:29
Definition hashvec.cpp:30
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition hashvec.h:51