Flutter Engine
The Flutter Engine
Classes | Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
dart::SimpleHashMap Class Reference

#include <hashmap.h>

Classes

struct  Entry
 

Public Types

typedef bool(* MatchFun) (void *key1, void *key2)
 
typedef void(* ClearFun) (void *value)
 

Public Member Functions

 SimpleHashMap (MatchFun match, uint32_t initial_capacity)
 
 ~SimpleHashMap ()
 
EntryLookup (void *key, uint32_t hash, bool insert)
 
void Remove (void *key, uint32_t hash)
 
void Clear (ClearFun clear=nullptr)
 
intptr_t size () const
 
intptr_t capacity () const
 
EntryStart () const
 
EntryNext (Entry *p) const
 

Static Public Member Functions

static bool SamePointerValue (void *key1, void *key2)
 
static uint32_t StringHash (const char *key)
 
static bool SameStringValue (void *key1, void *key2)
 

Friends

class IntSet
 

Detailed Description

Definition at line 12 of file hashmap.h.

Member Typedef Documentation

◆ ClearFun

typedef void(* dart::SimpleHashMap::ClearFun) (void *value)

Definition at line 16 of file hashmap.h.

◆ MatchFun

typedef bool(* dart::SimpleHashMap::MatchFun) (void *key1, void *key2)

Definition at line 14 of file hashmap.h.

Constructor & Destructor Documentation

◆ SimpleHashMap()

dart::SimpleHashMap::SimpleHashMap ( MatchFun  match,
uint32_t  initial_capacity 
)

Definition at line 11 of file hashmap.cc.

11 {
12 match_ = match;
13 Initialize(initial_capacity);
14}
def match(bench, filt)
Definition: benchmark.py:23

◆ ~SimpleHashMap()

dart::SimpleHashMap::~SimpleHashMap ( )

Definition at line 16 of file hashmap.cc.

16 {
17 delete[] map_;
18}

Member Function Documentation

◆ capacity()

intptr_t dart::SimpleHashMap::capacity ( ) const
inline

Definition at line 79 of file hashmap.h.

79{ return capacity_; }

◆ Clear()

void dart::SimpleHashMap::Clear ( ClearFun  clear = nullptr)

Definition at line 111 of file hashmap.cc.

111 {
112 // Mark all entries as empty.
113 const Entry* end = map_end();
114 for (Entry* p = map_; p < end; p++) {
115 if ((clear != nullptr) && (p->key != nullptr)) {
116 clear(p->value);
117 }
118 p->key = nullptr;
119 }
120 occupancy_ = 0;
121}
glong glong end

◆ Lookup()

SimpleHashMap::Entry * dart::SimpleHashMap::Lookup ( void *  key,
uint32_t  hash,
bool  insert 
)

Definition at line 20 of file hashmap.cc.

22 {
23 // Find a matching entry.
24 Entry* p = Probe(key, hash);
25 if (p->key != nullptr) {
26 return p;
27 }
28
29 // No entry found; insert one if necessary.
30 if (insert) {
31 p->key = key;
32 p->value = nullptr;
33 p->hash = hash;
34 occupancy_++;
35
36 // Grow the map if we reached >= 80% occupancy.
37 if ((occupancy_ + (occupancy_ / 4)) >= capacity_) {
38 Resize();
39 p = Probe(key, hash);
40 }
41
42 return p;
43 }
44
45 // No entry found and none inserted.
46 return nullptr;
47}
static uint32_t hash(const SkShaderBase::GradientInfo &v)

◆ Next()

SimpleHashMap::Entry * dart::SimpleHashMap::Next ( Entry p) const

Definition at line 127 of file hashmap.cc.

127 {
128 const Entry* end = map_end();
129 ASSERT(map_ - 1 <= p && p < end);
130 for (p++; p < end; p++) {
131 if (p->key != nullptr) {
132 return p;
133 }
134 }
135 return nullptr;
136}
#define ASSERT(E)

◆ Remove()

void dart::SimpleHashMap::Remove ( void *  key,
uint32_t  hash 
)

Definition at line 49 of file hashmap.cc.

