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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::collections::HashMap;
use std::hash::Hash;
/// Counts the number of occurrences of each value in a collection after applying a mapper function.
///
/// This function iterates over a slice of items, applies the mapper function to each item, and returns a `HashMap`
/// where each key is the mapped value, and the corresponding value is the number of times that mapped value appears.
///
/// **Time Complexity:** O(n), where n is the number of elements in the collection.
///
/// # Arguments
///
/// * `collection` - A slice of items to be counted.
/// * `mapper` - A function that maps an item of type `T` to a key of type `U`.
///
/// # Type Parameters
///
/// * `T` - The type of elements in the input collection.
/// * `U` - The type of keys in the resulting `HashMap`. Must implement `Hash`, `Eq`, and `Clone`.
///
/// # Returns
///
/// * `HashMap<U, usize>` - A map where keys are the mapped values from the collection and values are their counts.
///
/// # Examples
///
/// ```rust
/// use lowdash::count_values_by;
/// use std::collections::HashMap;
///
/// let chars = vec!['a', 'b', 'a', 'c', 'b', 'd'];
/// let result = count_values_by(&chars, |x| x.clone());
/// let mut expected = HashMap::new();
/// expected.insert('a', 2);
/// expected.insert('b', 2);
/// expected.insert('c', 1);
/// expected.insert('d', 1);
/// assert_eq!(result, expected);
/// ```
///
/// ```rust
/// use lowdash::count_values_by;
/// use std::collections::HashMap;
///
/// let numbers = vec![1, 2, 2, 3, 4, 3, 5];
/// let result = count_values_by(&numbers, |x| *x);
/// let mut expected = HashMap::new();
/// expected.insert(1, 1);
/// expected.insert(2, 2);
/// expected.insert(3, 2);
/// expected.insert(4, 1);
/// expected.insert(5, 1);
/// assert_eq!(result, expected);
/// ```
pub fn count_values_by<T, U, F>(collection: &[T], mapper: F) -> HashMap<U, usize>
where
U: Hash + Eq + Clone,
F: Fn(&T) -> U,
{
let mut result = HashMap::new();
for item in collection {
let key = mapper(item);
*result.entry(key).or_insert(0) += 1;
}
result
}
#[cfg(test)]
mod tests {
use super::*;
use crate::common::Float;
use std::collections::HashMap;
#[test]
fn test_count_values_by_integers() {
let numbers = vec![1, 2, 2, 3, 4, 3, 5];
let result = count_values_by(&numbers, |x| *x);
let mut expected = HashMap::new();
expected.insert(1, 1);
expected.insert(2, 2);
expected.insert(3, 2);
expected.insert(4, 1);
expected.insert(5, 1);
assert_eq!(result, expected);
}
#[test]
fn test_count_values_by_strings() {
let strings = vec!["apple", "banana", "apple", "cherry", "banana"];
let result = count_values_by(&strings, |x| x.to_string());
let mut expected = HashMap::new();
expected.insert("apple".to_string(), 2);
expected.insert("banana".to_string(), 2);
expected.insert("cherry".to_string(), 1);
assert_eq!(result, expected);
}
#[test]
fn test_count_values_by_structs() {
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person {
name: "Alice".to_string(),
age: 25,
},
Person {
name: "Bob".to_string(),
age: 30,
},
Person {
name: "Alice".to_string(),
age: 25,
},
Person {
name: "Carol".to_string(),
age: 35,
},
];
let result = count_values_by(&people, |p| p.clone());
let mut expected = HashMap::new();
expected.insert(
Person {
name: "Alice".to_string(),
age: 25,
},
2,
);
expected.insert(
Person {
name: "Bob".to_string(),
age: 30,
},
1,
);
expected.insert(
Person {
name: "Carol".to_string(),
age: 35,
},
1,
);
assert_eq!(result, expected);
}
#[test]
fn test_count_values_by_with_floats() {
let float_collection = vec![
Float(1.1),
Float(2.2),
Float(2.2),
Float(3.3),
Float(4.4),
Float(3.3),
Float(5.5),
];
let result = count_values_by(&float_collection, |f| f.clone());
let mut expected = HashMap::new();
expected.insert(Float(1.1), 1);
expected.insert(Float(2.2), 2);
expected.insert(Float(3.3), 2);
expected.insert(Float(4.4), 1);
expected.insert(Float(5.5), 1);
assert_eq!(result, expected);
}
#[test]
fn test_count_values_by_with_optionals() {
let collection = vec![Some(1), None, Some(2), Some(1), None, Some(3), Some(2)];
let result = count_values_by(&collection, |x| x.clone());
let mut expected = HashMap::new();
expected.insert(Some(1), 2);
expected.insert(None, 2);
expected.insert(Some(2), 2);
expected.insert(Some(3), 1);
assert_eq!(result, expected);
}
#[test]
fn test_count_values_by_with_identity_mapper() {
let chars = vec!['a', 'b', 'a', 'c', 'b', 'd'];
let result = count_values_by(&chars, |x| x.clone());
let mut expected = HashMap::new();
expected.insert('a', 2);
expected.insert('b', 2);
expected.insert('c', 1);
expected.insert('d', 1);
assert_eq!(result, expected);
}
#[test]
fn test_count_values_by_empty_collection() {
let empty: Vec<i32> = vec![];
let result: HashMap<i32, usize> = count_values_by(&empty, |x| *x);
let expected: HashMap<i32, usize> = HashMap::new();
assert_eq!(result, expected);
}
}