use crate::BitmapView;
use core::fmt;
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Sub, SubAssign};
use super::Bitmap;
impl fmt::Debug for Bitmap {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.cardinality() < 32 {
write!(f, "Bitmap<[")?;
let mut first = true;
for value in self.iter() {
let prefix = if first {
first = false;
""
} else {
", "
};
write!(f, "{prefix}{value}")?;
}
write!(f, "]>")?;
Ok(())
} else {
write!(
f,
"Bitmap<{:?} values between {:?} and {:?}>",
self.cardinality(),
self.minimum().unwrap(),
self.maximum().unwrap()
)
}
}
}
impl Default for Bitmap {
fn default() -> Self {
Self::new()
}
}
impl From<&'_ [u32]> for Bitmap {
#[inline]
#[doc(alias = "roaring_bitmap_of_ptr")]
fn from(values: &'_ [u32]) -> Self {
Self::of(values)
}
}
impl<const N: usize> From<[u32; N]> for Bitmap {
#[inline]
#[doc(alias = "roaring_bitmap_of_ptr")]
fn from(values: [u32; N]) -> Self {
Self::of(&values)
}
}
impl PartialEq for Bitmap {
#[inline]
#[doc(alias = "roaring_bitmap_equals")]
fn eq(&self, other: &Bitmap) -> bool {
unsafe { ffi::roaring_bitmap_equals(&self.bitmap, &other.bitmap) }
}
}
impl PartialEq<BitmapView<'_>> for Bitmap {
#[inline]
fn eq(&self, other: &BitmapView) -> bool {
unsafe { ffi::roaring_bitmap_equals(&self.bitmap, &other.bitmap) }
}
}
impl PartialEq<Bitmap> for BitmapView<'_> {
#[inline]
fn eq(&self, other: &Bitmap) -> bool {
unsafe { ffi::roaring_bitmap_equals(&self.bitmap, &other.bitmap) }
}
}
impl Eq for Bitmap {}
impl Clone for Bitmap {
#[inline]
fn clone(&self) -> Self {
let mut result = Self::new();
result.clone_from(self);
result
}
#[doc(alias = "roaring_bitmap_overwrite")]
fn clone_from(&mut self, source: &Self) {
unsafe {
let success = ffi::roaring_bitmap_overwrite(&mut self.bitmap, &source.bitmap);
assert!(success, "Memory allocation failure cloning roaring bitmap");
}
}
}
impl Drop for Bitmap {
#[allow(clippy::assertions_on_constants)]
#[doc(alias = "roaring_bitmap_clear")]
fn drop(&mut self) {
const _: () = assert!(ffi::ROARING_VERSION_MAJOR == 4);
unsafe { ffi::roaring_bitmap_clear(&mut self.bitmap) }
}
}
macro_rules! impl_binop {
(
impl $trait_name:ident {
$(type $type_name:ident = $type_value:ty;)*
$(#[$($attr:tt)*])*
fn $fn_name:ident -> $ret_ty:ty as $alias:ident
}
) => {
impl_binop!{
impl $trait_name {
$(type $type_name = $type_value;)*
$(#[$($attr)*])*
fn $fn_name(self, other) -> $ret_ty {
self.$alias(&other)
}
}
}
};
(
impl $trait_name:ident {
$(type $type_name:ident = $type_value:ty;)*
$(#[$($attr:tt)*])*
fn $fn_name:ident($self_ident:ident, $other_ident:ident) -> $ret_ty:ty
$body:block
}
) => {
impl $trait_name for Bitmap {
$(type $type_name = $type_value;)*
$(#[$($attr)*])*
fn $fn_name($self_ident, $other_ident: Bitmap) -> $ret_ty
$body
}
impl $trait_name<&Bitmap> for Bitmap {
$(type $type_name = $type_value;)*
$(#[$($attr)*])*
fn $fn_name($self_ident, $other_ident: &Bitmap) -> $ret_ty
$body
}
impl $trait_name<Bitmap> for &Bitmap {
$(type $type_name = $type_value;)*
$(#[$($attr)*])*
fn $fn_name($self_ident, $other_ident: Bitmap) -> $ret_ty
$body
}
impl $trait_name<&Bitmap> for &Bitmap {
$(type $type_name = $type_value;)*
$(#[$($attr)*])*
fn $fn_name($self_ident, $other_ident: &Bitmap) -> $ret_ty
$body
}
impl $trait_name for BitmapView<'_> {
$(type $type_name = $type_value;)*
$(#[$($attr)*])*
fn $fn_name($self_ident, $other_ident: BitmapView<'_>) -> $ret_ty
$body
}
impl $trait_name<&BitmapView<'_>> for BitmapView<'_> {
$(type $type_name = $type_value;)*
$(#[$($attr)*])*
fn $fn_name($self_ident, $other_ident: &BitmapView<'_>) -> $ret_ty
$body
}
impl $trait_name<BitmapView<'_>> for &BitmapView<'_> {
$(type $type_name = $type_value;)*
$(#[$($attr)*])*
fn $fn_name($self_ident, $other_ident: BitmapView<'_>) -> $ret_ty
$body
}
impl $trait_name<&BitmapView<'_>> for &BitmapView<'_> {
$(type $type_name = $type_value;)*
$(#[$($attr)*])*
fn $fn_name($self_ident, $other_ident: &BitmapView<'_>) -> $ret_ty
$body
}
impl $trait_name<Bitmap> for BitmapView<'_> {
$(type $type_name = $type_value;)*
$(#[$($attr)*])*
fn $fn_name($self_ident, $other_ident: Bitmap) -> $ret_ty
$body
}
impl $trait_name<&Bitmap> for BitmapView<'_> {
$(type $type_name = $type_value;)*
$(#[$($attr)*])*
fn $fn_name($self_ident, $other_ident: &Bitmap) -> $ret_ty
$body
}
impl $trait_name<&Bitmap> for &BitmapView<'_> {
$(type $type_name = $type_value;)*
$(#[$($attr)*])*
fn $fn_name($self_ident, $other_ident: &Bitmap) -> $ret_ty
$body
}
impl $trait_name<Bitmap> for &BitmapView<'_> {
$(type $type_name = $type_value;)*
$(#[$($attr)*])*
fn $fn_name($self_ident, $other_ident: Bitmap) -> $ret_ty
$body
}
};
}
macro_rules! impl_binop_assign {
(
impl $trait_name:ident {
$(#[$($attr:tt)*])*
fn $fn_name:ident as $alias:ident
}
) => {
impl $trait_name for Bitmap {
$(#[$($attr)*])*
fn $fn_name(&mut self, other: Bitmap) {
self.$alias(&other)
}
}
impl $trait_name<&'_ Bitmap> for Bitmap {
$(#[$($attr)*])*
fn $fn_name(&mut self, other: &Bitmap) {
self.$alias(other)
}
}
impl $trait_name<BitmapView<'_>> for Bitmap {
$(#[$($attr)*])*
fn $fn_name(&mut self, other: BitmapView<'_>) {
self.$alias(&other)
}
}
impl $trait_name<&BitmapView<'_>> for Bitmap {
$(#[$($attr)*])*
fn $fn_name(&mut self, other: &BitmapView<'_>) {
self.$alias(other)
}
}
};
}
impl_binop! {
impl BitAnd {
type Output = Bitmap;
#[inline]
#[doc(alias = "roaring_bitmap_and")]
fn bitand -> Bitmap as and
}
}
impl_binop! {
impl BitOr {
type Output = Bitmap;
#[inline]
#[doc(alias = "roaring_bitmap_or")]
fn bitor -> Bitmap as or
}
}
impl_binop! {
impl BitXor {
type Output = Bitmap;
#[inline]
#[doc(alias = "roaring_bitmap_xor")]
fn bitxor -> Bitmap as xor
}
}
impl_binop! {
impl Sub {
type Output = Bitmap;
#[inline]
#[doc(alias = "andnot")]
#[doc(alias = "roaring_bitmap_andnot")]
fn sub -> Bitmap as andnot
}
}
impl_binop_assign! {
impl BitAndAssign {
#[inline]
#[doc(alias = "roaring_bitmap_and_inplace")]
fn bitand_assign as and_inplace
}
}
impl_binop_assign! {
impl BitOrAssign {
#[inline]
#[doc(alias = "roaring_bitmap_or_inplace")]
fn bitor_assign as or_inplace
}
}
impl_binop_assign! {
impl BitXorAssign {
#[inline]
#[doc(alias = "roaring_bitmap_xor_inplace")]
fn bitxor_assign as xor_inplace
}
}
impl_binop_assign! {
impl SubAssign {
#[inline]
#[doc(alias = "andnot_inplace")]
#[doc(alias = "roaring_bitmap_andnot_inplace")]
fn sub_assign as andnot_inplace
}
}