-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_list.go
70 lines (62 loc) · 1.41 KB
/
set_list.go
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
65
66
67
68
69
70
package mpq
import (
"sort"
"strings"
)
type set_list struct {
combined_list []string
index int
}
func (set *Set) combine_list() (err error) {
// merge lists into a dictionary to avoid repetition
var dict = map[string]string{}
var list List
for _, archive := range set.archives {
list, err = archive.List()
if err != nil {
return err
}
for list.Next() {
path := list.Path()
dict[strings.ToLower(path)] = path
}
list.Close()
}
// combine dictionary into a sorted slice of strings
set.combined_list = make([]string, len(dict))
i := 0
for _, v := range dict {
set.combined_list[i] = v
i++
}
dict = nil
sort.Strings(set.combined_list)
return
}
func (list *set_list) Next() bool {
return list.index < len(list.combined_list)
}
func (list *set_list) Path() string {
path := list.combined_list[list.index]
list.index++
return path
}
func (list *set_list) Close() error {
return nil
}
// (Slow)
// Returns a combined List for all the archives in the set.
// Call this after you've loaded all your Archives,
// the results are then frozen in memory and fast after the first slow call
func (set *Set) List() (list List, err error) {
if len(set.combined_list) == 0 {
if err = set.combine_list(); err != nil {
return
}
}
list = &set_list{
index: 0,
combined_list: set.combined_list,
}
return
}