49 {
50 // Lookup the entry for the key to remove.
51 Entry* candidate = Probe(key, hash);
52 if (candidate->key == nullptr) {
53 // Key not found nothing to remove.
54 return;
55 }
56
57 // To remove an entry we need to ensure that it does not create an empty
58 // entry that will cause the search for another entry to stop too soon. If all
59 // the entries between the entry to remove and the next empty slot have their
60 // initial position inside this interval, clearing the entry to remove will
61 // not break the search. If, while searching for the next empty entry, an
62 // entry is encountered which does not have its initial position between the
63 // entry to remove and the position looked at, then this entry can be moved to
64 // the place of the entry to remove without breaking the search for it. The
65 // entry made vacant by this move is now the entry to remove and the process
66 // starts over.
67 // Algorithm from http://en.wikipedia.org/wiki/Open_addressing.
68
69 // This guarantees loop termination as there is at least one empty entry so
70 // eventually the removed entry will have an empty entry after it.
71 ASSERT(occupancy_ < capacity_);
72
73 // "candidate" is the candidate entry to clear. "next" is used to scan
74 // forwards.
75 Entry* next = candidate; // Start at the entry to remove.
76 while (true) {
77 // Move "next" to the next entry. Wrap when at the end of the array.
78 next = next + 1;
79 if (next == map_end()) {
80 next = map_;
81 }
82
83 // All entries between "candidate" and "next" have their initial position
84 // between candidate and entry and the entry candidate can be cleared
85 // without breaking the search for these entries.
86 if (next->key == nullptr) {
87 break;
88 }
89
90 // Find the initial position for the entry at position "next". That is
91 // the entry where searching for the entry at position "next" will
92 // actually start.
93 Entry* start = map_ + (next->hash & (capacity_ - 1));
94
95 // If the entry at position "next" has its initial position outside the
96 // range between "candidate" and "next" it can be moved forward to
97 // position "candidate" and will still be found. There is now the new
98 // candidate entry for clearing.
99 if ((next > candidate && (start <= candidate || start > next)) ||
100 (next < candidate && (start <= candidate && start > next))) {
101 *candidate = *next;
102 candidate = next;
103 }
104 }
105
106 // Clear the candidate which will not break searching the hash table.
107 candidate->key = nullptr;
108 occupancy_--;
109}
static float next(float f)

◆ SamePointerValue()

static bool dart::SimpleHashMap::SamePointerValue ( void *  key1,
void *  key2 
)
inlinestatic

Definition at line 24 of file hashmap.h.

24{ return key1 == key2; }

◆ SameStringValue()

static bool dart::SimpleHashMap::SameStringValue ( void *  key1,
void *  key2 
)
inlinestatic

Definition at line 41 of file hashmap.h.

41 {
42 return strcmp(reinterpret_cast<char*>(key1),
43 reinterpret_cast<char*>(key2)) == 0;
44 }

◆ size()

intptr_t dart::SimpleHashMap::size ( ) const
inline

Definition at line 74 of file hashmap.h.

74{ return occupancy_; }

◆ Start()

SimpleHashMap::Entry * dart::SimpleHashMap::Start ( ) const

Definition at line 123 of file hashmap.cc.

123 {
124 return Next(map_ - 1);
125}
Entry * Next(Entry *p) const
Definition: hashmap.cc:127

◆ StringHash()

static uint32_t dart::SimpleHashMap::StringHash ( const char *  key)
inlinestatic

Definition at line 26 of file hashmap.h.

26 {
27 uint32_t hash_ = 0;
28 if (key == nullptr) return hash_;
29 int len = strlen(key);
30 for (int i = 0; i < len; i++) {
31 hash_ += key[i];
32 hash_ += hash_ << 10;
33 hash_ ^= hash_ >> 6;
34 }
35 hash_ += hash_ << 3;
36 hash_ ^= hash_ >> 11;
37 hash_ += hash_ << 15;
38 return hash_ == 0 ? 1 : hash_;
39 }

Friends And Related Function Documentation

◆ IntSet

friend class IntSet
friend

Definition at line 103 of file hashmap.h.


The documentation for this class was generated from the following files: