Download loda-rust/rust_project/loda-rust-cli/src/mine/prevent_flooding.rs from Alignment-Lab-AI/ARC-stuff: direct link, hf CLI and curl.
- Browser
- Download file 2.74 kB
-
https://huggingface.co/datasets/Alignment-Lab-AI/ARC-stuff/resolve/main/loda-rust/rust_project/loda-rust-cli/src/mine/prevent_flooding.rs
- Command line
-
hf download hf://datasets/Alignment-Lab-AI/ARC-stuff/loda-rust/rust_project/loda-rust-cli/src/mine/prevent_flooding.rs
-
curl -L -o prevent_flooding.rs https://huggingface.co/datasets/Alignment-Lab-AI/ARC-stuff/resolve/main/loda-rust/rust_project/loda-rust-cli/src/mine/prevent_flooding.rs
2.74 kB
| use std::collections::HashSet; | |
| use loda_rust_core::util::{BigIntVec, BigIntVecToString}; | |
| /// The `mine-event` dir can quickly get filled up with candidate programs | |
| /// that output the same terms. I have experienced that 80k files was | |
| /// created in a few seconds with nearly identical programs. | |
| /// | |
| /// This is a mechanism to prevent the `mine-event` dir from getting filled up. | |
| /// The way it works. If there already is a program with the same terms in the `mine-event` dir, | |
| /// then prevent the new program from being written to the dir. | |
| /// | |
| /// On load, the `mine-event` dir is scanned for what programs are there. | |
| pub struct PreventFlooding { | |
| hashset: HashSet<String>, | |
| } | |
| pub enum PreventFloodingError { | |
| AlreadyRegistered | |
| } | |
| impl PreventFlooding { | |
| pub fn new() -> Self { | |
| Self { | |
| hashset: HashSet::<String>::new(), | |
| } | |
| } | |
| pub fn contains(&self, bigintvec: &BigIntVec) -> bool { | |
| let s: String = bigintvec.to_compact_comma_string(); | |
| if self.hashset.contains(&s) { | |
| return true; | |
| } | |
| false | |
| } | |
| pub fn try_register(&mut self, bigintvec: &BigIntVec) -> Result<(), PreventFloodingError> { | |
| let s: String = bigintvec.to_compact_comma_string(); | |
| if self.hashset.contains(&s) { | |
| // The `mine-event` dir already contains a program with these terms. | |
| return Err(PreventFloodingError::AlreadyRegistered); | |
| } | |
| self.hashset.insert(s); | |
| // Successfully registered the new program. | |
| Ok(()) | |
| } | |
| pub fn len(&self) -> usize { | |
| self.hashset.len() | |
| } | |
| } | |
| mod tests { | |
| use super::*; | |
| use loda_rust_core::util::BigIntVecFromI64; | |
| fn test_10000_try_register() { | |
| let mut pf = PreventFlooding::new(); | |
| let bigintvec = BigIntVec::from_i64array(&[1, 2, 3, 4, 5]); | |
| assert_eq!(pf.try_register(&bigintvec).is_ok(), true); | |
| assert_eq!(pf.try_register(&bigintvec).is_ok(), false); | |
| } | |
| fn test_10001_contains() { | |
| let mut pf = PreventFlooding::new(); | |
| assert_eq!(pf.contains(&BigIntVec::from_i64array(&[1, 1, 1, 1, 1])), false); | |
| assert_eq!(pf.contains(&BigIntVec::from_i64array(&[1, 2, 3, 4, 5])), false); | |
| assert_eq!(pf.try_register(&BigIntVec::from_i64array(&[1, 2, 3, 4, 5])).is_ok(), true); | |
| assert_eq!(pf.try_register(&BigIntVec::from_i64array(&[1, 1, 1, 1, 1])).is_ok(), true); | |
| assert_eq!(pf.contains(&BigIntVec::from_i64array(&[1, 1, 1, 1, 1])), true); | |
| assert_eq!(pf.contains(&BigIntVec::from_i64array(&[1, 2, 3, 4, 5])), true); | |
| assert_eq!(pf.contains(&BigIntVec::from_i64array(&[1984, 1984, 1984])), false); | |
| } | |
| } | |