P4C
The P4 Compiler
Loading...
Searching...
No Matches
hashvec.h
1/*
2 * SPDX-FileCopyrightText: 2023 Intel
3 * Copyright 2023-present Intel
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef LIB_HASHVEC_H_
9#define LIB_HASHVEC_H_
10
11#include <cstdint>
12
13#include "bitvec.h"
14#ifdef DEBUG
15#include <iostream>
16#endif
17
18namespace P4 {
19
20class hash_vector_base {
21 struct internal;
22 internal *info;
23 union {
24 uint8_t *s1;
25 uint16_t *s2;
26 uint32_t *s3;
27 // FIXME -- add uint64_t for vectors of more than 2**32 elements?
28 } hash;
29 static uint32_t gethash_s1(const hash_vector_base *, size_t);
30 static uint32_t gethash_s2(const hash_vector_base *, size_t);
31 static uint32_t gethash_s3(const hash_vector_base *, size_t);
32 static void sethash_s1(hash_vector_base *, size_t, uint32_t);
33 static void sethash_s2(hash_vector_base *, size_t, uint32_t);
34 static void sethash_s3(hash_vector_base *, size_t, uint32_t);
35 void allochash();
36 void freehash();
37
38 size_t hashsize; /* size of the hash array - always a power-of-2-minus-1 */
39 size_t collisions, log_hashsize; /* log(base 2) of hashsize+1 */
40
41 public:
42 struct lookup_cache {
43 size_t slot;
44 size_t collisions;
45 uint32_t getidx(const hash_vector_base *);
46 const void *getkey(const hash_vector_base *);
47 void *getval(hash_vector_base *);
48 };
49#ifdef DEBUG
50 void dump(std::ostream &);
51#endif
52
53 protected:
54 size_t inuse; /* number of elements in use in the table */
55 bitvec erased; /* elements that have been erased */
56 hash_vector_base(bool ismap, bool ismulti, size_t capacity);
57 hash_vector_base(const hash_vector_base &);
58 hash_vector_base(hash_vector_base &&);
59 hash_vector_base &operator=(const hash_vector_base &);
60 hash_vector_base &operator=(hash_vector_base &&);
61 virtual ~hash_vector_base() { freehash(); }
62 virtual size_t hashfn(const void *) const = 0;
63 virtual bool cmpfn(const void *, const void *) const = 0;
64 virtual bool cmpfn(const void *, size_t) const = 0;
65 virtual const void *getkey(uint32_t) const = 0;
66 virtual void *getval(uint32_t) = 0;
67 virtual uint32_t limit() = 0;
68 virtual void resizedata(size_t) = 0;
69 virtual void moveentry(size_t, size_t) = 0;
70
71 void clear();
72 size_t find(const void *key, lookup_cache *cache) const;
73 size_t find_next(const void *key, lookup_cache *cache) const;
74 void *lookup(const void *key, lookup_cache *cache = nullptr);
75 void *lookup_next(const void *key, lookup_cache *cache = nullptr);
76 size_t hv_insert(const void *key, lookup_cache *cache = nullptr);
77 // returns data index caller needs to store value in; if == data.size(), need emplace_back
78 // also need to clear erased[index]
79 size_t remove(const void *key, lookup_cache *cache = nullptr);
80 // returns data index caller needs to clear (if < data.size()) or -1 if nothing removed
81 void redo_hash();
82};
83
84} // namespace P4
85
86#endif /* LIB_HASHVEC_H_ */
Definition bitvec.h:120
Definition hashvec.cpp:19
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
Definition hashvec.h:42