OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 #include <algorithm> | 5 #include <algorithm> |
6 #include <cstring> | 6 #include <cstring> |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
(...skipping 15 matching lines...) Expand all Loading... |
26 static uword Hash(const char* key) { | 26 static uword Hash(const char* key) { |
27 return static_cast<uword>(strlen(key)); | 27 return static_cast<uword>(strlen(key)); |
28 } | 28 } |
29 static bool IsMatch(const Object& a, const Object& b) { | 29 static bool IsMatch(const Object& a, const Object& b) { |
30 return a.IsString() && b.IsString() && | 30 return a.IsString() && b.IsString() && |
31 String::Cast(a).Equals(String::Cast(b)); | 31 String::Cast(a).Equals(String::Cast(b)); |
32 } | 32 } |
33 static uword Hash(const Object& obj) { | 33 static uword Hash(const Object& obj) { |
34 return String::Cast(obj).Length(); | 34 return String::Cast(obj).Length(); |
35 } | 35 } |
| 36 static RawObject* NewKey(const char* key) { |
| 37 return String::New(key); |
| 38 } |
36 }; | 39 }; |
37 | 40 |
38 | 41 |
39 template<typename Table> | 42 template<typename Table> |
40 void Validate(const Table& table) { | 43 void Validate(const Table& table) { |
41 // Verify consistency of entry state tracking. | 44 // Verify consistency of entry state tracking. |
42 intptr_t num_entries = table.NumEntries(); | 45 intptr_t num_entries = table.NumEntries(); |
43 intptr_t num_unused = table.NumUnused(); | 46 intptr_t num_unused = table.NumUnused(); |
44 intptr_t num_occupied = table.NumOccupied(); | 47 intptr_t num_occupied = table.NumOccupied(); |
45 intptr_t num_deleted = table.NumDeleted(); | 48 intptr_t num_deleted = table.NumDeleted(); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 Table table(Array::Handle(HashTables::New<Table>(5))); | 124 Table table(Array::Handle(HashTables::New<Table>(5))); |
122 table.UpdateOrInsert(String::Handle(String::New("a")), | 125 table.UpdateOrInsert(String::Handle(String::New("a")), |
123 String::Handle(String::New("A"))); | 126 String::Handle(String::New("A"))); |
124 EXPECT(table.ContainsKey("a")); | 127 EXPECT(table.ContainsKey("a")); |
125 table.UpdateValue("a", String::Handle(String::New("AAA"))); | 128 table.UpdateValue("a", String::Handle(String::New("AAA"))); |
126 String& a_value = String::Handle(); | 129 String& a_value = String::Handle(); |
127 a_value ^= table.GetOrNull("a"); | 130 a_value ^= table.GetOrNull("a"); |
128 EXPECT(a_value.Equals("AAA")); | 131 EXPECT(a_value.Equals("AAA")); |
129 Object& null_value = Object::Handle(table.GetOrNull("0")); | 132 Object& null_value = Object::Handle(table.GetOrNull("0")); |
130 EXPECT(null_value.IsNull()); | 133 EXPECT(null_value.IsNull()); |
| 134 |
| 135 // Test on-demand allocation of a new key object using NewKey in traits. |
| 136 String& b_value = String::Handle(); |
| 137 b_value ^= |
| 138 table.InsertNewOrGetValue("b", String::Handle(String::New("BBB"))); |
| 139 EXPECT(b_value.Equals("BBB")); |
| 140 { |
| 141 // When the key is already present, there should be no allocation. |
| 142 NoGCScope no_gc; |
| 143 b_value ^= table.InsertNewOrGetValue("b", a_value); |
| 144 EXPECT(b_value.Equals("BBB")); |
| 145 } |
131 table.Release(); | 146 table.Release(); |
132 } | 147 } |
133 | 148 |
134 | 149 |
135 std::string ToStdString(const String& str) { | 150 std::string ToStdString(const String& str) { |
136 EXPECT(str.IsOneByteString()); | 151 EXPECT(str.IsOneByteString()); |
137 std::string result; | 152 std::string result; |
138 for (intptr_t i = 0; i < str.Length(); ++i) { | 153 for (intptr_t i = 0; i < str.Length(); ++i) { |
139 result += static_cast<char>(str.CharAt(i)); | 154 result += static_cast<char>(str.CharAt(i)); |
140 } | 155 } |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 TEST_CASE(Maps) { | 293 TEST_CASE(Maps) { |
279 for (intptr_t initial_capacity = 0; | 294 for (intptr_t initial_capacity = 0; |
280 initial_capacity < 32; | 295 initial_capacity < 32; |
281 ++initial_capacity) { | 296 ++initial_capacity) { |
282 TestMap<UnorderedHashMap<TestTraits> >(initial_capacity, false); | 297 TestMap<UnorderedHashMap<TestTraits> >(initial_capacity, false); |
283 TestMap<EnumIndexHashMap<TestTraits> >(initial_capacity, true); | 298 TestMap<EnumIndexHashMap<TestTraits> >(initial_capacity, true); |
284 } | 299 } |
285 } | 300 } |
286 | 301 |
287 } // namespace dart | 302 } // namespace dart |
OLD | NEW |