File: list.go

package info (click to toggle)
aptly 1.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 49,928 kB
  • sloc: python: 10,398; sh: 252; makefile: 184
file content (140 lines) | stat: -rw-r--r-- 2,447 bytes parent folder | download | duplicates (6)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package utils

import (
	"fmt"
	"sort"
)

// StringsIsSubset checks that subset is strict subset of full, and returns
// error formatted with errorFmt otherwise
func StringsIsSubset(subset, full []string, errorFmt string) error {
	for _, checked := range subset {
		found := false
		for _, s := range full {
			if checked == s {
				found = true
				break
			}
		}
		if !found {
			return fmt.Errorf(errorFmt, checked)
		}
	}
	return nil
}

// StrSlicesEqual compares two slices for equality
func StrSlicesEqual(s1, s2 []string) bool {
	if len(s1) != len(s2) {
		return false
	}

	for i, s := range s1 {
		if s != s2[i] {
			return false
		}
	}

	return true
}

// StrMapsEqual compares two map[string]string
func StrMapsEqual(m1, m2 map[string]string) bool {
	if len(m1) != len(m2) {
		return false
	}

	for k, v := range m1 {
		v2, ok := m2[k]
		if !ok || v != v2 {
			return false
		}
	}

	return true
}

// StrSliceHasItem checks item for presence in slice
func StrSliceHasItem(s []string, item string) bool {
	for _, v := range s {
		if v == item {
			return true
		}
	}
	return false
}

// StrMapSortedKeys returns keys of map[string]string sorted
func StrMapSortedKeys(m map[string]string) []string {
	keys := make([]string, len(m))
	i := 0
	for k := range m {
		keys[i] = k
		i++
	}
	sort.Strings(keys)
	return keys
}

// StrSliceDeduplicate removes dups in slice
func StrSliceDeduplicate(s []string) []string {
	l := len(s)
	if l < 2 {
		return s
	}
	if l == 2 {
		if s[0] == s[1] {
			return s[0:1]
		}
		return s
	}

	found := make(map[string]bool, l)
	j := 0
	for i, x := range s {
		if !found[x] {
			found[x] = true
			s[j] = s[i]
			j++
		}
	}

	return s[:j]
}

// StrSlicesSubstract finds all the strings which are in l but not in r, both slices shoult be sorted
func StrSlicesSubstract(l, r []string) []string {
	var result []string

	// pointer to left and right reflists
	il, ir := 0, 0
	// length of reflists
	ll, lr := len(l), len(r)

	for il < ll || ir < lr {
		if il == ll {
			// left list exhausted, we got the result
			break
		}
		if ir == lr {
			// right list exhausted, append what is left to result
			result = append(result, l[il:]...)
			break
		}

		if l[il] == r[ir] {
			// r contains entry from l, so we skip it
			il++
			ir++
		} else if l[il] < r[ir] {
			// item il is not in r, append
			result = append(result, l[il])
			il++
		} else {
			// skip over to next item in r
			ir++
		}
	}

	return result
}