OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef RUNTIME_VM_FIXED_CACHE_H_ | 5 #ifndef RUNTIME_VM_FIXED_CACHE_H_ |
6 #define RUNTIME_VM_FIXED_CACHE_H_ | 6 #define RUNTIME_VM_FIXED_CACHE_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 namespace dart { | 11 namespace dart { |
12 | 12 |
13 // A simple sorted fixed size Key-Value storage. | 13 // A simple sorted fixed size Key-Value storage. |
14 // | 14 // |
15 // Assumes both Key and Value are POD-like objects. | 15 // Assumes both Key and Value are default-constructible objects. |
16 // | 16 // |
17 // Keys must be comparable with operator<. | 17 // Keys must be comparable with operator<. |
18 // | 18 // |
19 // Duplicates are not allowed - check with Lookup before insertion. | 19 // Duplicates are not allowed - check with Lookup before insertion. |
20 // | 20 // |
21 // Optionally Values may have cleanup function to delete | |
22 // any resources they point to. | |
23 template <class K, class V, intptr_t kCapacity> | 21 template <class K, class V, intptr_t kCapacity> |
24 class FixedCache { | 22 class FixedCache { |
25 public: | 23 public: |
26 typedef void (*Deleter)(V*); | |
27 | |
28 struct Entry { | 24 struct Entry { |
29 K key; | 25 K key; |
30 V value; | 26 V value; |
31 }; | 27 }; |
32 | 28 |
33 explicit FixedCache(Deleter deleter = NULL) : deleter_(deleter), length_(0) {} | 29 FixedCache() : length_(0) {} |
34 | 30 |
35 ~FixedCache() { Clear(); } | 31 ~FixedCache() { Clear(); } |
36 | 32 |
37 V* Lookup(K key) { | 33 V* Lookup(K key) { |
38 intptr_t i = LowerBound(key); | 34 intptr_t i = LowerBound(key); |
39 if (i != length_ && pairs_[i].key == key) return &pairs_[i].value; | 35 if (i != length_ && pairs_[i].key == key) return &pairs_[i].value; |
40 return NULL; | 36 return NULL; |
41 } | 37 } |
42 | 38 |
43 void Insert(K key, V value) { | 39 void Insert(K key, V value) { |
44 intptr_t i = LowerBound(key); | 40 intptr_t i = LowerBound(key); |
45 | 41 |
46 if (length_ == kCapacity) { | 42 if (length_ == kCapacity) { |
47 if (deleter_) deleter_(&pairs_[length_ - 1].value); | |
48 length_ = kCapacity - 1; | 43 length_ = kCapacity - 1; |
49 if (i == kCapacity) i = kCapacity - 1; | 44 if (i == kCapacity) i = kCapacity - 1; |
50 } | 45 } |
51 | 46 |
52 for (intptr_t j = length_ - 1; j >= i; j--) { | 47 for (intptr_t j = length_ - 1; j >= i; j--) { |
53 pairs_[j + 1] = pairs_[j]; | 48 pairs_[j + 1] = pairs_[j]; |
54 } | 49 } |
55 | 50 |
56 length_ += 1; | 51 length_ += 1; |
57 pairs_[i].key = key; | 52 pairs_[i].key = key; |
58 pairs_[i].value = value; | 53 pairs_[i].value = value; |
59 } | 54 } |
60 | 55 |
61 void Clear() { | 56 void Clear() { |
62 if (deleter_) { | |
63 for (intptr_t i = 0; i < length_; i++) { | |
64 deleter_(&pairs_[i].value); | |
65 } | |
66 } | |
67 length_ = 0; | 57 length_ = 0; |
68 } | 58 } |
69 | 59 |
70 private: | 60 private: |
71 intptr_t LowerBound(K key) { | 61 intptr_t LowerBound(K key) { |
72 intptr_t low = 0, high = length_; | 62 intptr_t low = 0, high = length_; |
73 while (low != high) { | 63 while (low != high) { |
74 intptr_t mid = low + (high - low) / 2; | 64 intptr_t mid = low + (high - low) / 2; |
75 if (key < pairs_[mid].key) { | 65 if (key < pairs_[mid].key) { |
76 high = mid; | 66 high = mid; |
77 } else if (key > pairs_[mid].key) { | 67 } else if (key > pairs_[mid].key) { |
78 low = mid + 1; | 68 low = mid + 1; |
79 } else { | 69 } else { |
80 low = high = mid; | 70 low = high = mid; |
81 } | 71 } |
82 } | 72 } |
83 return low; | 73 return low; |
84 } | 74 } |
85 | 75 |
86 Entry pairs_[kCapacity]; // Sorted array of pairs. | 76 Entry pairs_[kCapacity]; // Sorted array of pairs. |
87 Deleter deleter_; | |
88 intptr_t length_; | 77 intptr_t length_; |
89 }; | 78 }; |
90 | 79 |
91 } // namespace dart | 80 } // namespace dart |
92 | 81 |
93 #endif // RUNTIME_VM_FIXED_CACHE_H_ | 82 #endif // RUNTIME_VM_FIXED_CACHE_H_ |
OLD | NEW |