33
44use std:: cell:: RefCell ;
55use std:: collections:: BTreeMap ;
6- use std:: ops:: Bound ;
76use std:: rc:: { Rc , Weak } ;
87
98use crate :: {
@@ -87,11 +86,24 @@ impl<K, T> Default for Map<K, T> {
8786
8887impl < K : ' static , T : ' static > ByteRepr for Map < K , T > { }
8988
90- impl < K : Ord + Clone , T > Clone for Map < K , T > {
89+ impl < K : Ord + Clone , V : Clone > Clone for Map < K , ( Value < K > , Value < V > ) > {
9190 fn clone ( & self ) -> Self {
9291 let mut map = Self :: default ( ) ;
93- for ( key, value) in self . entries ( ) {
94- map. link ( key. clone ( ) , value) ;
92+ let mut prev = map. header . clone ( ) ;
93+ for ( key, node) in & self . nodes {
94+ let value = {
95+ let node = node. borrow ( ) ;
96+ let entry = node
97+ . value
98+ . as_ref ( )
99+ . expect ( "map node without a value" )
100+ . borrow ( ) ;
101+ (
102+ Rc :: new ( RefCell :: new ( entry. 0 . borrow ( ) . clone ( ) ) ) ,
103+ Rc :: new ( RefCell :: new ( entry. 1 . borrow ( ) . clone ( ) ) ) ,
104+ )
105+ } ;
106+ prev = map. link_after ( prev, key. clone ( ) , Rc :: new ( RefCell :: new ( value) ) ) ;
95107 }
96108 map
97109 }
@@ -117,23 +129,15 @@ impl<K, T> Map<K, T> {
117129 pub fn end ( & self ) -> BidirectionalIterator < T > {
118130 BidirectionalIterator :: Map ( self . header . clone ( ) )
119131 }
120-
121- pub fn entries ( & self ) -> impl Iterator < Item = ( & K , Value < T > ) > + ' _ {
122- self . nodes . iter ( ) . map ( |( key, node) | {
123- let value = node. borrow ( ) . value . clone ( ) ;
124- ( key, value. expect ( "map node without a value" ) )
125- } )
126- }
127132}
128133
129134impl < K : Ord , T > Map < K , T > {
130- fn link ( & mut self , key : K , value : Value < T > ) -> Value < MapNode < T > > {
131- let prev = self
132- . nodes
133- . range ( ( Bound :: Unbounded , Bound :: Excluded ( & key) ) )
134- . next_back ( )
135- . map ( |( _, node) | node. clone ( ) )
136- . unwrap_or_else ( || self . header . clone ( ) ) ;
135+ fn link_after (
136+ & mut self ,
137+ prev : Value < MapNode < T > > ,
138+ key : K ,
139+ value : Value < T > ,
140+ ) -> Value < MapNode < T > > {
137141 let next = MapNode :: next ( & prev) ;
138142 let node = Rc :: new ( RefCell :: new ( MapNode {
139143 value : Some ( value) ,
@@ -156,20 +160,22 @@ impl<K: Ord, T> Map<K, T> {
156160 }
157161
158162 pub fn get_or_insert_with ( & mut self , key : K , value : impl FnOnce ( ) -> T ) -> Value < T > {
159- if let Some ( existing) = self . get ( & key) {
160- return existing;
161- }
163+ let prev = match self . nodes . range ( ..=& key) . next_back ( ) {
164+ Some ( ( k, node) ) if * k == key => {
165+ return node
166+ . borrow ( )
167+ . value
168+ . clone ( )
169+ . expect ( "map node without a value" ) ;
170+ }
171+ Some ( ( _, node) ) => node. clone ( ) ,
172+ None => self . header . clone ( ) ,
173+ } ;
162174 let value = Rc :: new ( RefCell :: new ( value ( ) ) ) ;
163- self . link ( key, value. clone ( ) ) ;
175+ self . link_after ( prev , key, value. clone ( ) ) ;
164176 value
165177 }
166178
167- pub fn insert ( & mut self , key : K , value : Value < T > ) {
168- if !self . nodes . contains_key ( & key) {
169- self . link ( key, value) ;
170- }
171- }
172-
173179 pub fn find ( & self , key : & K ) -> BidirectionalIterator < T > {
174180 match self . nodes . get ( key) {
175181 Some ( node) => BidirectionalIterator :: Map ( node. clone ( ) ) ,
@@ -292,11 +298,10 @@ impl<K, T> Default for UnsafeMap<K, T> {
292298impl < K : Ord + Clone , T : Clone > Clone for UnsafeMap < K , T > {
293299 fn clone ( & self ) -> Self {
294300 let mut map = Self :: default ( ) ;
301+ let mut prev = map. header_ptr ( ) ;
295302 for ( key, node) in & self . nodes {
296- map. link (
297- key. clone ( ) ,
298- node. value . clone ( ) . expect ( "map node without a value" ) ,
299- ) ;
303+ let value = node. value . clone ( ) . expect ( "map node without a value" ) ;
304+ prev = map. link_after ( prev, key. clone ( ) , value) ;
300305 }
301306 map
302307 }
@@ -340,13 +345,12 @@ impl<K, T> UnsafeMap<K, T> {
340345}
341346
342347impl < K : Ord , T > UnsafeMap < K , T > {
343- fn link ( & mut self , key : K , value : T ) -> * mut UnsafeMapNode < T > {
344- let prev = self
345- . nodes
346- . range ( ( Bound :: Unbounded , Bound :: Excluded ( & key) ) )
347- . next_back ( )
348- . map ( |( _, node) | & * * node as * const UnsafeMapNode < T > as * mut UnsafeMapNode < T > )
349- . unwrap_or_else ( || self . header_ptr ( ) ) ;
348+ fn link_after (
349+ & mut self ,
350+ prev : * mut UnsafeMapNode < T > ,
351+ key : K ,
352+ value : T ,
353+ ) -> * mut UnsafeMapNode < T > {
350354 let next = UnsafeMapNode :: next ( prev) ;
351355 let mut node = Box :: new ( UnsafeMapNode {
352356 value : Some ( value) ,
@@ -369,10 +373,17 @@ impl<K: Ord, T> UnsafeMap<K, T> {
369373 }
370374
371375 pub fn get_or_insert_with ( & mut self , key : K , value : impl FnOnce ( ) -> T ) -> * mut T {
372- if let Some ( existing) = self . get ( & key) {
373- return existing;
374- }
375- UnsafeMapNode :: value ( self . link ( key, value ( ) ) )
376+ let prev = match self . nodes . range ( ..=& key) . next_back ( ) {
377+ Some ( ( k, node) ) => {
378+ let node = & * * node as * const UnsafeMapNode < T > as * mut UnsafeMapNode < T > ;
379+ if * k == key {
380+ return UnsafeMapNode :: value ( node) ;
381+ }
382+ node
383+ }
384+ None => self . header_ptr ( ) ,
385+ } ;
386+ UnsafeMapNode :: value ( self . link_after ( prev, key, value ( ) ) )
376387 }
377388
378389 pub fn find ( & self , key : & K ) -> UnsafeBidirectionalIterator < T > {
0 commit comments