Skip to content

Commit 7247db6

Browse files
committed
Make IndexMapCore fields private
1 parent 8f53c9a commit 7247db6

File tree

2 files changed

+58
-33
lines changed

2 files changed

+58
-33
lines changed

src/map.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,22 @@ impl<K, V, S> Entries for IndexMap<K, V, S> {
9494
type Entry = Bucket<K, V>;
9595

9696
fn into_entries(self) -> Vec<Self::Entry> {
97-
self.core.entries
97+
self.core.into_entries()
9898
}
9999

100100
fn as_entries(&self) -> &[Self::Entry] {
101-
&self.core.entries
101+
self.core.as_entries()
102102
}
103103

104104
fn as_entries_mut(&mut self) -> &mut [Self::Entry] {
105-
&mut self.core.entries
105+
self.core.as_entries_mut()
106106
}
107107

108108
fn with_entries<F>(&mut self, f: F)
109109
where
110110
F: FnOnce(&mut [Self::Entry]),
111111
{
112-
let side_index = self.core.save_hash_index();
113-
f(&mut self.core.entries);
114-
self.core.restore_hash_index(side_index);
112+
self.core.with_entries(f);
115113
}
116114
}
117115

@@ -282,36 +280,36 @@ where
282280
/// Return an iterator over the key-value pairs of the map, in their order
283281
pub fn iter(&self) -> Iter<K, V> {
284282
Iter {
285-
iter: self.core.entries.iter(),
283+
iter: self.as_entries().iter(),
286284
}
287285
}
288286

289287
/// Return an iterator over the key-value pairs of the map, in their order
290288
pub fn iter_mut(&mut self) -> IterMut<K, V> {
291289
IterMut {
292-
iter: self.core.entries.iter_mut(),
290+
iter: self.as_entries_mut().iter_mut(),
293291
}
294292
}
295293

296294
/// Return an iterator over the keys of the map, in their order
297295
pub fn keys(&self) -> Keys<K, V> {
298296
Keys {
299-
iter: self.core.entries.iter(),
297+
iter: self.as_entries().iter(),
300298
}
301299
}
302300

303301
/// Return an iterator over the values of the map, in their order
304302
pub fn values(&self) -> Values<K, V> {
305303
Values {
306-
iter: self.core.entries.iter(),
304+
iter: self.as_entries().iter(),
307305
}
308306
}
309307

310308
/// Return an iterator over mutable references to the the values of the map,
311309
/// in their order
312310
pub fn values_mut(&mut self) -> ValuesMut<K, V> {
313311
ValuesMut {
314-
iter: self.core.entries.iter_mut(),
312+
iter: self.as_entries_mut().iter_mut(),
315313
}
316314
}
317315

