btypes/inf_bbool.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
use crate::error::BBoolError;
use anyhow::Result;
use std::marker::PhantomData;
/// Type alias for the infinite-capacity `BetterBool` implementation
pub type BInf = BetterBoolInf;
/// A dynamically-sized boolean collection backed by a vector
///
/// This struct provides storage and operations for boolean values with
/// virtually unlimited capacity, growing as needed.
pub struct BetterBoolInf {
/// The vector storing the boolean bits as bytes
pub(crate) store: Vec<u8>,
/// Current position of the reader head
pub(crate) reader_head_pos: u128,
/// Phantom data for the vector type
pub(crate) _marker: PhantomData<Vec<u8>>,
}
impl Default for BetterBoolInf {
fn default() -> Self {
Self {
store: Vec::new(),
reader_head_pos: 0,
_marker: PhantomData,
}
}
}
impl BetterBoolInf {
/// The limit of the "Infinite" `BetterBool` will unfortunately be finite, due to limitations of the head position without unnecessary complexity.
pub const CAP: u128 = u128::MAX;
}
impl BetterBoolInf {
/// Creates a new empty `BetterBoolInf` instance initialized with an empty vector.
///
/// # Examples
/// ```
/// use btypes::inf_bbool::BInf;
/// let bools = BInf::new();
/// ```
#[must_use] pub fn new() -> Self {
Self::default()
}
/// Creates a new `BetterBoolInf` instance with a specified initial vector of bytes.
///
/// # Arguments
/// * `initial_value` - The initial vector of bytes to store the boolean states
///
/// # Examples
/// ```
/// use btypes::inf_bbool::BInf;
/// let bools = BInf::from_vec(vec![42]);
/// ```
#[must_use] pub const fn from_vec(initial_value: Vec<u8>) -> Self {
Self {
store: initial_value,
reader_head_pos: 0,
_marker: PhantomData,
}
}
/// Returns a Vec of all bools in the container.
///
/// # Examples
/// ```
/// use btypes::inf_bbool::BInf;
/// use anyhow::Result;
/// fn main() -> Result<()> {
/// let mut bools = BInf::from_vec(vec![5]);
/// let all_bools = bools.all()?;
/// Ok(())
/// }
/// ```
///
/// # Errors
/// Returns an error if accessing any position fails
pub fn all(&mut self) -> Result<Vec<bool>> {
let mut out = vec![];
// Multiply by 8 since each byte contains 8 bits
for i in 0..(self.store.len() * 8) {
out.push(self.get_at_pos(i.try_into()?)?);
}
Ok(out)
}
/// Returns a new `BetterBoolInf` that has been sorted.
///
/// # Examples
/// ```
/// use btypes::inf_bbool::BInf;
/// use anyhow::Result;
/// fn main() -> Result<()> {
/// let mut bools = BInf::from_vec(vec![5]);
/// let sorted = bools.sorted()?;
/// Ok(())
/// }
/// ```
///
/// # Errors
/// Returns an error if sorting operation fails
pub fn sorted(&mut self) -> Result<Self> {
let mut bools = self.all()?;
bools.sort_unstable();
let mut sorted = Self::new();
for (i, &value) in bools.iter().enumerate() {
sorted.set_at_pos(i.try_into()?, value)?;
}
Ok(sorted)
}
/// Gets the bool at the current head position.
///
/// # Examples
/// ```
/// use btypes::inf_bbool::BInf;
/// use anyhow::Result;
/// fn main() -> Result<()> {
/// let bools = BInf::from_vec(vec![5]);
/// let value = bools.get()?;
/// Ok(())
/// }
/// ```
///
/// # Errors
/// Returns an error if head position is invalid
pub fn get(&self) -> Result<bool, BBoolError> {
if self.reader_head_pos < Self::CAP {
let byte_index = (self.reader_head_pos / 8) as usize;
let bit_offset = self.reader_head_pos % 8;
if byte_index >= self.store.len() {
return Ok(false); // Return false for unallocated positions
}
let mask = 1u8 << bit_offset;
return Ok((self.store[byte_index] & mask) != 0);
}
Err(BBoolError::InvalidHeadPosInf(self.reader_head_pos))
}
/// Gets the bool at the given position.
///
/// # Arguments
/// * `pos` - The position to read from
///
/// # Examples
/// ```
/// use btypes::inf_bbool::BInf;
/// use anyhow::Result;
/// fn main() -> Result<()> {
/// let bools = BInf::from_vec(vec![5]);
/// let value = bools.get_at_pos(2)?;
/// Ok(())
/// }
/// ```
///
/// # Errors
/// Returns an error if position is invalid
pub fn get_at_pos(&self, pos: u128) -> Result<bool, BBoolError> {
if pos < Self::CAP {
let byte_index = (pos / 8) as usize;
let bit_offset = pos % 8;
if byte_index >= self.store.len() {
return Ok(false); // Return false for unallocated positions
}
let mask = 1u8 << bit_offset;
return Ok((self.store[byte_index] & mask) != 0);
}
Err(BBoolError::InvalidPosInf(pos))
}
/// Gets the bool at the current head position without validity checks.
///
/// # Safety
/// This function performs no bounds checking. The caller must ensure the head position is valid.
#[must_use] pub unsafe fn get_unchecked(&self) -> bool {
let byte_index = (self.reader_head_pos / 8) as usize;
let bit_offset = self.reader_head_pos % 8;
if byte_index >= self.store.len() {
return false;
}
let mask = 1u8 << bit_offset;
(self.store[byte_index] & mask) != 0
}
/// Gets the bool at the given position without validity checks.
///
/// # Arguments
/// * `pos` - The position to read from
///
/// # Safety
/// This function performs no bounds checking. the position is valid.
#[must_use] pub unsafe fn get_unchecked_at_pos(&self, pos: u128) -> bool {
let byte_index = (pos / 8) as usize;
let bit_offset = pos % 8;
if byte_index >= self.store.len() {
return false;
}
let mask = 1u8 << bit_offset;
(self.store[byte_index] & mask) != 0
}
/// Sets the bool at the current head position without validity checks.
///
/// # Arguments
/// * `new` - The boolean value to set
///
/// # Safety
/// This function performs no bounds checking. The caller must ensure the head position is valid.
pub unsafe fn set_unchecked(&mut self, new: bool) {
let byte_index = (self.reader_head_pos / 8) as usize;
let bit_offset = self.reader_head_pos % 8;
while byte_index >= self.store.len() {
self.store.push(0);
}
let mask = 1u8 << bit_offset;
if new {
self.store[byte_index] |= mask;
} else {
self.store[byte_index] &= !mask;
}
}
/// Sets the bool at the given position without validity checks.
///
/// # Arguments
/// * `pos` - The position to set
/// * `new` - The boolean value to set
///
/// # Safety
/// This function performs no bounds checking. The caller must ensure the position is valid.
pub unsafe fn set_unchecked_at_pos(&mut self, pos: u128, new: bool) {
let byte_index = (pos / 8) as usize;
let bit_offset = pos % 8;
while byte_index >= self.store.len() {
self.store.push(0);
}
let mask = 1u8 << bit_offset;
if new {
self.store[byte_index] |= mask;
} else {
self.store[byte_index] &= !mask;
}
}
/// Get an immutable reference to the bools contained in a raw byte vector format.
///
/// # Examples
/// ```
/// use btypes::inf_bbool::BInf;
/// let bools = BInf::from_vec(vec![5]);
/// let raw = bools.get_raw();
/// ```
#[must_use] pub const fn get_raw(&self) -> &Vec<u8> {
&self.store
}
/// Get a mutable reference to the bools contained in a raw byte vector format.
///
/// # Examples
/// ```
/// use btypes::inf_bbool::BInf;
/// let mut bools = BInf::from_vec(vec![5]);
/// let raw_mut = bools.get_raw_mut();
/// ```
pub fn get_raw_mut(&mut self) -> &mut Vec<u8> {
&mut self.store
}
/// Sets the bool at the current head position.
///
/// # Arguments
/// * `new` - The boolean value to set
///
/// # Examples
/// ```
/// use btypes::inf_bbool::BInf;
/// use anyhow::Result;
/// fn main() -> Result<()> {
/// let mut bools = BInf::new();
/// bools.set(true)?;
/// Ok(())
/// }
/// ```
///
/// # Errors
/// Returns an error if head position is invalid
pub fn set(&mut self, new: bool) -> Result<(), BBoolError> {
if self.reader_head_pos < Self::CAP {
let byte_index = (self.reader_head_pos / 8) as usize;
let bit_offset = self.reader_head_pos % 8;
// Extend the vector if necessary
while byte_index >= self.store.len() {
self.store.push(0);
}
let mask = 1u8 << bit_offset;
if new {
self.store[byte_index] |= mask;
} else {
self.store[byte_index] &= !mask;
}
return Ok(());
}
Err(BBoolError::InvalidHeadPosInf(self.reader_head_pos))
}
/// Sets the bool at the given position.
///
/// # Arguments
/// * `pos` - The position to set
/// * `new` - The boolean value to set
///
/// # Examples
/// ```
/// use btypes::inf_bbool::BInf;
/// use anyhow::Result;
/// fn main() -> Result<()> {
/// let mut bools = BInf::new();
/// bools.set_at_pos(2, true)?;
/// Ok(())
/// }
/// ```
///
/// # Errors
/// Returns an error if position is invalid
pub fn set_at_pos(&mut self, pos: u128, new: bool) -> Result<(), BBoolError> {
if pos < Self::CAP {
let byte_index = (pos / 8) as usize;
let bit_offset = pos % 8;
// Extend the vector if necessary
while byte_index >= self.store.len() {
self.store.push(0);
}
let mask = 1u8 << bit_offset;
if new {
self.store[byte_index] |= mask;
} else {
self.store[byte_index] &= !mask;
}
return Ok(());
}
Err(BBoolError::InvalidPosInf(pos))
}
/// Gets the value at the current head position and increments the head position.
///
/// # Examples
/// ```
/// use btypes::inf_bbool::BInf;
/// use anyhow::Result;
/// fn main() -> Result<()> {
/// let mut bools = BInf::from_vec(vec![5]);
/// let value = bools.next_b()?;
/// Ok(())
/// }
/// ```
///
/// # Errors
/// Returns an error if:
/// * Getting the current value fails
/// * Incrementing the head position fails
pub fn next_b(&mut self) -> Result<bool> {
let val = self.get()?;
self.inc()?;
Ok(val)
}
/// Gets the value at the current head position, wipes it, and increments the head position.
///
/// # Examples
/// ```
/// use btypes::inf_bbool::BInf;
/// use anyhow::Result;
/// fn main() -> Result<()> {
/// let mut bools = BInf::from_vec(vec![5]);
/// let value = bools.next_b_res()?;
/// Ok(())
/// }
/// ```
///
/// # Errors
/// Returns an error if:
/// * Getting the current value fails
/// * Setting the value fails
/// * Incrementing the head position fails
pub fn next_b_res(&mut self) -> Result<bool> {
let val = self.get()?;
self.set(false)?;
self.inc()?;
Ok(val)
}
/// Increments the head position by 1.
///
/// # Examples
/// ```
/// use btypes::inf_bbool::BInf;
/// use anyhow::Result;
/// fn main() -> Result<()> {
/// let mut bools = BInf::new();
/// bools.inc()?;
/// Ok(())
/// }
/// ```
///
/// # Errors
/// Returns an error if the new head position would be invalid
pub fn inc(&mut self) -> Result<(), BBoolError> {
if self.reader_head_pos + 1 < Self::CAP {
self.reader_head_pos += 1;
return Ok(());
}
Err(BBoolError::InvalidHeadPosInf(self.reader_head_pos))
}
/// Increments the head position by 1 without validity checks.
///
/// # Safety
/// This function performs no bounds checking. The caller must ensure the new head position will be valid.
pub unsafe fn inc_unchecked(&mut self) {
self.reader_head_pos += 1;
}
/// Sets the head position without validity checks.
///
/// # Arguments
/// * `new` - The new head position
///
/// # Safety
/// This function performs no bounds checking. The caller must ensure the new head position is valid.
pub unsafe fn shp_unchecked(&mut self, new: u128) {
self.reader_head_pos = new;
}
/// Sets the head position to a new value.
///
/// # Arguments
/// * new - The new head position
///
/// # Examples
/// ```
/// use btypes::inf_bbool::BInf;
/// use anyhow::Result;
/// fn main() -> Result<()> {
/// let mut bools = BInf::new();
/// bools.shp(42)?;
/// Ok(())
/// }
/// ```
///
/// # Errors
/// Returns an error if the new head position would be invalid
pub fn shp(&mut self, new: u128) -> Result<(), BBoolError> {
if new < Self::CAP {
self.reader_head_pos = new;
return Ok(());
}
Err(BBoolError::InvalidHeadPosInf(new))
}
/// Gets an immutable reference to the current head position.
///
/// # Examples
/// ```
/// use btypes::inf_bbool::BInf;
/// let bools = BInf::new();
/// let head_pos = bools.ghp();
/// ```
///
#[must_use] pub const fn ghp(&self) -> &u128 {
&self.reader_head_pos
}
/// Gets a mutable reference to the current head position.
///
/// # Examples
/// ```
/// use btypes::inf_bbool::BInf;
/// let mut bools = BInf::new();
/// let head_pos_mut = bools.ghp_mut();
/// ```
///
pub fn ghp_mut(&mut self) -> &mut u128 {
&mut self.reader_head_pos
}
/// Clears all stored boolean values.
///
/// # Examples
/// ```
/// use btypes::inf_bbool::BInf;
/// let mut bools = BInf::from_vec(vec![5]);
/// bools.clear();
/// ```
///
pub fn clear(&mut self) {
self.store.clear();
}
}