Skip to content

Commit 6740685

Browse files
committed
Make IndexMapCore fields private
1 parent 643f11b commit 6740685

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

@@ -273,36 +271,36 @@ where
273271
/// Return an iterator over the key-value pairs of the map, in their order
274272
pub fn iter(&self) -> Iter<K, V> {
275273
Iter {
276-
iter: self.core.entries.iter(),
274+
iter: self.as_entries().iter(),
277275
}
278276
}
279277

280278
/// Return an iterator over the key-value pairs of the map, in their order
281279
pub fn iter_mut(&mut self) -> IterMut<K, V> {
282280
IterMut {
283-
iter: self.core.entries.iter_mut(),
281+
iter: self.as_entries_mut().iter_mut(),
284282
}
285283
}
286284

287285
/// Return an iterator over the keys of the map, in their order
288286
pub fn keys(&self) -> Keys<K, V> {
289287
Keys {
290-
iter: self.core.entries.iter(),
288+
iter: self.as_entries().iter(),
291289
}
292290
}
293291

294292
/// Return an iterator over the values of the map, in their order
295293
pub fn values(&self) -> Values<K, V> {
296294
Values {
297-
iter: self.core.entries.iter(),
295+
iter: self.as_entries().iter(),
298296
}
299297
}
300298

301299
/// Return an iterator over mutable references to the the values of the map,
302300
/// in their order
303301
pub fn values_mut(&mut self) -> ValuesMut<K, V> {
304302
ValuesMut {
305-
iter: self.core.entries.iter_mut(),
303+
iter: self.as_entries_mut().iter_mut(),
306304
}
307305
}
308306

