|
| 1 | +package suffix |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | +) |
| 6 | + |
| 7 | +func ExampleInsert() { |
| 8 | + tree := NewTree() |
| 9 | + tree.Insert([]byte("sth"), "sth") |
| 10 | + oldValue, ok := tree.Insert([]byte("sth"), "else") |
| 11 | + // Always ok |
| 12 | + if ok { |
| 13 | + fmt.Println(oldValue.(string)) |
| 14 | + } |
| 15 | + value, found := tree.Get([]byte("sth")) |
| 16 | + if found { |
| 17 | + fmt.Println(value.(string)) |
| 18 | + } |
| 19 | + // Output: |
| 20 | + // sth |
| 21 | + // else |
| 22 | +} |
| 23 | + |
| 24 | +func ExampleGet() { |
| 25 | + tree := NewTree() |
| 26 | + tree.Insert([]byte("sth"), "sth") |
| 27 | + value, found := tree.Get([]byte("sth")) |
| 28 | + if found { |
| 29 | + fmt.Println(value.(string)) |
| 30 | + } |
| 31 | + // Output: sth |
| 32 | +} |
| 33 | + |
| 34 | +func ExampleLongestSuffix() { |
| 35 | + tree := NewTree() |
| 36 | + tree.Insert([]byte("table"), "table") |
| 37 | + tree.Insert([]byte("able"), "able") |
| 38 | + tree.Insert([]byte("present"), "present") |
| 39 | + key, value, found := tree.LongestSuffix([]byte("presentable")) |
| 40 | + if found { |
| 41 | + fmt.Println("Matched key:", string(key)) |
| 42 | + fmt.Println(value.(string)) |
| 43 | + } |
| 44 | + // Output: |
| 45 | + // Matched key: table |
| 46 | + // table |
| 47 | +} |
| 48 | + |
| 49 | +func ExampleRemove() { |
| 50 | + tree := NewTree() |
| 51 | + tree.Insert([]byte("sth"), "sth") |
| 52 | + oldValue, found := tree.Remove([]byte("sth")) |
| 53 | + if found { |
| 54 | + fmt.Println(oldValue.(string)) |
| 55 | + } |
| 56 | + _, found = tree.Get([]byte("sth")) |
| 57 | + if !found { |
| 58 | + fmt.Println("Already removed") |
| 59 | + } |
| 60 | + // Output: |
| 61 | + // sth |
| 62 | + // Already removed |
| 63 | +} |
| 64 | + |
| 65 | +func ExampleWalk() { |
| 66 | + tree := NewTree() |
| 67 | + tree.Insert([]byte("able"), 1) |
| 68 | + tree.Insert([]byte("table"), 2) |
| 69 | + tree.Insert([]byte("presentable"), 3) |
| 70 | + tree.Walk(func(key []byte, _ interface{}) (stop bool) { |
| 71 | + fmt.Println(string(key)) |
| 72 | + return false |
| 73 | + }) |
| 74 | + fmt.Println("Walk and stop in the middle:") |
| 75 | + tree.Walk(func(key []byte, _ interface{}) (stop bool) { |
| 76 | + fmt.Println(string(key)) |
| 77 | + return string(key) == "able" |
| 78 | + }) |
| 79 | + // Output: |
| 80 | + // able |
| 81 | + // table |
| 82 | + // presentable |
| 83 | + // Walk and stop in the middle: |
| 84 | + // able |
| 85 | +} |
| 86 | + |
| 87 | +func ExampleWalkSuffix() { |
| 88 | + tree := NewTree() |
| 89 | + tree.Insert([]byte("able"), 1) |
| 90 | + tree.Insert([]byte("table"), 2) |
| 91 | + tree.Insert([]byte("presentable"), 3) |
| 92 | + tree.Insert([]byte("present"), 4) |
| 93 | + tree.WalkSuffix([]byte("table"), func(key []byte, _ interface{}) (stop bool) { |
| 94 | + fmt.Println(string(key)) |
| 95 | + return false |
| 96 | + }) |
| 97 | + fmt.Println("Walk and stop in the middle:") |
| 98 | + tree.WalkSuffix([]byte("table"), func(key []byte, _ interface{}) (stop bool) { |
| 99 | + fmt.Println(string(key)) |
| 100 | + return string(key) == "table" |
| 101 | + }) |
| 102 | + // Output: |
| 103 | + // table |
| 104 | + // presentable |
| 105 | + // Walk and stop in the middle: |
| 106 | + // table |
| 107 | +} |
0 commit comments