Skip to content

Commit f6d8a5d

Browse files
authored
Merge pull request #17 from cuviper/deprecate-inserts
Add more back/front methods and deprecate unspecified inserts
2 parents 5698b84 + 72b7032 commit f6d8a5d

File tree

7 files changed

+243
-88
lines changed

7 files changed

+243
-88
lines changed

benches/bench.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,24 @@ fn entry_hashmap_150(b: &mut Bencher) {
226226
}
227227

228228
#[bench]
229-
fn entry_ringmap_150(b: &mut Bencher) {
229+
fn entry_ringmap_back_150(b: &mut Bencher) {
230230
let c = 150;
231231
b.iter(|| {
232232
let mut map = RingMap::with_capacity(c);
233233
for x in 0..c {
234-
map.entry(x).or_insert(());
234+
map.entry(x).or_push_back(());
235+
}
236+
map
237+
});
238+
}
239+
240+
#[bench]
241+
fn entry_ringmap_front_150(b: &mut Bencher) {
242+
let c = 150;
243+
b.iter(|| {
244+
let mut map = RingMap::with_capacity(c);
245+
for x in 0..c {
246+
map.entry(x).or_push_front(());
235247
}
236248
map
237249
});

src/map.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -442,27 +442,16 @@ where
442442
/// Computes in **O(1)** time (amortized average).
443443
///
444444
/// See also [`entry`][Self::entry] if you want to insert *or* modify,
445-
/// or [`insert_full`][Self::insert_full] if you need to get the index of
446-
/// the corresponding key-value pair.
445+
/// or [`push_back`][Self::push_back] or [`push_front`][Self::push_front]
446+
/// if you need to get the index of the corresponding key-value pair,
447+
/// explicitly choosing which position to make new insertions.
447448
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
448-
self.insert_full(key, value).1
449+
self.push_back(key, value).1
449450
}
450451

451-
/// Insert a key-value pair in the map, and get their index.
452-
///
453-
/// If an equivalent key already exists in the map: the key remains and
454-
/// retains in its place in the order, its corresponding value is updated
455-
/// with `value`, and the older value is returned inside `(index, Some(_))`.
456-
///
457-
/// If no equivalent key existed in the map: the new key-value pair is
458-
/// inserted, last in order, and `(index, None)` is returned.
459-
///
460-
/// Computes in **O(1)** time (amortized average).
461-
///
462-
/// See also [`entry`][Self::entry] if you want to insert *or* modify.
452+
#[deprecated = "use `push_back` or `push_front` instead"]
463453
pub fn insert_full(&mut self, key: K, value: V) -> (usize, Option<V>) {
464-
let hash = self.hash(&key);
465-
self.core.push_back(hash, key, value)
454+
self.push_back(key, value)
466455
}
467456

468457
/// Appends or updates a key-value pair in the map, and get their index.

src/map/core/entry.rs

Lines changed: 134 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,52 @@ pub enum Entry<'a, K, V> {
3737
}
3838

3939
impl<'a, K, V> Entry<'a, K, V> {
40-
/// Return the index where the key-value pair exists or will be inserted.
40+
/// Return the index where the key-value pair exists or may be appended.
41+
///
42+
/// Note that some methods may instead prepend new items at index 0.
4143
pub fn index(&self) -> usize {
4244
match *self {
4345
Entry::Occupied(ref entry) => entry.index(),
4446
Entry::Vacant(ref entry) => entry.index(),
4547
}
4648
}
4749

48-
/// Sets the value of the entry (after inserting if vacant), and returns an `OccupiedEntry`.
50+
#[deprecated = "use `push_back_entry` or `push_front_entry` instead"]
51+
pub fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V> {
52+
self.push_back_entry(value)
53+
}
54+
55+
/// Sets the value of the entry (after appending if vacant), and returns an `OccupiedEntry`.
4956
///
5057
/// Computes in **O(1)** time (amortized average).
51-
pub fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V> {
58+
pub fn push_back_entry(self, value: V) -> OccupiedEntry<'a, K, V> {
5259
match self {
5360
Entry::Occupied(mut entry) => {
5461
entry.insert(value);
5562
entry
5663
}
57-
Entry::Vacant(entry) => entry.insert_entry(value),
64+
Entry::Vacant(entry) => entry.push_back_entry(value),
5865
}
5966
}
6067

61-
/// Inserts the given default value in the entry if it is vacant and returns a mutable
62-
/// reference to it. Otherwise a mutable reference to an already existent value is returned.
68+
/// Sets the value of the entry (after prepending if vacant), and returns an `OccupiedEntry`.
6369
///
6470
/// Computes in **O(1)** time (amortized average).
65-
pub fn or_insert(self, default: V) -> &'a mut V {
71+
pub fn push_front_entry(self, value: V) -> OccupiedEntry<'a, K, V> {
6672
match self {
67-
Entry::Occupied(entry) => entry.into_mut(),
68-
Entry::Vacant(entry) => entry.insert(default),
73+
Entry::Occupied(mut entry) => {
74+
entry.insert(value);
75+
entry
76+
}
77+
Entry::Vacant(entry) => entry.push_front_entry(value),
6978
}
7079
}
7180

