| ... | @@ -241,7 +241,10 @@ pub fn StaticStringMapWithEql( | ... | @@ -241,7 +241,10 @@ pub fn StaticStringMapWithEql( |
| 241 | return self.kvs.values[self.getIndex(str) orelse return null]; | 241 | return self.kvs.values[self.getIndex(str) orelse return null]; |
| 242 | } | 242 | } |
| 243 | | 243 | |
| 244 | fn getIndex(self: Self, str: []const u8) ?usize { | 244 | /// Returns the index corresponding to the `str` within the |
| | 245 | /// generated `kvs`, or `null` if `str` was not found. |
| | 246 | /// The returned index is unrelated to the input `kvs_list`. |
| | 247 | pub fn getIndex(self: Self, str: []const u8) ?usize { |
| 245 | const kvs = self.kvs.*; | 248 | const kvs = self.kvs.*; |
| 246 | if (kvs.len == 0) | 249 | if (kvs.len == 0) |
| 247 | return null; | 250 | return null; |
| ... | @@ -279,6 +282,14 @@ pub fn StaticStringMapWithEql( | ... | @@ -279,6 +282,14 @@ pub fn StaticStringMapWithEql( |
| 279 | }; | 282 | }; |
| 280 | } | 283 | } |
| 281 | | 284 | |
| | 285 | /// Returns the index within the generated `kvs` corresponding to the |
| | 286 | /// key-value pair where key is the longest prefix of `str`, or `null` |
| | 287 | /// if no such key-value pair was found. The returned index is unrelated |
| | 288 | /// to the input `kvs_list`. |
| | 289 | /// |
| | 290 | /// This is effectively an O(N) algorithm which loops from `max_len` to |
| | 291 | /// `min_len` and calls `getIndex()` to check all keys with the given |
| | 292 | /// len. |
| 282 | pub fn getLongestPrefixIndex(self: Self, str: []const u8) ?usize { | 293 | pub fn getLongestPrefixIndex(self: Self, str: []const u8) ?usize { |
| 283 | if (self.kvs.len == 0) | 294 | if (self.kvs.len == 0) |
| 284 | return null; | 295 | return null; |
| ... | @@ -294,11 +305,15 @@ pub fn StaticStringMapWithEql( | ... | @@ -294,11 +305,15 @@ pub fn StaticStringMapWithEql( |
| 294 | return null; | 305 | return null; |
| 295 | } | 306 | } |
| 296 | | 307 | |
| | 308 | /// Returns the slice of keys from the generated `kvs`, which may |
| | 309 | /// be in a different order than the input `kvs_list`. |
| 297 | pub fn keys(self: Self) []const []const u8 { | 310 | pub fn keys(self: Self) []const []const u8 { |
| 298 | const kvs = self.kvs.*; | 311 | const kvs = self.kvs.*; |
| 299 | return kvs.keys[0..kvs.len]; | 312 | return kvs.keys[0..kvs.len]; |
| 300 | } | 313 | } |
| 301 | | 314 | |
| | 315 | /// Returns the slice of values from the generated `kvs`, which may |
| | 316 | /// be in a different order than the input `kvs_list`. |
| 302 | pub fn values(self: Self) []const V { | 317 | pub fn values(self: Self) []const V { |
| 303 | const kvs = self.kvs.*; | 318 | const kvs = self.kvs.*; |
| 304 | return kvs.values[0..kvs.len]; | 319 | return kvs.values[0..kvs.len]; |
| ... | @@ -504,6 +519,20 @@ test "comptime-only value" { | ... | @@ -504,6 +519,20 @@ test "comptime-only value" { |
| 504 | try testing.expect(map.get("d") == null); | 519 | try testing.expect(map.get("d") == null); |
| 505 | } | 520 | } |
| 506 | | 521 | |
| | 522 | test "getIndex" { |
| | 523 | const slice = [_]TestKV{ |
| | 524 | .{ "longer", .A }, |
| | 525 | .{ "short", .B }, |
| | 526 | }; |
| | 527 | const map = TestMap.initComptime(slice); |
| | 528 | |
| | 529 | // This index can be different than the index of the "short" KV in `slice` |
| | 530 | const short_index = map.getIndex("short").?; |
| | 531 | try testing.expectEqualStrings("short", map.keys()[short_index]); |
| | 532 | try testing.expectEqual(.B, map.values()[short_index]); |
| | 533 | try testing.expectEqual(null, map.getIndex("missing")); |
| | 534 | } |
| | 535 | |
| 507 | test "getLongestPrefix" { | 536 | test "getLongestPrefix" { |
| 508 | const slice = [_]TestKV{ | 537 | const slice = [_]TestKV{ |
| 509 | .{ "a", .A }, | 538 | .{ "a", .A }, |