github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/native/map.h (about) 1 /** 2 * Copyright 2023 CloudWeGo Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "native.h" 18 #include "memops.h" 19 20 #ifndef MAP_H 21 #define MAP_H 22 23 #define CHAR_POINT (unsigned char)'.' 24 25 typedef struct 26 { 27 uint32_t hash; 28 GoString key; 29 void *val; 30 } Entry; 31 32 typedef struct 33 { 34 Entry *b; 35 uint32_t c; 36 uint32_t N; 37 } HashMap; 38 39 typedef struct 40 { 41 void *val; 42 GoString key; 43 } Pair; 44 45 typedef struct 46 { 47 GoSlice *leaves; 48 GoSlice index; 49 } TrieNode; 50 51 typedef struct 52 { 53 size_t count; 54 GoSlice positions; 55 void *empty; 56 TrieNode node; 57 } TrieTree; 58 59 void hm_set(HashMap *self, const GoString *key, void *val); 60 void *hm_get(const HashMap *self, const GoString *key); 61 void *trie_get(const TrieTree *self, const GoString *key); 62 63 typedef uint16_t tid; 64 65 typedef int8_t req_em; 66 67 #define REQ_OPTIONAL 0 68 #define REQ_DEFAULT 1 69 #define REQ_REQUIRED 2 70 #define BSIZE_INT64 64 71 #define SIZE_INT64 8 72 #define BSIZE_REQ 2 73 #define RequirenessInt64Count \ 74 (BSIZE_INT64 / BSIZE_REQ) 75 76 #define RequirenessMaskLeft \ 77 (BSIZE_INT64 - BSIZE_REQ) 78 79 #define RequirenessInt64Count \ 80 (BSIZE_INT64 / BSIZE_REQ) 81 82 #define Int64Mask 0xffffffffffffffffull 83 84 #define RequiredMask 0xaaaaaaaaaaaaaaaaull 85 86 #define DefaultMask \ 87 (RequiredMask >> 1) 88 89 #define BM_HAS_REQUIRED(v) \ 90 (v & RequiredMask) != 0 91 92 #define BM_HAS_DEFAULT(v) \ 93 (v & DefaultMask) != 0 94 95 typedef struct 96 { 97 uint64_t *buf; 98 size_t len; 99 } ReqBitMap; 100 101 bool bm_is_set(ReqBitMap b, tid id); 102 void bm_set_req(ReqBitMap b, tid id, req_em req); 103 104 #endif // MAP_H