mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
arrays: add map_of_counts/1, map_of_indexes/1 as well as index_of_first/2, index_of_last/2 utilities (#16618)
This commit is contained in:
29
vlib/arrays/map_of.v
Normal file
29
vlib/arrays/map_of.v
Normal file
@ -0,0 +1,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 occures 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
|
||||
}
|
Reference in New Issue
Block a user