Skip to content

Commit 2f27c44

Browse files
authored
Merge pull request #59 from dtolnay/node
Rename LinkedHashMapEntry to Node
2 parents fa9588f + 94e7d49 commit 2f27c44

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

src/lib.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ use std::ptr;
5151

5252
struct KeyRef<K> { k: *const K }
5353

54-
struct LinkedHashMapEntry<K, V> {
55-
next: *mut LinkedHashMapEntry<K, V>,
56-
prev: *mut LinkedHashMapEntry<K, V>,
54+
struct Node<K, V> {
55+
next: *mut Node<K, V>,
56+
prev: *mut Node<K, V>,
5757
key: K,
5858
value: V,
5959
}
6060

6161
/// A linked hash map.
6262
pub struct LinkedHashMap<K, V, S = hash_map::RandomState> {
63-
map: HashMap<KeyRef<K>, *mut LinkedHashMapEntry<K, V>, S>,
64-
head: *mut LinkedHashMapEntry<K, V>,
65-
free: *mut LinkedHashMapEntry<K, V>,
63+
map: HashMap<KeyRef<K>, *mut Node<K, V>, S>,
64+
head: *mut Node<K, V>,
65+
free: *mut Node<K, V>,
6666
}
6767

6868
impl<K: Hash> Hash for KeyRef<K> {
@@ -95,9 +95,9 @@ impl<K, Q: ?Sized> Borrow<Qey<Q>> for KeyRef<K> where K: Borrow<Q> {
9595
}
9696
}
9797

98-
impl<K, V> LinkedHashMapEntry<K, V> {
98+
impl<K, V> Node<K, V> {
9999
fn new(k: K, v: V) -> Self {
100-
LinkedHashMapEntry {
100+
Node {
101101
key: k,
102102
value: v,
103103
next: ptr::null_mut(),
@@ -106,9 +106,9 @@ impl<K, V> LinkedHashMapEntry<K, V> {
106106
}
107107
}
108108

109-
unsafe fn drop_empty_entry_box<K, V>(the_box: *mut LinkedHashMapEntry<K, V>) {
109+
unsafe fn drop_empty_node<K, V>(the_box: *mut Node<K, V>) {
110110
// Prevent compiler from trying to drop the un-initialized key and values in the node.
111-
let LinkedHashMapEntry { key, value, .. } = *Box::from_raw(the_box);
111+
let Node { key, value, .. } = *Box::from_raw(the_box);
112112
mem::forget(key);
113113
mem::forget(value);
114114
}
@@ -139,7 +139,7 @@ impl<K, V, S> LinkedHashMap<K, V, S> {
139139
let mut free = self.free;
140140
while ! free.is_null() {
141141
let next_free = (*free).next;
142-
drop_empty_entry_box(free);
142+
drop_empty_node(free);
143143
free = next_free;
144144
}
145145
self.free = ptr::null_mut();
@@ -148,7 +148,7 @@ impl<K, V, S> LinkedHashMap<K, V, S> {
148148
}
149149

150150
impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
151-
fn with_map(map: HashMap<KeyRef<K>, *mut LinkedHashMapEntry<K, V>, S>) -> Self {
151+
fn with_map(map: HashMap<KeyRef<K>, *mut Node<K, V>, S>) -> Self {
152152
LinkedHashMap {
153153
map: map,
154154
head: ptr::null_mut(),
@@ -212,13 +212,13 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
212212
}
213213
None => {
214214
let node = if self.free.is_null() {
215-
Box::into_raw(Box::new(LinkedHashMapEntry::new(k, v)))
215+
Box::into_raw(Box::new(Node::new(k, v)))
216216
} else {
217217
// use a recycled box
218218
unsafe {
219219
let free = self.free;
220220
self.free = (*free).next;
221-
ptr::write(free, LinkedHashMapEntry::new(k, v));
221+
ptr::write(free, Node::new(k, v));
222222
free
223223
}
224224
};
@@ -617,15 +617,15 @@ impl<'a, K, V, S, Q: ?Sized> IndexMut<&'a Q> for LinkedHashMap<K, V, S>
617617

618618
impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
619619
#[inline]
620-
fn detach(&mut self, node: *mut LinkedHashMapEntry<K, V>) {
620+
fn detach(&mut self, node: *mut Node<K, V>) {
621621
unsafe {
622622
(*(*node).prev).next = (*node).next;
623623
(*(*node).next).prev = (*node).prev;
624624
}
625625
}
626626

627627
#[inline]
628-
fn attach(&mut self, node: *mut LinkedHashMapEntry<K, V>) {
628+
fn attach(&mut self, node: *mut Node<K, V>) {
629629
unsafe {
630630
(*node).next = (*self.head).next;
631631
(*node).prev = self.head;
@@ -742,7 +742,7 @@ impl<K, V, S> Drop for LinkedHashMap<K, V, S> {
742742
if !self.head.is_null() {
743743
unsafe {
744744
self.drop_entries();
745-
drop_empty_entry_box(self.head);
745+
drop_empty_node(self.head);
746746
}
747747
}
748748
self.clear_free_list();
@@ -752,25 +752,25 @@ impl<K, V, S> Drop for LinkedHashMap<K, V, S> {
752752
/// An insertion-order iterator over a `LinkedHashMap`'s entries, with immutable references to the
753753
/// values.
754754
pub struct Iter<'a, K: 'a, V: 'a> {
755-
head: *const LinkedHashMapEntry<K, V>,
756-
tail: *const LinkedHashMapEntry<K, V>,
755+
head: *const Node<K, V>,
756+
tail: *const Node<K, V>,
757757
remaining: usize,
758758
marker: marker::PhantomData<(&'a K, &'a V)>,
759759
}
760760

761761
/// An insertion-order iterator over a `LinkedHashMap`'s entries, with mutable references to the
762762
/// values.
763763
pub struct IterMut<'a, K: 'a, V: 'a> {
764-
head: *mut LinkedHashMapEntry<K, V>,
765-
tail: *mut LinkedHashMapEntry<K, V>,
764+
head: *mut Node<K, V>,
765+
tail: *mut Node<K, V>,
766766
remaining: usize,
767767
marker: marker::PhantomData<(&'a K, &'a mut V)>,
768768
}
769769

770770
/// A consuming insertion-order iterator over a `LinkedHashMap`'s entries.
771771
pub struct IntoIter<K, V> {
772-
head: *mut LinkedHashMapEntry<K, V>,
773-
tail: *mut LinkedHashMapEntry<K, V>,
772+
head: *mut Node<K, V>,
773+
tail: *mut Node<K, V>,
774774
remaining: usize,
775775
marker: marker::PhantomData<(K, V)>,
776776
}
@@ -797,20 +797,20 @@ impl<K, V> Clone for IntoIter<K, V> where K: Clone, V: Clone {
797797
return IntoIter { ..*self }
798798
}
799799

800-
fn clone_entry<K, V>(e: *mut LinkedHashMapEntry<K, V>) -> *mut LinkedHashMapEntry<K, V>
800+
fn clone_node<K, V>(e: *mut Node<K, V>) -> *mut Node<K, V>
801801
where K: Clone, V: Clone,
802802
{
803-
Box::into_raw(Box::new(LinkedHashMapEntry::new(
803+
Box::into_raw(Box::new(Node::new(
804804
unsafe { (*e).key.clone() }, unsafe { (*e).value.clone() }
805805
)))
806806
}
807807

808808
let mut cur = self.head;
809-
let head = clone_entry(cur);
809+
let head = clone_node(cur);
810810
let mut tail = head;
811811
for _ in 1..self.remaining {
812812
unsafe {
813-
(*tail).prev = clone_entry((*cur).prev);
813+
(*tail).prev = clone_node((*cur).prev);
814814
(*(*tail).prev).next = tail;
815815
tail = (*tail).prev;
816816
cur = (*cur).prev;
@@ -1030,7 +1030,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> IntoIterator for LinkedHashMap<K, V, S> {
10301030
let len = self.len();
10311031

10321032
if !self.head.is_null() {
1033-
unsafe { drop_empty_entry_box(self.head) }
1033+
unsafe { drop_empty_node(self.head) }
10341034
}
10351035
self.clear_free_list();
10361036
// drop the HashMap but not the LinkedHashMap

0 commit comments

Comments
 (0)