### Work with Different Buffer Types using SplinterRef (Rust) Source: https://context7.com/orbitinghail/splinter-rs/llms.txt Demonstrates the flexibility of `SplinterRef` in working with various backing storage types, including `Vec`, `Arc<[u8]>` (for shared ownership), and `&[u8]` (for borrowed data). The examples show how to create `SplinterRef` instances from these different buffer types and perform basic checks like `contains` and `cardinality`. ```rust use splinter_rs::{Splinter, SplinterRef, PartitionWrite, PartitionRead, Encodable}; use std::sync::Arc; let mut splinter = Splinter::EMPTY; splinter.insert(42); splinter.insert(100); // With Vec let bytes_vec = splinter.encode_to_bytes().to_vec(); let ref_vec = SplinterRef::from_bytes(bytes_vec).unwrap(); assert!(ref_vec.contains(42)); // With Arc<[u8]> for shared ownership let bytes_arc: Arc<[u8]> = splinter.encode_to_bytes().to_vec().into(); let ref_arc = SplinterRef::from_bytes(bytes_arc).unwrap(); assert!(ref_arc.contains(100)); // With &[u8] for borrowed data let bytes = splinter.encode_to_bytes(); let ref_slice = SplinterRef::from_bytes(&bytes[..]).unwrap(); assert_eq!(ref_slice.cardinality(), 2); ``` -------------------------------- ### Copy-on-Write (CowSplinter) Usage in Rust Source: https://context7.com/orbitinghail/splinter-rs/llms.txt Shows how to use `CowSplinter` for efficient handling of potentially mutable data. It starts by referencing existing data without copying and only performs a clone when a mutation (like insertion) occurs. This requires the `splinter_rs` crate and relevant traits like `PartitionWrite`, `PartitionRead`, and `Encodable`. ```rust use splinter_rs::{Splinter, CowSplinter, PartitionWrite, PartitionRead, Encodable}; use bytes::Bytes; // Start with serialized data let original = Splinter::from_iter([100, 200, 300]); let splinter_ref = original.encode_to_splinter_ref(); let mut cow = CowSplinter::from_ref(splinter_ref); // Read operations work without deserialization assert_eq!(cow.cardinality(), 3); assert!(cow.contains(200)); // First write triggers conversion to owned cow.insert(400); // Clone-on-write happens here assert_eq!(cow.cardinality(), 4); // Subsequent writes use the owned copy cow.insert(500); assert_eq!(cow.cardinality(), 5); // Convert to owned splinter explicitly let owned: Splinter = cow.into_owned(); assert_eq!(owned.cardinality(), 5); ``` -------------------------------- ### Handle Deserialization Errors with SplinterRef (Rust) Source: https://context7.com/orbitinghail/splinter-rs/llms.txt Shows how to handle potential errors when deserializing byte slices into `SplinterRef` objects. This includes checking for insufficient data length and invalid magic bytes. The example demonstrates asserting against specific `DecodeErr` variants and also shows successful deserialization of valid Splinter data. ```rust use splinter_rs::{SplinterRef, codec::DecodeErr}; // Invalid bytes (too short) let invalid_bytes = vec![0u8; 5]; let result = SplinterRef::from_bytes(invalid_bytes); assert!(matches!(result.unwrap_err(), DecodeErr::Length)); // Invalid magic bytes let invalid_bytes = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; let result = SplinterRef::from_bytes(invalid_bytes); assert!(result.is_err()); // Valid splinter use splinter_rs::{Splinter, PartitionWrite, Encodable}; let mut splinter = Splinter::EMPTY; splinter.insert(42); let bytes = splinter.encode_to_bytes(); let valid_ref = SplinterRef::from_bytes(bytes); assert!(valid_ref.is_ok()); ``` -------------------------------- ### Create and Populate Splinter Bitmap Source: https://context7.com/orbitinghail/splinter-rs/llms.txt Demonstrates how to create an empty Splinter bitmap, insert values, check for membership, retrieve cardinality, optimize for compression, and iterate over the values in order. Requires the `Splinter`, `PartitionWrite`, `PartitionRead`, and `Optimizable` traits. ```rust use splinter_rs::{ Splinter, PartitionWrite, PartitionRead, Optimizable }; // Create empty splinter let mut splinter = Splinter::EMPTY; // Insert values splinter.insert(1024); splinter.insert(2048); splinter.insert(123); // Check membership assert!(splinter.contains(1024)); assert!(!splinter.contains(999)); // Get cardinality assert_eq!(splinter.cardinality(), 3); // Optimize for better compression (recommended before serialization) splinter.optimize(); // Iterate over all values in order let values: Vec = splinter.iter().collect(); assert_eq!(values, vec![123, 1024, 2048]); ``` -------------------------------- ### Creating CowSplinter from Different Sources in Rust Source: https://context7.com/orbitinghail/splinter-rs/llms.txt Illustrates the flexible initialization options for `CowSplinter`, including creating it from an owned `Splinter`, an iterator, raw bytes, or a `SplinterRef`. This allows for seamless integration with various data sources. It requires the `splinter_rs` crate and traits like `PartitionRead` and `Encodable`. ```rust use splinter_rs::{Splinter, CowSplinter, PartitionRead, Encodable}; use bytes::Bytes; // From owned splinter let owned = Splinter::from_iter([1, 2, 3]); let cow1: CowSplinter = CowSplinter::from_owned(owned); assert_eq!(cow1.cardinality(), 3); // From iterator let cow2 = CowSplinter::::from_iter([10, 20, 30]); assert_eq!(cow2.cardinality(), 3); // From bytes let bytes = cow2.encode_to_bytes(); let cow3 = CowSplinter::from_bytes(bytes).unwrap(); assert_eq!(cow3.cardinality(), 3); // From reference let splinter_ref = Splinter::from_iter([100, 200]).encode_to_splinter_ref(); let cow4 = CowSplinter::from_ref(splinter_ref); assert_eq!(cow4.cardinality(), 2); ``` -------------------------------- ### Optimize Compression Ratio Before Serialization (Rust) Source: https://context7.com/orbitinghail/splinter-rs/llms.txt Demonstrates how to optimize the compression ratio of a Splinter set before encoding it to bytes. This typically reduces the size of the serialized data. It involves inserting elements, measuring the size, calling the optimize() method, and then measuring the size again. The optimized bytes are then encoded. ```rust use splinter_rs::{Splinter, Optimizable, Encodable, PartitionWrite}; let mut splinter = Splinter::EMPTY; // Insert many values for i in (0..10000).step_by(100) { splinter.insert(i); } // Size before optimization let size_before = splinter.encoded_size(); // Optimize to improve compression splinter.optimize(); // Size after optimization (typically smaller) let size_after = splinter.encoded_size(); println!("Before: {} bytes, After: {} bytes", size_before, size_after); // Always optimize before encoding for best compression let bytes = splinter.encode_to_bytes(); ``` -------------------------------- ### Perform Query Operations on Splinter Source: https://context7.com/orbitinghail/splinter-rs/llms.txt Illustrates advanced querying capabilities of Splinter bitmaps, including finding the position of a value, ranking (counting elements less than or equal to a value), selecting an element by its index, and retrieving the largest element. Requires `Splinter` and `PartitionRead`. ```rust use splinter_rs::{ Splinter, PartitionRead }; let splinter = Splinter::from_iter([10, 20, 30, 100, 200]); // Position: find the 0-based index of a value assert_eq!(splinter.position(20), Some(1)); assert_eq!(splinter.position(25), None); // Rank: count elements <= value assert_eq!(splinter.rank(5), 0); // No elements <= 5 assert_eq!(splinter.rank(20), 2); // Two elements <= 20 assert_eq!(splinter.rank(150), 4); // Four elements <= 150 // Select: get element at 0-based index assert_eq!(splinter.select(0), Some(10)); // Smallest element assert_eq!(splinter.select(2), Some(30)); // Third element assert_eq!(splinter.select(10), None); // Out of bounds // Last: get largest element assert_eq!(splinter.last(), Some(200)); ``` -------------------------------- ### Direct Encoding to SplinterRef in Rust Source: https://context7.com/orbitinghail/splinter-rs/llms.txt Demonstrates how to encode a Splinter data structure directly into a zero-copy SplinterRef. This allows for efficient, in-place querying and iteration without intermediate byte allocations. It requires the `splinter_rs` crate. ```rust use splinter_rs::{Splinter, PartitionWrite, PartitionRead}; let mut splinter = Splinter::from_iter([42, 1337, 9000]); // Encode directly to SplinterRef without intermediate bytes let splinter_ref = splinter.encode_to_splinter_ref(); // Zero-copy queries assert_eq!(splinter_ref.cardinality(), 3); assert!(splinter_ref.contains(1337)); // Iterate over values let values: Vec = splinter_ref.iter().collect(); assert_eq!(values, vec![42, 1337, 9000]); ``` -------------------------------- ### Perform Set Operations Between Owned and Zero-Copy Types (Rust) Source: https://context7.com/orbitinghail/splinter-rs/llms.txt Illustrates how to perform standard set operations (union, intersection) between an owned Splinter set (`Splinter`) and a zero-copy `SplinterRef`. These operations work seamlessly, allowing for efficient manipulation of data without unnecessary copying. Both | and & operators for union and intersection, as well as |= for in-place union, are demonstrated. ```rust use splinter_rs::{Splinter, PartitionRead, Encodable}; let a = Splinter::from_iter([1, 2, 3, 4, 5]); let b = Splinter::from_iter([3, 4, 5, 6, 7]).encode_to_splinter_ref(); // Operations work seamlessly between types let union = &a | &b; assert_eq!(union.cardinality(), 7); let intersection = &a & &b; assert_eq!(intersection.cardinality(), 3); // In-place operations also work let mut result = a.clone(); result |= &b; assert_eq!(result.cardinality(), 7); ``` -------------------------------- ### Serialize and Access Splinter with Zero-Copy Source: https://context7.com/orbitinghail/splinter-rs/llms.txt Explains the process of serializing a Splinter bitmap into a byte vector and then creating a zero-copy immutable reference (`SplinterRef`) from these bytes. This allows querying the data without full deserialization. Requires `Splinter`, `SplinterRef`, `PartitionWrite`, `PartitionRead`, and `Encodable`. ```rust use splinter_rs::{ Splinter, SplinterRef, PartitionWrite, PartitionRead, Encodable }; // Create and populate let mut splinter = Splinter::EMPTY; splinter.insert(100); splinter.insert(200); splinter.insert(300); // Serialize to bytes let bytes = splinter.encode_to_bytes(); println!("Encoded size: {} bytes", bytes.len()); // Create zero-copy reference from bytes let splinter_ref = SplinterRef::from_bytes(bytes).unwrap(); // Query without deserializing assert_eq!(splinter_ref.cardinality(), 3); assert!(splinter_ref.contains(200)); assert_eq!(splinter_ref.select(1), Some(200)); // Convert back to owned if mutation needed let decoded = splinter_ref.decode_to_splinter(); assert_eq!(decoded.cardinality(), 3); ``` -------------------------------- ### In-Place Set Operations in Rust Source: https://context7.com/orbitinghail/splinter-rs/llms.txt Shows how to perform set operations directly on a mutable Splinter instance, modifying it in place. This is useful for optimizing memory usage when the original data is no longer needed. It requires the `splinter_rs` crate and the `PartitionWrite` trait. ```rust use splinter_rs::{Splinter, PartitionRead, PartitionWrite}; let mut a = Splinter::from_iter([1, 2, 3, 4]); let b = Splinter::from_iter([3, 4, 5, 6]); // Union in place let mut result = a.clone(); result |= &b; assert_eq!(result.cardinality(), 6); // Intersection in place let mut result = a.clone(); result &= &b; assert_eq!(result.cardinality(), 2); // Difference in place let mut result = a.clone(); result -= &b; assert_eq!(result.cardinality(), 2); assert!(result.contains(1) && result.contains(2)); // Symmetric difference in place let mut result = a.clone(); result ^= &b; assert_eq!(result.cardinality(), 4); ``` -------------------------------- ### Create Splinter from Iterators and Ranges Source: https://context7.com/orbitinghail/splinter-rs/llms.txt Shows how to construct Splinter bitmaps from various data sources, including iterators, standard Rust ranges, and the full range of u32 values. This utilizes the `Splinter` struct and the `PartitionRead` trait. ```rust use splinter_rs::{ Splinter, PartitionRead }; // From iterator let values = vec![100, 200, 300, 400]; let splinter: Splinter = values.into_iter().collect(); assert_eq!(splinter.cardinality(), 4); // From range let splinter = Splinter::from(10..=20); assert_eq!(splinter.cardinality(), 11); assert!(splinter.contains(15)); // Full range (all u32 values) let splinter = Splinter::from(..); assert_eq!(splinter.cardinality(), (u32::MAX as usize) + 1); ``` -------------------------------- ### Set Operations (Union, Intersection, Difference, Symmetric Difference) in Rust Source: https://context7.com/orbitinghail/splinter-rs/llms.txt Illustrates performing standard set operations on Splinter data structures using bitwise operators. These operations return new Splinter instances representing the result. It requires the `splinter_rs` crate and the `PartitionRead` trait. ```rust use splinter_rs::{Splinter, PartitionRead}; let a = Splinter::from_iter([1, 2, 3, 4, 5]); let b = Splinter::from_iter([3, 4, 5, 6, 7]); // Union (OR) let union = &a | &b; assert_eq!(union.cardinality(), 7); assert!(union.contains(1) && union.contains(7)); // Intersection (AND) let intersection = &a & &b; assert_eq!(intersection.cardinality(), 3); assert!(intersection.contains(3) && intersection.contains(4) && intersection.contains(5)); // Difference (SUB) let difference = &a - &b; assert_eq!(difference.cardinality(), 2); assert!(difference.contains(1) && difference.contains(2)); // Symmetric difference (XOR) let sym_diff = &a ^ &b; assert_eq!(sym_diff.cardinality(), 4); assert!(sym_diff.contains(1) && sym_diff.contains(7)); ``` -------------------------------- ### Perform Range Operations on Splinter Source: https://context7.com/orbitinghail/splinter-rs/llms.txt Details how to perform range-based checks and modifications on Splinter bitmaps. This includes verifying if all or any elements within a given range are present and removing a specified range of values. Requires `Splinter`, `PartitionRead`, and `PartitionWrite`. ```rust use splinter_rs::{ Splinter, PartitionRead, PartitionWrite }; let mut splinter = Splinter::from_iter([10, 11, 12, 13, 14, 15, 100]); // Check if all values in range are contained assert!(splinter.contains_all(10..=15)); assert!(splinter.contains_all(11..=14)); assert!(!splinter.contains_all(10..=16)); // 16 is missing // Check if any value in range is contained assert!(splinter.contains_any(10..=15)); // Contains 10-15 assert!(splinter.contains_any(5..=10)); // Contains 10 assert!(!splinter.contains_any(40..=50)); // No overlap // Remove a range splinter.remove_range(3..=7); assert!(!splinter.contains(5)); assert!(splinter.contains(10)); ``` -------------------------------- ### Cut Operation in Rust Source: https://context7.com/orbitinghail/splinter-rs/llms.txt Demonstrates the `cut` operation, which extracts the intersection of two Splinter sets and simultaneously removes those elements from the source set. This is an efficient way to process shared elements and update the source. It requires the `splinter_rs` crate and the `Cut` trait. ```rust use splinter_rs::{Splinter, Cut, PartitionRead}; let mut source = Splinter::from_iter([1, 2, 3, 4, 5]); let other = Splinter::from_iter([3, 4, 5, 6, 7]); // Cut returns intersection and removes it from source let intersection = source.cut(&other); // Intersection contains shared elements assert_eq!(intersection.cardinality(), 3); assert!(intersection.contains(3) && intersection.contains(4) && intersection.contains(5)); // Source now contains only elements not in other assert_eq!(source.cardinality(), 2); assert!(source.contains(1) && source.contains(2)); assert!(!source.contains(3)); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.