/
map_of.v
29 lines (27 loc) · 959 Bytes
/
map_of.v
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
module arrays
// map_of_indexes returns a map, where each key is an unique value in `array`,
// and each value for that key is an array, containing the indexes in `array`,
// where that value has been found.
// Example: arrays.map_of_indexes([1,2,3,4,4,2,1,4,4,999]) == {1: [0, 6], 2: [1, 5], 3: [2], 4: [3, 4, 7, 8], 999: [9]}
pub fn map_of_indexes[T](array []T) map[T][]int {
mut result := map[T][]int{}
for i, e in array {
if _ := result[e] {
result[e] << i
} else {
result[e] = [i]
}
}
return result
}
// map_of_counts returns a map, where each key is an unique value in `array`,
// and each value for that key is how many times that value occurs in `array`.
// It can be useful for building histograms of discrete measurements.
// Example: arrays.map_of_counts([1,2,3,4,4,2,1,4,4]) == {1: 2, 2: 2, 3: 1, 4: 4}
pub fn map_of_counts[T](array []T) map[T]int {
mut result := map[T]int{}
for e in array {
result[e]++
}
return result
}