Debian Codegraph @

backlog.go

raw symbols SCIP
1 package main
2
3 // A Backlog represents a queue of file paths that may be received while we're
4 // still running a command. There are a couple of different policies for how to
5 // handle this. If there are no {} (substitution sequences) in the command, then
6 // we only need to preserve one of the paths. If there is a {}, then we need to
7 // preserve each unique path in the backlog.
8 type Backlog interface {
9 // Add a path to the backlog.
10 Add(path string)
11 // Show what path should be processed next.
12 Next() string
13 // Remove the next path from the backlog and return whether
14 // the backlog is now empty.
15 RemoveOne() (empty bool)
16 }
17
18 // A UnifiedBacklog only remembers one backlog item at a time.
19 type UnifiedBacklog struct {
20 s string
21 empty bool
22 }
23
24 func NewUnifiedBacklog() *UnifiedBacklog {
25 return &UnifiedBacklog{empty: true}
26 }
27
28 // Add adds path to b if there is not a path there currently.
29 // Otherwise it discards it.
30 func (b *UnifiedBacklog) Add(path string) {
31 if b.empty {
32 b.s = path
33 b.empty = false
34 }
35 }
36
37 // Next returns the path in b.
38 func (b *UnifiedBacklog) Next() string {
39 if b.empty {
40 panic("Next() called on empty backlog")
41 }
42 return b.s
43 }
44
45 // RemoveOne removes the path in b.
46 func (b *UnifiedBacklog) RemoveOne() bool {
47 if b.empty {
48 panic("RemoveOne() called on empty backlog")
49 }
50 b.empty = true
51 b.s = ""
52 return true
53 }
54
55 // A UniqueFilesBacklog keeps a set of the paths it has received.
56 type UniqueFilesBacklog struct {
57 empty bool
58 next string
59 rest map[string]struct{}
60 }
61
62 func NewUniqueFilesBacklog() *UniqueFilesBacklog {
63 return &UniqueFilesBacklog{
64 empty: true,
65 next: "",
66 rest: make(map[string]struct{}),
67 }
68 }
69
70 // Add adds path to the set of files in b.
71 func (b *UniqueFilesBacklog) Add(path string) {
72 defer func() { b.empty = false }()
73 if b.empty {
74 b.next = path
75 return
76 }
77 if path == b.next {
78 return
79 }
80 b.rest[path] = struct{}{}
81 }
82
83 // Next returns one of the paths in b.
84 func (b *UniqueFilesBacklog) Next() string {
85 if b.empty {
86 panic("Next() called on empty backlog")
87 }
88 return b.next
89 }
90
91 // RemoveOne removes one of the paths from b (the same path that was returned by
92 // a preceding call to Next).
93 func (b *UniqueFilesBacklog) RemoveOne() bool {
94 if b.empty {
95 panic("RemoveOne() called on empty backlog")
96 }
97 if len(b.rest) == 0 {
98 b.next = ""
99 b.empty = true
100 return true
101 }
102 for next := range b.rest {
103 b.next = next
104 break
105 }
106 delete(b.rest, b.next)
107 return false
108 }