@@ -342,7 +340,7 @@ where
342340
Q: Hash + Equivalent<K>,
343341
{
344342
if let Some(found) = self.get_index_of(key) {
345-
let entry = &self.core.entries[found];
343+
let entry = &self.as_entries()[found];
346344
Some((found, &entry.key, &entry.value))
347345
} else {
348346
None
@@ -383,7 +381,7 @@ where
383381
Q: Hash + Equivalent<K>,
384382
{
385383
if let Some((_, found)) = self.find(key) {
386-
let entry = &mut self.core.entries[found];
384+
let entry = &mut self.as_entries_mut()[found];
387385
Some((found, &mut entry.key, &mut entry.value))
388386
} else {
389387
None
@@ -557,19 +555,16 @@ where
557555
where
558556
F: FnMut(&K, &V, &K, &V) -> Ordering,
559557
{
560-
self.core
561-
.entries
558+
self.as_entries_mut()
562559
.sort_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
563560
self.into_iter()
564561
}
565562

566563
/// Clears the `IndexMap`, returning all key-value pairs as a drain iterator.
567564
/// Keeps the allocated memory for reuse.
568565
pub fn drain(&mut self, range: RangeFull) -> Drain<K, V> {
569-
self.core.clear_indices();
570-
571566
Drain {
572-
iter: self.core.entries.drain(range),
567+
iter: self.core.drain(range),
573568
}
574569
}
575570
}
@@ -588,7 +583,7 @@ impl<K, V, S> IndexMap<K, V, S> {
588583
///
589584
/// Computes in **O(1)** time.
590585
pub fn get_index(&self, index: usize) -> Option<(&K, &V)> {
591-
self.core.entries.get(index).map(Bucket::refs)
586+
self.as_entries().get(index).map(Bucket::refs)
592587
}
593588

594589
/// Get a key-value pair by index
@@ -597,7 +592,7 @@ impl<K, V, S> IndexMap<K, V, S> {
597592
///
598593
/// Computes in **O(1)** time.
599594
pub fn get_index_mut(&mut self, index: usize) -> Option<(&mut K, &mut V)> {
600-
self.core.entries.get_mut(index).map(Bucket::muts)
595+
self.as_entries_mut().get_mut(index).map(Bucket::muts)
601596
}
602597

603598
/// Remove the key-value pair by index
@@ -611,8 +606,7 @@ impl<K, V, S> IndexMap<K, V, S> {
611606
/// Computes in **O(1)** time (average).
612607
pub fn swap_remove_index(&mut self, index: usize) -> Option<(K, V)> {
613608
let (probe, found) = match self
614-
.core
615-
.entries
609+
.as_entries()
616610
.get(index)
617611
.map(|e| self.core.find_existing_entry(e))
618612
{
@@ -633,8 +627,7 @@ impl<K, V, S> IndexMap<K, V, S> {
633627
/// Computes in **O(n)** time (average).
634628
pub fn shift_remove_index(&mut self, index: usize) -> Option<(K, V)> {
635629
let (probe, found) = match self
636-
.core
637-
.entries
630+
.as_entries()
638631
.get(index)
639632
.map(|e| self.core.find_existing_entry(e))
640633
{
@@ -933,7 +926,7 @@ where
933926
type IntoIter = IntoIter<K, V>;
934927
fn into_iter(self) -> Self::IntoIter {
935928
IntoIter {
936-
iter: self.core.entries.into_iter(),
929+
iter: self.into_entries().into_iter(),
937930
}
938931
}
939932
}

src/map_core.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ use std::fmt;
1717
use std::iter::FromIterator;
1818
use std::marker::PhantomData;
1919
use std::mem::replace;
20+
use std::ops::RangeFull;
21+
use std::vec::Drain;
2022

2123
use util::{enumerate, ptrdistance};
22-
use {Bucket, HashValue};
24+
use {Bucket, Entries, HashValue};
2325

2426
/// Trait for the "size class". Either u32 or u64 depending on the index
2527
/// size needed to address an entry's index in self.core.entries.
@@ -146,7 +148,7 @@ impl<Sz> From<ShortHash<Sz>> for HashValue {
146148
/// appropriate. Only the growth code needs some extra logic to handle the
147149
/// transition from one class to another
148150
#[derive(Copy)]
149-
pub(crate) struct Pos {
151+
struct Pos {
150152
index: u64,
151153
}
152154

@@ -368,11 +370,36 @@ enum Inserted<V> {
368370
/// Core of the map that does not depend on S
369371
#[derive(Clone)]
370372
pub(crate) struct IndexMapCore<K, V> {
371-
pub(crate) mask: usize,
373+
mask: usize,
372374
/// indices are the buckets. indices.len() == raw capacity
373-
pub(crate) indices: Box<[Pos]>,
375+
indices: Box<[Pos]>,
374376
/// entries is a dense vec of entries in their order. entries.len() == len
375-
pub(crate) entries: Vec<Bucket<K, V>>,
377+
entries: Vec<Bucket<K, V>>,
378+
}
379+
380+
impl<K, V> Entries for IndexMapCore<K, V> {
381+
type Entry = Bucket<K, V>;
382+
383+
fn into_entries(self) -> Vec<Self::Entry> {
384+
self.entries
385+
}
386+
387+
fn as_entries(&self) -> &[Self::Entry] {
388+
&self.entries
389+
}
390+
391+
fn as_entries_mut(&mut self) -> &mut [Self::Entry] {
392+
&mut self.entries
393+
}
394+
395+
fn with_entries<F>(&mut self, f: F)
396+
where
397+
F: FnOnce(&mut [Self::Entry]),
398+
{
399+
let side_index = self.save_hash_index();
400+
f(&mut self.entries);
401+
self.restore_hash_index(side_index);
402+
}
376403
}
377404

378405
impl<K, V> IndexMapCore<K, V> {
@@ -427,8 +454,13 @@ impl<K, V> IndexMapCore<K, V> {
427454
self.clear_indices();
428455
}
429456

457+
pub(crate) fn drain(&mut self, range: RangeFull) -> Drain<Bucket<K, V>> {
458+
self.clear_indices();
459+
self.entries.drain(range)
460+
}
461+
430462
// clear self.indices to the same state as "no elements"
431-
pub(crate) fn clear_indices(&mut self) {
463+
fn clear_indices(&mut self) {
432464
for pos in self.indices.iter_mut() {
433465
*pos = Pos::none();
434466
}
@@ -871,7 +903,7 @@ impl<K, V> IndexMapCore<K, V> {
871903
self.restore_hash_index(side_index);
872904
}
873905

874-
pub(crate) fn save_hash_index(&mut self) -> Vec<usize> {
906+
fn save_hash_index(&mut self) -> Vec<usize> {
875907
// Temporarily use the hash field in a bucket to store the old index.
876908
// Save the old hash values in `side_index`. Then we can sort
877909
// `self.entries` in place.
@@ -880,7 +912,7 @@ impl<K, V> IndexMapCore<K, V> {
880912
)
881913
}
882914

883-
pub(crate) fn restore_hash_index(&mut self, mut side_index: Vec<usize>) {
915+
fn restore_hash_index(&mut self, mut side_index: Vec<usize>) {
884916
// Write back the hash values from side_index and fill `side_index` with
885917
// a mapping from the old to the new index instead.
886918
for (i, ent) in enumerate(&mut self.entries) {

0 commit comments

Comments
 (0)