<commit_before>use std::alloc::{alloc, dealloc, Layout};
use std::collections::HashMap;

#[derive(Debug)]
struct Node {
    key: i32,
    val: i32,
    prev: Option<*mut Node>,
    next: Option<*mut Node>,
}

#[derive(Debug)]
struct LRUCache {
    cap: i32,
    count: i32,
    hm: HashMap<i32, *mut Node>,
    entry: Option<*mut Node>,
    exit: Option<*mut Node>,
}

impl LRUCache {
    fn new(capacity: i32) -> Self {
        LRUCache {
            cap: capacity,
            count: 0,
            hm: HashMap::with_capacity(capacity as usize),
            entry: None,
            exit: None,
        }
    }
    fn get(&mut self, key: i32) -> i32 {
        unsafe {
            match self.hm.get(&key) {
                Some(vp) => {
                    let node = &mut **vp;
                    if let Some(prevp) = node.prev {
                        let prev = &mut *prevp;
                        match node.next {
                            Some(nextp) => {
                                let next = &mut *nextp;
                                prev.next = Some(nextp);
                                next.prev = Some(prevp);
                            }
                            None => {
                                prev.next = None;
                                self.exit = Some(prevp);
                            }
                        }
                        let entryp = self.entry.unwrap();
                        let entry = &mut *entryp;
                        node.next = Some(entryp);
                        node.prev = None;
                        entry.prev = Some(node);
                        self.entry = Some(*vp);
                    }
                    node.val
                }
                None => -1,
            }
        }
    }

    fn put(&mut self, key: i32, value: i32) {
        unsafe {
            match self.hm.get(&key) {
                Some(vp) => {
                    let node = &mut **vp;
                    if let Some(prevp) = node.prev {
                        let prev = &mut *prevp;
                        match node.next {
                            Some(nextp) => {
                                let next = &mut *nextp;
                                prev.next = Some(nextp);
                                next.prev = Some(prevp);
                            }
                            None => {
                                prev.next = None;
                                self.exit = Some(prevp);
                            }
                        }
                        let entryp = self.entry.unwrap();
                        let entry = &mut *entryp;
                        node.next = Some(entryp);
                        node.prev = None;
                        entry.prev = Some(node);
                        self.entry = Some(*vp);
                    }
                    node.val = value;
                }
                None => {
                    let nodep = alloc(Layout::new::<Node>()) as *mut Node;
                    let node = &mut *nodep;
                    node.key = key;
                    node.val = value;
                    if self.count > 0 {
                        let entryp = self.entry.unwrap();
                        let entry = &mut *entryp;
                        node.next = Some(entry);
                        entry.prev = Some(nodep);
                        self.entry = Some(nodep);
                    } else {
                        self.entry = Some(nodep);
                        self.exit = Some(nodep);
                    }
                    self.hm.insert(key, nodep);
                    self.count += 1;
                    self.clean()
                }
            }
        }
    }

    fn clean(&mut self) {
        unsafe {
            while self.count > self.cap {
                if let Some(exitp) = self.exit {
                    let exit_node = &mut *exitp;
                    if let Some(prevp) = exit_node.prev {
                        let mut prev = &mut *prevp;
                        prev.next = None;
                        self.exit = exit_node.prev;
                    } else {
                        self.exit = None;
                    }
                    self.hm.remove(&exit_node.key);
                    // dealloc(exitp as *mut u8, Layout::new::<Node>());<commit_msg>LRU cache<commit_after>use std::alloc::{alloc_zeroed, dealloc, Layout};
use std::collections::HashMap;

#[derive(Debug)]
struct Node {
    key: i32,
    val: i32,
    prev: Option<*mut Node>,
    next: Option<*mut Node>,
}

#[derive(Debug)]
struct LRUCache {
    cap: i32,
    count: i32,
    hm: HashMap<i32, *mut Node>,
    entry: Option<*mut Node>,
    exit: Option<*mut Node>,
}

impl LRUCache {
    fn new(capacity: i32) -> Self {
        LRUCache {
            cap: capacity,
            count: 0,
            hm: HashMap::with_capacity(capacity as usize),
            entry: None,
            exit: None,
        }
    }
    fn get(&mut self, key: i32) -> i32 {
        unsafe {
            match self.hm.get(&key) {
                Some(vp) => {
                    let node = &mut **vp;
                    if let Some(prevp) = node.prev {
                        let prev = &mut *prevp;
                        match node.next {
                            Some(nextp) => {
                                let next = &mut *nextp;
                                prev.next = Some(nextp);
                                next.prev = Some(prevp);
                            }
                            None => {
                                prev.next = None;
                                self.exit = Some(prevp);
                            }
                        }
                        let entryp = self.entry.unwrap();
                        let entry = &mut *entryp;
                        node.next = Some(entryp);
                        node.prev = None;
                        entry.prev = Some(node);
                        self.entry = Some(*vp);
                    }
                    node.val
                }
                None => -1,
            }
        }
    }

    fn put(&mut self, key: i32, value: i32) {
        unsafe {
            match self.hm.get(&key) {
                Some(vp) => {
                    let node = &mut **vp;
                    if let Some(prevp) = node.prev {
                        let prev = &mut *prevp;
                        match node.next {
                            Some(nextp) => {
                                let next = &mut *nextp;
                                prev.next = Some(nextp);
                                next.prev = Some(prevp);
                            }
                            None => {
                                prev.next = None;
                                self.exit = Some(prevp);
                            }
                        }
                        let entryp = self.entry.unwrap();
                        let entry = &mut *entryp;
                        node.next = Some(entryp);
                        node.prev = None;
                        entry.prev = Some(node);
                        self.entry = Some(*vp);
                    }
                    node.val = value;
                }
                None => {
                    let nodep = alloc_zeroed(Layout::new::<Node>()) as *mut Node;
                    let node = &mut *nodep;
                    node.key = key;
                    node.val = value;
                    if self.count > 0 {
                        let entryp = self.entry.unwrap();
                        let entry = &mut *entryp;
                        node.next = Some(entry);
                        entry.prev = Some(nodep);
                        self.entry = Some(nodep);
                    } else {
                        self.entry = Some(nodep);
                        self.exit = Some(nodep);
                    }
                    self.hm.insert(key, nodep);
                    self.count += 1;
                    self.clean()
                }
            }
        }
    }

    fn clean(&mut self) {
        unsafe {
            while self.count > self.cap {
                if let Some(exitp) = self.exit {
                    let exit_node = &mut *exitp;
                    if let Some(prevp) = exit_node.prev {
                        let mut prev = &mut *prevp;
                        prev.next = None;
                        self.exit = exit_node.prev;
                    } else {
                        self.exit = None;
                    }
                    self.hm.remove(&exit_node.key);
                    dealloc(exitp as *mut u8, Layout::new::<Node>());
{'fraction_non_alphanumeric': 0.10149389896225339, 'fraction_numerical': 0.0057019044360816515, 'mean_word_length': 0.745273631840796, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2971283', 'n_tokens_mistral': 2279, 'n_tokens_neox': 2119, 'n_words': 490}