diff options
Diffstat (limited to 'src/trie.zig')
| -rw-r--r-- | src/trie.zig | 49 |
1 files changed, 40 insertions, 9 deletions
diff --git a/src/trie.zig b/src/trie.zig index ee77954..8d2f258 100644 --- a/src/trie.zig +++ b/src/trie.zig | |||
| @@ -1,11 +1,42 @@ | |||
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const mem = std.mem; | 2 | const mem = std.mem; |
| 3 | 3 | ||
| 4 | pub const Color = enum { red, blue }; | 4 | const gbp = @import("ziglyph").grapheme_break; |
| 5 | |||
| 6 | pub const Prop = enum { | ||
| 7 | none, | ||
| 8 | control, | ||
| 9 | extend, | ||
| 10 | hangul_l, | ||
| 11 | hangul_lv, | ||
| 12 | hangul_lvt, | ||
| 13 | hangul_v, | ||
| 14 | hangul_t, | ||
| 15 | prepend, | ||
| 16 | regional, | ||
| 17 | spacing, | ||
| 18 | zwj, | ||
| 19 | |||
| 20 | pub fn forCodePoint(cp: u21) Prop { | ||
| 21 | if (gbp.isControl(cp)) return .control; | ||
| 22 | if (gbp.isExtend(cp)) return .extend; | ||
| 23 | if (gbp.isL(cp)) return .hangul_l; | ||
| 24 | if (gbp.isLv(cp)) return .hangul_lv; | ||
| 25 | if (gbp.isLvt(cp)) return .hangul_lvt; | ||
| 26 | if (gbp.isT(cp)) return .hangul_t; | ||
| 27 | if (gbp.isV(cp)) return .hangul_v; | ||
| 28 | if (gbp.isPrepend(cp)) return .prepend; | ||
| 29 | if (gbp.isRegionalIndicator(cp)) return .regional; | ||
| 30 | if (gbp.isSpacingmark(cp)) return .spacing; | ||
| 31 | if (gbp.isZwj(cp)) return .zwj; | ||
| 32 | |||
| 33 | return .none; | ||
| 34 | } | ||
| 35 | }; | ||
| 5 | 36 | ||
| 6 | pub const Node = struct { | 37 | pub const Node = struct { |
| 7 | children: [256]?*Node = [_]?*Node{null} ** 256, | 38 | children: [256]?*Node = [_]?*Node{null} ** 256, |
| 8 | value: ?Color = null, | 39 | value: ?Prop = null, |
| 9 | }; | 40 | }; |
| 10 | 41 | ||
| 11 | pub const Trie = struct { | 42 | pub const Trie = struct { |
| @@ -26,7 +57,7 @@ pub const Trie = struct { | |||
| 26 | bytes[0..]; | 57 | bytes[0..]; |
| 27 | } | 58 | } |
| 28 | 59 | ||
| 29 | pub fn put(self: *Trie, cp: u24, v: Color) !void { | 60 | pub fn put(self: *Trie, cp: u24, v: Prop) !void { |
| 30 | const s = asBytes(cp); | 61 | const s = asBytes(cp); |
| 31 | var current: *Node = &self.root; | 62 | var current: *Node = &self.root; |
| 32 | 63 | ||
| @@ -49,7 +80,7 @@ pub const Trie = struct { | |||
| 49 | } | 80 | } |
| 50 | } | 81 | } |
| 51 | 82 | ||
| 52 | pub fn get(self: Trie, cp: u24) ?Color { | 83 | pub fn get(self: Trie, cp: u24) ?Prop { |
| 53 | const s = asBytes(cp); | 84 | const s = asBytes(cp); |
| 54 | var current = &self.root; | 85 | var current = &self.root; |
| 55 | 86 | ||
| @@ -73,9 +104,9 @@ test "Trie works" { | |||
| 73 | const cp_2: u21 = '\u{10ff}'; | 104 | const cp_2: u21 = '\u{10ff}'; |
| 74 | const cp_3: u21 = '\u{10}'; | 105 | const cp_3: u21 = '\u{10}'; |
| 75 | 106 | ||
| 76 | try trie.put(cp_1, .red); | 107 | try trie.put(cp_1, .control); |
| 77 | try trie.put(cp_3, .blue); | 108 | try trie.put(cp_3, .zwj); |
| 78 | try std.testing.expectEqual(@as(?Color, .red), trie.get(cp_1)); | 109 | try std.testing.expectEqual(@as(?Prop, .control), trie.get(cp_1)); |
| 79 | try std.testing.expectEqual(@as(?Color, null), trie.get(cp_2)); | 110 | try std.testing.expectEqual(@as(?Prop, null), trie.get(cp_2)); |
| 80 | try std.testing.expectEqual(@as(?Color, .blue), trie.get(cp_3)); | 111 | try std.testing.expectEqual(@as(?Prop, .zwj), trie.get(cp_3)); |
| 81 | } | 112 | } |