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
// SPDX-FileCopyrightText: 2025 maplike contributors
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Abstract Base Containers: traits that bundle multiple operations together.
//!
//! The module's name, `abc`, is borrowed from Python's
//! [`collections.abc`](https://docs.python.org/3/library/collections.abc.html).
use Index;
use crate;
/// Base trait for collections, without any operations defined yet.
///
/// Only declares the value type. Keyed collections also implement [`Keyed`].
/// Base trait for keyed collections, without any operations defined yet.
///
/// Just a key-value map without any methods yet. We however use the name
/// `Keyed` instead of `Map` to distinguish maps from vectors and stable
/// vectors, which also are keyed collections but with slightly different sets
/// of operations.
/// A single assignable value.
///
/// # Examples
///
/// ```
/// use maplike::ops::Assign;
///
/// // `.assign()` replaces the whole value for any `Scalarlike` type.
///
/// // Works for `i32`.
/// let mut count = 1;
/// count.assign(42);
/// assert_eq!(count, 42);
///
/// // Works for `f64`.
/// let mut ratio = 1.0;
/// ratio.assign(3.5);
/// assert_eq!(ratio, 3.5);
///
/// // Works for tuples.
/// let mut point = (1, 2);
/// point.assign((10, 20));
/// assert_eq!(point, (10, 20));
/// ```
pub use crateAssign as Scalarlike;
/// A keyed collection with get, set, insert, remove, clear operations.
///
/// # Examples
///
/// ```
/// use maplike::abc::Maplike;
/// use maplike::ops::{Get, Insert, Set};
/// use std::collections::{BTreeMap, HashMap};
///
/// // Generic over any `Maplike` collection.
/// fn scale_entry<C: Maplike<usize, Value = f64>>(map: &mut C, key: usize, factor: f64) {
/// if let Some(&value) = map.get(&key) {
/// // Scale an existing entry.
/// map.set(key, value * factor);
/// } else {
/// // Insert a new entry when the key is absent.
/// map.insert(key, factor);
/// }
/// }
///
/// // `scale_entry()` works on `HashMap`.
/// let mut hash_map = HashMap::from([(1, 2.0), (2, 3.0)]);
/// scale_entry(&mut hash_map, 1, 10.0);
/// assert_eq!(hash_map.get(&1), Some(&20.0));
/// scale_entry(&mut hash_map, 3, 5.0);
/// assert_eq!(hash_map.get(&3), Some(&5.0));
///
/// // `scale_entry()` works on `BTreeMap`.
/// let mut btree_map = BTreeMap::from([(1, 2.0), (2, 3.0)]);
/// scale_entry(&mut btree_map, 1, 10.0);
/// assert_eq!(btree_map.get(&1), Some(&20.0));
/// scale_entry(&mut btree_map, 3, 5.0);
/// assert_eq!(btree_map.get(&3), Some(&5.0));
/// ```
/// A map-like keyed collection whose value is the unit type, thus behaving like
/// a set.
/// An array-like keyed collection with get, set, len, index operations.
///
/// # Examples
///
/// ```
/// use maplike::abc::Arraylike;
/// use maplike::ops::{Get, Len, Set};
///
/// // Generic over any `Arraylike` collection.
/// fn scale_all<C: Arraylike<usize, Key = usize, Value = f64>>(collection: &mut C, factor: f64) {
/// for i in 0..collection.len() {
/// if let Some(&value) = collection.get(&i) {
/// // Multiply all collection elements by a constant factor.
/// collection.set(i, value * factor);
/// }
/// }
/// }
///
/// // `scale_all()` works on `[T; N]`.
/// let mut arr = [1.0, 2.0, 3.0];
/// scale_all(&mut arr, 10.0);
/// assert_eq!(arr, [10.0, 20.0, 30.0]);
///
/// // `scale_all()` works on `Vec`.
/// let mut vec = vec![4.0, 5.0, 6.0];
/// scale_all(&mut vec, 10.0);
/// assert_eq!(vec, [40.0, 50.0, 60.0]);
///
/// use std::collections::VecDeque;
///
/// // `scale_all()` works on `VecDeque`.
/// let mut deque = VecDeque::from([7.0, 8.0, 9.0]);
/// scale_all(&mut deque, 10.0);
/// assert_eq!(deque.into_iter().collect::<Vec<_>>(), vec![70.0, 80.0, 90.0]);
/// ```
/// An array-like keyed collection with additional push, pop, clear operations.
///
/// # Examples
///
/// ```
/// use maplike::abc::Veclike;
/// use maplike::ops::{Get, Len, Set};
///
/// // Generic over any `Veclike` collection.
/// fn scale_all<C: Veclike<usize, Key = usize, Value = f64>>(collection: &mut C, factor: f64) {
/// for i in 0..collection.len() {
/// if let Some(&value) = collection.get(&i) {
/// // Multiply all collection elements by a constant factor.
/// collection.set(i, value * factor);
/// }
/// }
/// }
///
/// // `scale_all()` works on `Vec`.
/// let mut vec = vec![4.0, 5.0, 6.0];
/// scale_all(&mut vec, 10.0);
/// assert_eq!(vec, [40.0, 50.0, 60.0]);
///
/// use std::collections::VecDeque;
///
/// // `scale_all()` works on `VecDeque`.
/// let mut deque = VecDeque::from([7.0, 8.0, 9.0]);
/// scale_all(&mut deque, 10.0);
/// assert_eq!(deque.into_iter().collect::<Vec<_>>(), vec![70.0, 80.0, 90.0]);
/// ```