pub fn put(&mut self, k: K, v: V) {
        // check if the key is already in the cache
        if let Some(idx) = self.map.get(&KeyRef { k: &k }) {
            let entry = &mut self.entries[*idx];
            entry.val = v;
            return;
        }

        let entry = if self.entries.len() < self.cap {
            // if entries is not full yet, push a new entry onto the end
            self.entries.push(ClockEntry::new(k, v));
            &mut self.entries[self.idx]
        } else {
            // if entries is full, find and use the first entry with its usage bit set to false
            let mut usage_bit = self.bits.get(self.idx).unwrap();

            while usage_bit {
                self.bits.set(self.idx, false);
                self.idx = (self.idx + 1) % self.cap;
                usage_bit = self.bits.get(self.idx).unwrap();
            }

            self.bits.set(self.idx, true);

            let entry = &mut self.entries[self.idx];

            let old_key = KeyRef { k: &entry.key };
            self.map.remove(&old_key);

            entry.key = k;
            entry.val = v;
            entry
        };

        let key = KeyRef { k: &entry.key };
        self.map.insert(key, self.idx);

        self.idx = (self.idx + 1) % self.cap;
    }
/// Put a key-value pair into the cache. If the key already exists update its value.
///
/// # Example
///
/// ```
/// use clock_cache::ClockCache;
/// let mut cache = ClockCache::new(2);
///
/// cache.put(1, "a");
/// cache.put(2, "b");
/// assert_eq!(cache.get(&1), Some(&"a"));
/// assert_eq!(cache.get(&2), Some(&"b"));
/// ```
Put a key-value pair into the cache. If the key already exists update its value.
Put a key - value pair into the cache . If the key already exists update its value .
pub fn put(&mut self, k: K, v: V) {
        // check if the key is already in the cache
        if let Some(idx) = self.map.get(&KeyRef { k: &k }) {
            let entry = &mut self.entries[*idx];
            entry.val = v;
            return;
        }
        let entry = if self.entries.len() < self.cap {
            // if entries is not full yet, push a new entry onto the end
            self.entries.push(ClockEntry::new(k, v));
            &mut self.entries[self.idx]
        } else {
            // if entries is full, find and use the first entry with its usage bit set to false
            let mut usage_bit = self.bits.get(self.idx).unwrap();
            while usage_bit {
                self.bits.set(self.idx, false);
                self.idx = (self.idx + 1) % self.cap;
                usage_bit = self.bits.get(self.idx).unwrap();
            }
            self.bits.set(self.idx, true);
            let entry = &mut self.entries[self.idx];
            let old_key = KeyRef { k: &entry.key };
            self.map.remove(&old_key);
            entry.key = k;
            entry.val = v;
            entry
        };
        let key = KeyRef { k: &entry.key };
        self.map.insert(key, self.idx);
        self.idx = (self.idx + 1) % self.cap;
    }
pub fn put ( & mut self , k : K , v : V ) { // check if the key is already in the cache if let Some ( idx ) = self . map . get ( & KeyRef { k : & k } ) { let entry = & mut self . entries [ * idx ] ; entry . val = v ; return ; } let entry = if self . entries . len ( ) < self . cap { // if entries is not full yet, push a new entry onto the end self . entries . push ( ClockEntry :: new ( k , v ) ) ; & mut self . entries [ self . idx ] } else { // if entries is full, find and use the first entry with its usage bit set to false let mut usage_bit = self . bits . get ( self . idx ) . unwrap ( ) ; while usage_bit { self . bits . set ( self . idx , false ) ; self . idx = ( self . idx + 1 ) % self . cap ; usage_bit = self . bits . get ( self . idx ) . unwrap ( ) ; } self . bits . set ( self . idx , true ) ; let entry = & mut self . entries [ self . idx ] ; let old_key = KeyRef { k : & entry . key } ; self . map . remove ( & old_key ) ; entry . key = k ; entry . val = v ; entry } ; let key = KeyRef { k : & entry . key } ; self . map . insert ( key , self . idx ) ; self . idx = ( self . idx + 1 ) % self . cap ; }
Put a key-value pair into the cache.
Put a key - value pair into the cache .
&mut self
k
K
v
V
example
```
use clock_cache::ClockCache;
let mut cache = ClockCache::new(2).


` ` ` use clock_cache :: ClockCache ; let mut cache = ClockCache :: new ( 2 ) .