use crate::core::index::index_reader::Identity;
use crate::core::search::doc_id_set_iterator::DocIdSetIterator;
use crate::core::search::doc_id_set_iterator::NO_MORE_DOCS;
use crate::core::util::accountable::Accountable;
use crate::core::util::bits::Bits;
use crate::core::util::error::lucene_error::Result;
use crate::core::util::fixed_bit_set::FixedBitSet;
use crate::core::util::sparse_fixed_bit_set::SparseFixedBitSet;
use std::rc::Rc;
use std::sync::Arc;
pub trait BitSetExtensions {
fn as_fixed_bit_set(&self) -> Option<&FixedBitSet> {
None
}
fn as_sparse_fixed_bit_set(&self) -> Option<&SparseFixedBitSet> {
None
}
}
pub trait BitSet: Bits + Accountable + BitSetExtensions {
fn clear(&mut self) -> Result<()> {
self.clear_range(0, self.length())
}
fn set(&mut self, i: usize) -> Result<()>;
fn get_and_set(&mut self, i: usize) -> Result<bool>;
fn clear_with_index(&mut self, i: usize) -> Result<()>;
fn clear_range(&mut self, start_index: usize, end_index: usize) -> Result<()>;
fn cardinality(&self) -> usize;
fn approximate_cardinality(&self) -> usize;
fn prev_set_bit(&self, index: usize) -> Option<usize>;
fn next_set_bit(&self, index: usize) -> usize {
self.next_set_bit_range(index, self.length())
}
fn next_set_bit_range(&self, start: usize, end: usize) -> usize;
fn or<T>(&mut self, iter: &mut T) -> Result<()>
where
T: DocIdSetIterator;
fn default_or<T>(&mut self, iter: &mut T) -> Result<()>
where
T: DocIdSetIterator,
{
check_unpositioned(iter)?;
loop {
let doc = iter.next_doc()?.try_convert()?;
if doc == NO_MORE_DOCS as usize {
break;
}
self.set(doc)?;
}
Ok(())
}
fn ensure_capacity(&mut self, _num_bits: usize) -> Result<()> {
Ok(())
}
}
pub enum BitSetEnum2<A, B> {
A(A),
B(B),
}
impl<A, B> HasIdentity for BitSetEnum2<A, B>
where
A: HasIdentity,
B: HasIdentity,
{
fn identity(&self) -> &Identity {
match self {
BitSetEnum2::A(t) => t.identity(),
BitSetEnum2::B(s) => s.identity(),
}
}
}
impl<A, B> Bits for BitSetEnum2<A, B>
where
A: BitSet,
B: BitSet,
{
fn get(&self, index: usize) -> Result<bool> {
match self {
BitSetEnum2::A(t) => t.get(index),
BitSetEnum2::B(s) => s.get(index),
}
}
fn length(&self) -> usize {
match self {
BitSetEnum2::A(t) => t.length(),
BitSetEnum2::B(s) => s.length(),
}
}
fn copy_of(&self) -> Result<FixedBitSet> {
match self {
BitSetEnum2::A(t) => t.copy_of(),
BitSetEnum2::B(s) => s.copy_of(),
}
}
}
impl<A, B> Accountable for BitSetEnum2<A, B>
where
A: Accountable,
B: Accountable,
{
fn ram_bytes_used(&self) -> Result<i64> {
match self {
BitSetEnum2::A(t) => t.ram_bytes_used(),
BitSetEnum2::B(s) => s.ram_bytes_used(),
}
}
}
impl<A, B> BitSet for BitSetEnum2<A, B>
where
A: BitSet,
B: BitSet,
{
fn clear(&mut self) -> Result<()> {
match self {
BitSetEnum2::A(t) => t.clear(),
BitSetEnum2::B(s) => s.clear(),
}
}
fn set(&mut self, i: usize) -> Result<()> {
match self {
BitSetEnum2::A(t) => t.set(i),
BitSetEnum2::B(s) => s.set(i),
}
}
fn get_and_set(&mut self, i: usize) -> Result<bool> {
match self {
BitSetEnum2::A(t) => t.get_and_set(i),
BitSetEnum2::B(s) => s.get_and_set(i),
}
}
fn clear_with_index(&mut self, i: usize) -> Result<()> {
match self {
BitSetEnum2::A(t) => t.clear_with_index(i),
BitSetEnum2::B(s) => s.clear_with_index(i),
}
}
fn clear_range(&mut self, start_index: usize, end_index: usize) -> Result<()> {
match self {
BitSetEnum2::A(t) => t.clear_range(start_index, end_index),
BitSetEnum2::B(s) => s.clear_range(start_index, end_index),
}
}
fn cardinality(&self) -> usize {
match self {
BitSetEnum2::A(t) => t.cardinality(),
BitSetEnum2::B(s) => s.cardinality(),
}
}
fn approximate_cardinality(&self) -> usize {
match self {
BitSetEnum2::A(t) => t.approximate_cardinality(),
BitSetEnum2::B(s) => s.approximate_cardinality(),
}
}
fn prev_set_bit(&self, index: usize) -> Option<usize> {
match self {
BitSetEnum2::A(t) => t.prev_set_bit(index),
BitSetEnum2::B(s) => s.prev_set_bit(index),
}
}
fn next_set_bit(&self, index: usize) -> usize {
match self {
BitSetEnum2::A(t) => t.next_set_bit(index),
BitSetEnum2::B(s) => s.next_set_bit(index),
}
}
fn next_set_bit_range(&self, start: usize, end: usize) -> usize {
match self {
BitSetEnum2::A(t) => t.next_set_bit_range(start, end),
BitSetEnum2::B(s) => s.next_set_bit_range(start, end),
}
}
fn or<T>(&mut self, iter: &mut T) -> Result<()>
where
T: DocIdSetIterator,
{
match self {
BitSetEnum2::A(t) => t.or(iter),
BitSetEnum2::B(s) => s.or(iter),
}
}
fn ensure_capacity(&mut self, _num_bits: usize) -> Result<()> {
match self {
BitSetEnum2::A(t) => t.ensure_capacity(_num_bits),
BitSetEnum2::B(s) => s.ensure_capacity(_num_bits),
}
}
}
impl<A, B> BitSetExtensions for BitSetEnum2<A, B>
where
A: BitSet,
B: BitSet,
{
fn as_fixed_bit_set(&self) -> Option<&FixedBitSet> {
match self {
BitSetEnum2::A(t) => t.as_fixed_bit_set(),
BitSetEnum2::B(s) => s.as_fixed_bit_set(),
}
}
fn as_sparse_fixed_bit_set(&self) -> Option<&SparseFixedBitSet> {
match self {
BitSetEnum2::A(t) => t.as_sparse_fixed_bit_set(),
BitSetEnum2::B(s) => s.as_sparse_fixed_bit_set(),
}
}
}
impl<T> BitSet for Arc<T>
where
T: BitSet,
{
fn clear(&mut self) -> Result<()> {
Err(LuceneError::unsupported_operation(
"cannot mutate a BitSet through Arc",
))
}
fn set(&mut self, _i: usize) -> Result<()> {
Err(LuceneError::unsupported_operation(
"cannot mutate a BitSet through Arc",
))
}
fn get_and_set(&mut self, _i: usize) -> Result<bool> {
Err(LuceneError::unsupported_operation(
"cannot mutate a BitSet through Arc",
))
}
fn clear_with_index(&mut self, _i: usize) -> Result<()> {
Err(LuceneError::unsupported_operation(
"cannot mutate a BitSet through Arc",
))
}
fn clear_range(&mut self, _start_index: usize, _end_index: usize) -> Result<()> {
Err(LuceneError::unsupported_operation(
"cannot mutate a BitSet through Arc",
))
}
fn cardinality(&self) -> usize {
(**self).cardinality()
}
fn approximate_cardinality(&self) -> usize {
(**self).approximate_cardinality()
}
fn prev_set_bit(&self, index: usize) -> Option<usize> {
(**self).prev_set_bit(index)
}
fn next_set_bit(&self, index: usize) -> usize {
(**self).next_set_bit(index)
}
fn next_set_bit_range(&self, start: usize, end: usize) -> usize {
(**self).next_set_bit_range(start, end)
}
fn or<T1>(&mut self, _iter: &mut T1) -> Result<()>
where
T1: DocIdSetIterator,
{
Err(LuceneError::unsupported_operation(
"cannot mutate a BitSet through Arc",
))
}
fn default_or<T1>(&mut self, _iter: &mut T1) -> Result<()>
where
T1: DocIdSetIterator,
{
Err(LuceneError::unsupported_operation(
"cannot mutate a BitSet through Arc",
))
}
fn ensure_capacity(&mut self, _num_bits: usize) -> Result<()> {
Err(LuceneError::unsupported_operation(
"cannot mutate a BitSet through Arc",
))
}
}
impl<T> BitSetExtensions for Arc<T>
where
T: BitSet,
{
fn as_fixed_bit_set(&self) -> Option<&FixedBitSet> {
(**self).as_fixed_bit_set()
}
fn as_sparse_fixed_bit_set(&self) -> Option<&SparseFixedBitSet> {
(**self).as_sparse_fixed_bit_set()
}
}
impl<T> HasIdentity for Rc<T>
where
T: HasIdentity,
{
fn identity(&self) -> &Identity {
(**self).identity()
}
}
impl<T> Bits for Rc<T>
where
T: BitSet,
{
fn get(&self, index: usize) -> Result<bool> {
(**self).get(index)
}
fn length(&self) -> usize {
(**self).length()
}
}
impl<T> BitSet for Rc<T>
where
T: BitSet,
{
fn clear(&mut self) -> Result<()> {
Err(LuceneError::unsupported_operation(
"cannot mutate a BitSet through Rc",
))
}
fn set(&mut self, _i: usize) -> Result<()> {
Err(LuceneError::unsupported_operation(
"cannot mutate a BitSet through Rc",
))
}
fn get_and_set(&mut self, _i: usize) -> Result<bool> {
Err(LuceneError::unsupported_operation(
"cannot mutate a BitSet through Rc",
))
}
fn clear_with_index(&mut self, _i: usize) -> Result<()> {
Err(LuceneError::unsupported_operation(
"cannot mutate a BitSet through Rc",
))
}
fn clear_range(&mut self, _start_index: usize, _end_index: usize) -> Result<()> {
Err(LuceneError::unsupported_operation(
"cannot mutate a BitSet through Rc",
))
}
fn cardinality(&self) -> usize {
(**self).cardinality()
}
fn approximate_cardinality(&self) -> usize {
(**self).approximate_cardinality()
}
fn prev_set_bit(&self, index: usize) -> Option<usize> {
(**self).prev_set_bit(index)
}
fn next_set_bit(&self, index: usize) -> usize {
(**self).next_set_bit(index)
}
fn next_set_bit_range(&self, start: usize, end: usize) -> usize {
(**self).next_set_bit_range(start, end)
}
fn or<T1>(&mut self, _iter: &mut T1) -> Result<()>
where
T1: DocIdSetIterator,
{
Err(LuceneError::unsupported_operation(
"cannot mutate a BitSet through Rc",
))
}
fn default_or<T1>(&mut self, _iter: &mut T1) -> Result<()>
where
T1: DocIdSetIterator,
{
Err(LuceneError::unsupported_operation(
"cannot mutate a BitSet through Rc",
))
}
fn ensure_capacity(&mut self, _num_bits: usize) -> Result<()> {
Err(LuceneError::unsupported_operation(
"cannot mutate a BitSet through Rc",
))
}
}
impl<T> BitSetExtensions for Rc<T>
where
T: BitSet,
{
fn as_fixed_bit_set(&self) -> Option<&FixedBitSet> {
(**self).as_fixed_bit_set()
}
fn as_sparse_fixed_bit_set(&self) -> Option<&SparseFixedBitSet> {
(**self).as_sparse_fixed_bit_set()
}
}
use crate::core::util::error::lucene_error::LuceneError;
use crate::core::util::{HasIdentity, TryIntoInt};
pub fn of<I>(it: &mut I, max_doc: usize) -> Result<SparseFixedBitSetBitSet>
where
I: DocIdSetIterator,
{
let cost = it.cost()?;
let threshold = max_doc >> 7;
let mut set;
if cost < (threshold as i64) {
set = BitSetEnum2::A(SparseFixedBitSet::new(max_doc)?);
} else {
let result = FixedBitSet::new(max_doc);
set = BitSetEnum2::B(result);
};
set.or(it)?;
Ok(set)
}
pub(crate) fn check_unpositioned<I>(iter: &I) -> Result<()>
where
I: DocIdSetIterator,
{
if iter.doc_id() != -1 {
return Err(LuceneError::illegal_state(format!(
"This operation only works with an unpositioned iterator, got current position = {}",
iter.doc_id()
)));
}
Ok(())
}
pub type SparseFixedBitSetBitSet = BitSetEnum2<SparseFixedBitSet, FixedBitSet>;