File: implementation.go

package info (click to toggle)
kitty 0.45.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,476 kB
  • sloc: ansic: 84,285; python: 57,992; objc: 5,432; sh: 1,333; xml: 364; makefile: 144; javascript: 78
file content (64 lines) | stat: -rw-r--r-- 1,281 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>

package shortcuts

import (
	"fmt"
)

var _ = fmt.Print

func (self *ShortcutMap[T]) first_action() (ans T) {
	for _, ac := range self.leaves {
		return ac
	}
	for _, child := range self.children {
		return child.first_action()
	}
	return
}

func (self *ShortcutMap[T]) shortcut_for(ac T) (keys []string) {
	keys = []string{}
	for key, q := range self.leaves {
		if ac == q {
			return append(keys, key)
		}
	}
	for key, child := range self.children {
		ckeys := child.shortcut_for(ac)
		if len(ckeys) > 0 {
			return append(append(keys, key), ckeys...)
		}
	}
	return
}

func (self *ShortcutMap[T]) add(ac T, keys []string) (conflict T) {
	sm := self
	last := len(keys) - 1
	for i, key := range keys {
		if i == last {
			if c, found := sm.leaves[key]; found {
				conflict = c
			}
			sm.leaves[key] = ac
			if c, found := sm.children[key]; found {
				conflict = c.first_action()
				delete(sm.children, key)
			}
		} else {
			if c, found := sm.leaves[key]; found {
				conflict = c
				delete(sm.leaves, key)
			}
			q := sm.children[key]
			if q == nil {
				q = &ShortcutMap[T]{leaves: map[string]T{}, children: map[string]*ShortcutMap[T]{}}
				sm.children[key] = q
			}
			sm = q
		}
	}
	return
}