File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 44#![ cfg( feature = "use_alloc" ) ]
55
66use alloc:: collections:: BTreeMap ;
7+ use alloc:: vec:: Vec ;
78#[ cfg( feature = "use_std" ) ]
89use core:: hash:: { BuildHasher , Hash } ;
910#[ cfg( feature = "use_std" ) ]
7475 self . entry ( key) . or_default ( )
7576 }
7677}
78+
79+ impl < K , V > Map for Vec < ( K , V ) >
80+ where
81+ K : PartialEq ,
82+ {
83+ type Key = K ;
84+ type Value = V ;
85+ fn is_empty ( & self ) -> bool {
86+ self . is_empty ( )
87+ }
88+ fn clear ( & mut self ) {
89+ self . clear ( )
90+ }
91+ fn insert ( & mut self , key : K , value : V ) -> Option < V > {
92+ match self . iter_mut ( ) . find ( |( k, _) | k == & key) {
93+ Some ( ( _, v) ) => Some ( core:: mem:: replace ( v, value) ) ,
94+ None => {
95+ self . push ( ( key, value) ) ;
96+ None
97+ }
98+ }
99+ }
100+ fn remove ( & mut self , key : & K ) -> Option < V > {
101+ let index = self . iter ( ) . position ( |( k, _) | k == key) ?;
102+ Some ( self . swap_remove ( index) . 1 )
103+ }
104+ fn entry_or_default ( & mut self , key : K ) -> & mut V
105+ where
106+ V : Default ,
107+ {
108+ let index = self . iter ( ) . position ( |( k, _) | k == & key) . unwrap_or_else ( || {
109+ self . push ( ( key, V :: default ( ) ) ) ;
110+ self . len ( ) - 1
111+ } ) ;
112+ & mut self [ index] . 1
113+ }
114+ }
You can’t perform that action at this time.
0 commit comments