pub fn insert(&mut self, k: K, v: V) -> Option<V> {
        if self.head.is_null() {
            // allocate the guard node if not present
            unsafe {
                self.head = Box::into_raw(Box::new(mem::uninitialized()));
                (*self.head).next = self.head;
                (*self.head).prev = self.head;
            }
        }
        let (node, old_val) = match self.map.get(&KeyRef { k: &k }) {
            Some(node) => {
                let old_val = unsafe { ptr::replace(&mut (**node).value, v) };
                (*node, Some(old_val))
            }
            None => {
                let node = if self.free.is_null() {
                    Box::into_raw(Box::new(Node::new(k, v)))
                } else {
                    // use a recycled box
                    unsafe {
                        let free = self.free;
                        self.free = (*free).next;
                        ptr::write(free, Node::new(k, v));
                        free
                    }
                };
                (node, None)
            }
        };
        match old_val {
            Some(_) => {
                // Existing node, just update LRU position
                self.detach(node);
                self.attach(node);
            }
            None => {
                let keyref = unsafe { &(*node).key };
                self.map.insert(KeyRef { k: keyref }, node);
                self.attach(node);
            }
        }
        old_val
    }
/// Inserts a key-value pair into the map. If the key already existed, the old value is
/// returned.
///
/// # Examples
///
/// ```
/// use mu_utils::LinkedHashMap;
/// let mut map = LinkedHashMap::new();
///
/// map.insert(1, "a");
/// map.insert(2, "b");
/// assert_eq!(map[&1], "a");
/// assert_eq!(map[&2], "b");
/// ```
Inserts a key-value pair into the map. If the key already existed, the old value is
returned.
Inserts a key - value pair into the map . If the key already existed the old value is returned .
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
        if self.head.is_null() {
            // allocate the guard node if not present
            unsafe {
                self.head = Box::into_raw(Box::new(mem::uninitialized()));
                (*self.head).next = self.head;
                (*self.head).prev = self.head;
            }
        }
        let (node, old_val) = match self.map.get(&KeyRef { k: &k }) {
            Some(node) => {
                let old_val = unsafe { ptr::replace(&mut (**node).value, v) };
                (*node, Some(old_val))
            }
            None => {
                let node = if self.free.is_null() {
                    Box::into_raw(Box::new(Node::new(k, v)))
                } else {
                    // use a recycled box
                    unsafe {
                        let free = self.free;
                        self.free = (*free).next;
                        ptr::write(free, Node::new(k, v));
                        free
                    }
                };
                (node, None)
            }
        };
        match old_val {
            Some(_) => {
                // Existing node, just update LRU position
                self.detach(node);
                self.attach(node);
            }
            None => {
                let keyref = unsafe { &(*node).key };
                self.map.insert(KeyRef { k: keyref }, node);
                self.attach(node);
            }
        }
        old_val
    }
pub fn insert ( & mut self , k : K , v : V ) -> Option < V > { if self . head . is_null ( ) { // allocate the guard node if not present unsafe { self . head = Box :: into_raw ( Box :: new ( mem :: uninitialized ( ) ) ) ; ( * self . head ) . next = self . head ; ( * self . head ) . prev = self . head ; } } let ( node , old_val ) = match self . map . get ( & KeyRef { k : & k } ) { Some ( node ) => { let old_val = unsafe { ptr :: replace ( & mut ( * * node ) . value , v ) } ; ( * node , Some ( old_val ) ) } None => { let node = if self . free . is_null ( ) { Box :: into_raw ( Box :: new ( Node :: new ( k , v ) ) ) } else { // use a recycled box unsafe { let free = self . free ; self . free = ( * free ) . next ; ptr :: write ( free , Node :: new ( k , v ) ) ; free } } ; ( node , None ) } } ; match old_val { Some ( _ ) => { // Existing node, just update LRU position self . detach ( node ) ; self . attach ( node ) ; } None => { let keyref = unsafe { & ( * node ) . key } ; self . map . insert ( KeyRef { k : keyref } , node ) ; self . attach ( node ) ; } } old_val }
Inserts a key-value pair into the map.
Inserts a key - value pair into the map .
&mut self
k
K
v
V
&mut self
k
K
v
V
examples
```
use mu_utils::LinkedHashMap;
let mut map = LinkedHashMap::new().


` ` ` use mu_utils :: LinkedHashMap ; let mut map = LinkedHashMap :: new () .