1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
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 #ifndef nvs_item_hash_list_h
16 #define nvs_item_hash_list_h
17 
18 #include "nvs.h"
19 #include "nvs_types.hpp"
20 #include "intrusive_list.h"
21 
22 namespace nvs
23 {
24 
25 class HashList
26 {
27 public:
28     HashList();
29     ~HashList();
30 
31     esp_err_t insert(const Item& item, size_t index);
32     void erase(const size_t index, bool itemShouldExist=true);
33     size_t find(size_t start, const Item& item);
34     void clear();
35 
36 private:
37     HashList(const HashList& other);
38     const HashList& operator= (const HashList& rhs);
39 
40 protected:
41 
42     struct HashListNode {
HashListNodenvs::HashList::HashListNode43         HashListNode() :
44             mIndex(0xff), mHash(0)
45         {
46         }
47 
HashListNodenvs::HashList::HashListNode48         HashListNode(uint32_t hash, size_t index) :
49             mIndex((uint32_t) index), mHash(hash)
50         {
51         }
52 
53         uint32_t mIndex : 8;
54         uint32_t mHash  : 24;
55     };
56 
57     struct HashListBlock : public intrusive_list_node<HashList::HashListBlock> {
58         HashListBlock();
59 
60         static const size_t BYTE_SIZE = 128;
61         static const size_t ENTRY_COUNT = (BYTE_SIZE - sizeof(intrusive_list_node<HashListBlock>) - sizeof(size_t)) / 4;
62 
63         size_t mCount = 0;
64         HashListNode mNodes[ENTRY_COUNT];
65     };
66 
67     typedef intrusive_list<HashListBlock> TBlockList;
68     TBlockList mBlockList;
69 }; // class HashList
70 
71 } // namespace nvs
72 
73 
74 #endif /* nvs_item_hash_list_h */
75