81+
#[deprecated = "use `or_push_back` or `or_push_front` instead"]
82+
pub fn or_insert(self, default: V) -> &'a mut V {
83+
self.or_push_back(default)
84+
}
85+
7286
/// Appends the given default value in the entry if it is vacant and returns a mutable
7387
/// reference to it. Otherwise a mutable reference to an already existent value is returned.
7488
///
@@ -91,34 +105,112 @@ impl<'a, K, V> Entry<'a, K, V> {
91105
}
92106
}
93107

94-
/// Inserts the result of the `call` function in the entry if it is vacant and returns a mutable
108+
#[deprecated = "use `or_push_back_default` or `or_push_front_default` instead"]
109+
pub fn or_default(self) -> &'a mut V
110+
where
111+
V: Default,
112+
{
113+
self.or_push_back_default()
114+
}
115+
116+
/// Appends a default-constructed value in the entry if it is vacant and returns a mutable
117+
/// reference to it. Otherwise a mutable reference to an already existent value is returned.
118+
///
119+
/// Computes in **O(1)** time (amortized average).
120+
pub fn or_push_back_default(self) -> &'a mut V
121+
where
122+
V: Default,
123+
{
124+
self.or_push_back_with(V::default)
125+
}
126+
127+
/// Prepends a default-constructed value in the entry if it is vacant and returns a mutable
95128
/// reference to it. Otherwise a mutable reference to an already existent value is returned.
96129
///
97130
/// Computes in **O(1)** time (amortized average).
131+
pub fn or_push_front_default(self) -> &'a mut V
132+
where
133+
V: Default,
134+
{
135+
self.or_push_front_with(V::default)
136+
}
137+
138+
#[deprecated = "use `or_push_back_with` or `or_push_front_with` instead"]
98139
pub fn or_insert_with<F>(self, call: F) -> &'a mut V
140+
where
141+
F: FnOnce() -> V,
142+
{
143+
self.or_push_back_with(call)
144+
}
145+
146+
/// Appends the result of the `call` function in the entry if it is vacant and returns a mutable
147+
/// reference to it. Otherwise a mutable reference to an already existent value is returned.
148+
///
149+
/// Computes in **O(1)** time (amortized average).
150+
pub fn or_push_back_with<F>(self, call: F) -> &'a mut V
99151
where
100152
F: FnOnce() -> V,
101153
{
102154
match self {
103155
Entry::Occupied(entry) => entry.into_mut(),
104-
Entry::Vacant(entry) => entry.insert(call()),
156+
Entry::Vacant(entry) => entry.push_back(call()),
105157
}
106158
}
107159