@@ -333,7 +331,7 @@ where
333331
Q: Hash + Equivalent<K>,
334332
{
335333
if let Some(found) = self.get_index_of(key) {
336-
let entry = &self.core.entries[found];
334+
let entry = &self.as_entries()[found];
337335
Some((found, &entry.key, &entry.value))
338336
} else {
339337
None
@@ -374,7 +372,7 @@ where
374372
Q: Hash + Equivalent<K>,
375373
{
376374
if let Some((_, found)) = self.find(key) {
377-
let entry = &mut self.core.entries[found];
375+
let entry = &mut self.as_entries_mut()[found];
378376
Some((found, &mut entry.key, &mut entry.value))
379377
} else {
380378
None
@@ -548,8 +546,7 @@ where
548546
where
549547
F: FnMut(&K, &V, &K, &V) -> Ordering,
550548
{
551-
self.core
552-
.entries
549+
self.as_entries_mut()
553550
.sort_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
554551
self.into_iter()
555552
}
@@ -564,10 +561,8 @@ where
564561
/// Clears the `IndexMap`, returning all key-value pairs as a drain iterator.
565562
/// Keeps the allocated memory for reuse.
566563
pub fn drain(&mut self, range: RangeFull) -> Drain<K, V> {
567-
self.core.clear_indices();
568-
569564
Drain {
570-
iter: self.core.entries.drain(range),
565+
iter: self.core.drain(range),
571566
}
572567
}
573568
}
@@ -586,7 +581,7 @@ impl<K, V, S> IndexMap<K, V, S> {
586581
///
587582
/// Computes in **O(1)** time.
588583
pub fn get_index(&self, index: usize) -> Option<(&K, &V)> {
589-
self.core.entries.get(index).map(Bucket::refs)
584+
self.as_entries().get(index).map(Bucket::refs)
590585
}
591586

592587
/// Get a key-value pair by index
@@ -595,7 +590,7 @@ impl<K, V, S> IndexMap<K, V, S> {
595590
///
596591
/// Computes in **O(1)** time.
597592
pub fn get_index_mut(&mut self, index: usize) -> Option<(&mut K, &mut V)> {
598-
self.core.entries.get_mut(index).map(Bucket::muts)
593+
self.as_entries_mut().get_mut(index).map(Bucket::muts)
599594
}
600595

601596
/// Remove the key-value pair by index
@@ -609,8 +604,7 @@ impl<K, V, S> IndexMap<K, V, S> {
609604
/// Computes in **O(1)** time (average).
610605
pub fn swap_remove_index(&mut self, index: usize) -> Option<(K, V)> {
611606
let (probe, found) = match self
612-
.core
613-
.entries
607+
.as_entries()
614608
.get(index)
615609
.map(|e| self.core.find_existing_entry(e))
616610
{
@@ -631,8 +625,7 @@ impl<K, V, S> IndexMap<K, V, S> {
631625
/// Computes in **O(n)** time (average).
632626
pub fn shift_remove_index(&mut self, index: usize) -> Option<(K, V)> {
633627
let (probe, found) = match self
634-
.core
635-
.entries
628+
.as_entries()
636629
.get(index)
637630
.map(|e| self.core.find_existing_entry(e))
638631
{
@@ -931,7 +924,7 @@ where
931924
type IntoIter = IntoIter<K, V>;
932925
fn into_iter(self) -> Self::IntoIter {
933926
IntoIter {
934-
iter: self.core.entries.into_iter(),
927+
iter: self.into_entries().into_iter(),
935928
}
936929
}
937930
}

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

@@ -362,11 +364,36 @@ where
362364
/// Core of the map that does not depend on S
363365
#[derive(Clone)]
364366
pub(crate) struct IndexMapCore<K, V> {
365-
pub(crate) mask: usize,
367+
mask: usize,
366368
/// indices are the buckets. indices.len() == raw capacity
367-
pub(crate) indices: Box<[Pos]>,
369+
indices: Box<[Pos]>,
368370
/// entries is a dense vec of entries in their order. entries.len() == len
369-
pub(crate) entries: Vec<Bucket<K, V>>,
371+
entries: Vec<Bucket<K, V>>,
372+
}
373+
374+
impl<K, V> Entries for IndexMapCore<K, V> {
375+
type Entry = Bucket<K, V>;
376+
377+
fn into_entries(self) -> Vec<Self::Entry> {
378+
self.entries
379+
}
380+
381+
fn as_entries(&self) -> &[Self::Entry] {
382+
&self.entries
383+
}
384+
385+
fn as_entries_mut(&mut self) -> &mut [Self::Entry] {
386+
&mut self.entries
387+
}
388+
389+
fn with_entries<F>(&mut self, f: F)
390+
where
391+
F: FnOnce(&mut [Self::Entry]),
392+
{
393+
let side_index = self.save_hash_index();
394+
f(&mut self.entries);
395+
self.restore_hash_index(side_index);
396+
}
370397
}
371398

372399
impl<K, V> IndexMapCore<K, V> {
@@ -421,8 +448,13 @@ impl<K, V> IndexMapCore<K, V> {
421448
self.clear_indices();
422449
}
423450

451+
pub(crate) fn drain(&mut self, range: RangeFull) -> Drain<Bucket<K, V>> {
452+
self.clear_indices();
453+
self.entries.drain(range)
454+
}
455+
424456
// clear self.indices to the same state as "no elements"
425-
pub(crate) fn clear_indices(&mut self) {
457+
fn clear_indices(&mut self) {
426458
for pos in self.indices.iter_mut() {
427459
*pos = Pos::none();
428460
}
@@ -820,7 +852,7 @@ impl<K, V> IndexMapCore<K, V> {
820852
}
821853
}
822854

823-
pub(crate) fn save_hash_index(&mut self) -> Vec<usize> {
855+
fn save_hash_index(&mut self) -> Vec<usize> {
824856
// Temporarily use the hash field in a bucket to store the old index.
825857
// Save the old hash values in `side_index`. Then we can sort
826858
// `self.entries` in place.
@@ -829,7 +861,7 @@ impl<K, V> IndexMapCore<K, V> {
829861
)
830862
}
831863

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

0 commit comments

Comments
 (0)