108-
/// Inserts the result of the `call` function with a reference to the entry's key if it is
160+
/// Prepends the result of the `call` function in the entry if it is vacant and returns a mutable
161+
/// reference to it. Otherwise a mutable reference to an already existent value is returned.
162+
///
163+
/// Computes in **O(1)** time (amortized average).
164+
pub fn or_push_front_with<F>(self, call: F) -> &'a mut V
165+
where
166+
F: FnOnce() -> V,
167+
{
168+
match self {
169+
Entry::Occupied(entry) => entry.into_mut(),
170+
Entry::Vacant(entry) => entry.push_front(call()),
171+
}
172+
}
173+
174+
#[deprecated = "use `or_push_back_with_key` or `or_push_front_with_key` instead"]
175+
pub fn or_insert_with_key<F>(self, call: F) -> &'a mut V
176+
where
177+
F: FnOnce(&K) -> V,
178+
{
179+
self.or_push_back_with_key(call)
180+
}
181+
182+
/// Appends the result of the `call` function with a reference to the entry's key if it is
109183
/// vacant, and returns a mutable reference to the new value. Otherwise a mutable reference to
110184
/// an already existent value is returned.
111185
///
112186
/// Computes in **O(1)** time (amortized average).
113-
pub fn or_insert_with_key<F>(self, call: F) -> &'a mut V
187+
pub fn or_push_back_with_key<F>(self, call: F) -> &'a mut V
114188
where
115189
F: FnOnce(&K) -> V,
116190
{
117191
match self {
118192
Entry::Occupied(entry) => entry.into_mut(),
119193
Entry::Vacant(entry) => {
120194
let value = call(&entry.key);
121-
entry.insert(value)
195+
entry.push_back(value)
196+
}
197+
}
198+
}
199+
200+
/// Prepends the result of the `call` function with a reference to the entry's key if it is
201+
/// vacant, and returns a mutable reference to the new value. Otherwise a mutable reference to
202+
/// an already existent value is returned.
203+
///
204+
/// Computes in **O(1)** time (amortized average).
205+
pub fn or_push_front_with_key<F>(self, call: F) -> &'a mut V
206+
where
207+
F: FnOnce(&K) -> V,
208+
{
209+
match self {
210+
Entry::Occupied(entry) => entry.into_mut(),
211+
Entry::Vacant(entry) => {
212+
let value = call(&entry.key);
213+
entry.push_front(value)
122214
}
123215
}
124216
}
@@ -142,20 +234,6 @@ impl<'a, K, V> Entry<'a, K, V> {
142234
}
143235
self
144236
}
145-
146-
/// Inserts a default-constructed value in the entry if it is vacant and returns a mutable
147-
/// reference to it. Otherwise a mutable reference to an already existent value is returned.
148-
///
149-
/// Computes in **O(1)** time (amortized average).
150-
pub fn or_default(self) -> &'a mut V
151-
where
152-
V: Default,
153-
{
154-
match self {
155-
Entry::Occupied(entry) => entry.into_mut(),
156-
Entry::Vacant(entry) => entry.insert(V::default()),
157-
}
158-
}
159237
}
160238

161239
impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Entry<'_, K, V> {
@@ -392,7 +470,10 @@ pub struct VacantEntry<'a, K, V> {
392470
}
393471

394472
impl<'a, K, V> VacantEntry<'a, K, V> {
395-
/// Return the index where a key-value pair may be inserted.
473+
/// Return the index where a key-value pair may be appended.
474+
///
475+
/// Note that some methods may instead prepend new items at index 0, or
476+
/// even insert at arbitrary indexes in-between.
396477
pub fn index(&self) -> usize {
397478
self.map.indices.len()
398479
}
@@ -411,18 +492,17 @@ impl<'a, K, V> VacantEntry<'a, K, V> {
411492
self.key
412493
}
413494

414-
/// Inserts the entry's key and the given value into the map,
415-
/// and returns a mutable reference to the value.
495+
#[deprecated = "use `push_back` or `push_front` instead"]
416496
pub fn insert(self, value: V) -> &'a mut V {
417-
// this is now redundant...
418497
self.push_back(value)
419498
}
420499

421-
/// Inserts the entry's key and the given value into the map, and returns an `OccupiedEntry`.
422-
///
423-
/// Computes in **O(1)** time (amortized average).
424-
pub fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V> {
425-
self.map.push_back_unique(self.hash, self.key, value)
500+
/// Appends the entry's key and the given value onto the map,
501+
/// and returns a mutable reference to the value.
502+
pub fn push_back(self, value: V) -> &'a mut V {
503+
self.map
504+
.push_back_unique(self.hash, self.key, value)
505+
.into_mut()
426506
}
427507

428508
/// Prepends the entry's key and the given value onto the map,
@@ -433,12 +513,23 @@ impl<'a, K, V> VacantEntry<'a, K, V> {
433513
.into_mut()
434514
}
435515

436-
/// Appends the entry's key and the given value onto the map,
437-
/// and returns a mutable reference to the value.
438-
pub fn push_back(self, value: V) -> &'a mut V {
439-
self.map
440-
.push_back_unique(self.hash, self.key, value)
441-
.into_mut()
516+
#[deprecated = "use `push_back_entry` or `push_front_entry` instead"]
517+
pub fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V> {
518+
self.push_back_entry(value)
519+
}
520+
521+
/// Appends the entry's key and the given value into the map, and returns an `OccupiedEntry`.
522+
///
523+
/// Computes in **O(1)** time (amortized average).
524+
pub fn push_back_entry(self, value: V) -> OccupiedEntry<'a, K, V> {
525+
self.map.push_back_unique(self.hash, self.key, value)
526+
}
527+
528+
/// Prepends the entry's key and the given value into the map, and returns an `OccupiedEntry`.
529+
///
530+
/// Computes in **O(1)** time (amortized average).
531+
pub fn push_front_entry(self, value: V) -> OccupiedEntry<'a, K, V> {
532+
self.map.push_front_unique(self.hash, self.key, value)
442533
}
443534

444535
/// Inserts the entry's key and the given value into the map at its ordered

0 commit comments

Comments
 (0)