use crate::core::index::BytesRef;
use crate::core::index::index_reader::Identity;
use crate::core::util::bit_util::BitUtil;
use crate::core::util::error::lucene_error::{LuceneError, Result};
use crate::core::util::ints_ref::IntsRef;
use bit_set::BitSet;
use num_traits::PrimInt;
use std::cmp::Ordering;
use std::hash::{DefaultHasher, Hash, Hasher};
use std::sync::Arc;
use wide::{f32x8, f64x4, i32x8, u8x32};
#[inline]
fn array_element_equals_f32(a: f32, b: f32) -> bool {
a.to_bits() == b.to_bits() || (a.is_nan() && b.is_nan())
}
#[inline]
fn array_element_equals_f64(a: f64, b: f64) -> bool {
a.to_bits() == b.to_bits() || (a.is_nan() && b.is_nan())
}
#[inline(never)]
fn array_equals_f32_wide(a: &[f32], b: &[f32]) -> bool {
debug_assert_eq!(a.len(), b.len());
const LANES: usize = 8;
const BLOCK_LANES: usize = LANES * 4;
const ALL_LANES: u32 = (1 << LANES) - 1;
let mut i = 0;
while i + BLOCK_LANES <= a.len() {
let raw_equal_0 = f32x8::from(&a[i..i + LANES])
.to_bits()
.simd_eq(f32x8::from(&b[i..i + LANES]).to_bits());
let raw_equal_1 = f32x8::from(&a[i + LANES..i + LANES * 2])
.to_bits()
.simd_eq(f32x8::from(&b[i + LANES..i + LANES * 2]).to_bits());
let raw_equal_2 = f32x8::from(&a[i + LANES * 2..i + LANES * 3])
.to_bits()
.simd_eq(f32x8::from(&b[i + LANES * 2..i + LANES * 3]).to_bits());
let raw_equal_3 = f32x8::from(&a[i + LANES * 3..i + BLOCK_LANES])
.to_bits()
.simd_eq(f32x8::from(&b[i + LANES * 3..i + BLOCK_LANES]).to_bits());
if (raw_equal_0 & raw_equal_1 & raw_equal_2 & raw_equal_3).to_bitmask() != ALL_LANES {
let equal_0 = raw_equal_0
| (f32x8::from(&a[i..i + LANES]).is_nan() & f32x8::from(&b[i..i + LANES]).is_nan())
.to_bits();
let equal_1 = raw_equal_1
| (f32x8::from(&a[i + LANES..i + LANES * 2]).is_nan()
& f32x8::from(&b[i + LANES..i + LANES * 2]).is_nan())
.to_bits();
let equal_2 = raw_equal_2
| (f32x8::from(&a[i + LANES * 2..i + LANES * 3]).is_nan()
& f32x8::from(&b[i + LANES * 2..i + LANES * 3]).is_nan())
.to_bits();
let equal_3 = raw_equal_3
| (f32x8::from(&a[i + LANES * 3..i + BLOCK_LANES]).is_nan()
& f32x8::from(&b[i + LANES * 3..i + BLOCK_LANES]).is_nan())
.to_bits();
if (equal_0 & equal_1 & equal_2 & equal_3).to_bitmask() != ALL_LANES {
return false;
}
}
i += BLOCK_LANES;
}
while i + LANES <= a.len() {
let a_values = f32x8::from(&a[i..i + LANES]);
let b_values = f32x8::from(&b[i..i + LANES]);
let raw_equal = a_values.to_bits().simd_eq(b_values.to_bits());
if raw_equal.to_bitmask() != ALL_LANES {
let both_nan = (a_values.is_nan() & b_values.is_nan()).to_bits();
if (raw_equal | both_nan).to_bitmask() != ALL_LANES {
return false;
}
}
i += LANES;
}
a[i..]
.iter()
.zip(&b[i..])
.all(|(&a, &b)| array_element_equals_f32(a, b))
}
#[inline(never)]
fn array_equals_f64_wide(a: &[f64], b: &[f64]) -> bool {
debug_assert_eq!(a.len(), b.len());
const LANES: usize = 4;
const BLOCK_LANES: usize = LANES * 4;
const ALL_LANES: u32 = (1 << LANES) - 1;
let mut i = 0;
while i + BLOCK_LANES <= a.len() {
let raw_equal_0 = f64x4::from(&a[i..i + LANES])
.to_bits()
.simd_eq(f64x4::from(&b[i..i + LANES]).to_bits());
let raw_equal_1 = f64x4::from(&a[i + LANES..i + LANES * 2])
.to_bits()
.simd_eq(f64x4::from(&b[i + LANES..i + LANES * 2]).to_bits());
let raw_equal_2 = f64x4::from(&a[i + LANES * 2..i + LANES * 3])
.to_bits()
.simd_eq(f64x4::from(&b[i + LANES * 2..i + LANES * 3]).to_bits());
let raw_equal_3 = f64x4::from(&a[i + LANES * 3..i + BLOCK_LANES])
.to_bits()
.simd_eq(f64x4::from(&b[i + LANES * 3..i + BLOCK_LANES]).to_bits());
if (raw_equal_0 & raw_equal_1 & raw_equal_2 & raw_equal_3).to_bitmask() != ALL_LANES {
let equal_0 = raw_equal_0
| (f64x4::from(&a[i..i + LANES]).is_nan() & f64x4::from(&b[i..i + LANES]).is_nan())
.to_bits();
let equal_1 = raw_equal_1
| (f64x4::from(&a[i + LANES..i + LANES * 2]).is_nan()
& f64x4::from(&b[i + LANES..i + LANES * 2]).is_nan())
.to_bits();
let equal_2 = raw_equal_2
| (f64x4::from(&a[i + LANES * 2..i + LANES * 3]).is_nan()
& f64x4::from(&b[i + LANES * 2..i + LANES * 3]).is_nan())
.to_bits();
let equal_3 = raw_equal_3
| (f64x4::from(&a[i + LANES * 3..i + BLOCK_LANES]).is_nan()
& f64x4::from(&b[i + LANES * 3..i + BLOCK_LANES]).is_nan())
.to_bits();
if (equal_0 & equal_1 & equal_2 & equal_3).to_bitmask() != ALL_LANES {
return false;
}
}
i += BLOCK_LANES;
}
while i + LANES <= a.len() {
let a_values = f64x4::from(&a[i..i + LANES]);
let b_values = f64x4::from(&b[i..i + LANES]);
let raw_equal = a_values.to_bits().simd_eq(b_values.to_bits());
if raw_equal.to_bitmask() != ALL_LANES {
let both_nan = (a_values.is_nan() & b_values.is_nan()).to_bits();
if (raw_equal | both_nan).to_bitmask() != ALL_LANES {
return false;
}
}
i += LANES;
}
a[i..]
.iter()
.zip(&b[i..])
.all(|(&a, &b)| array_element_equals_f64(a, b))
}
pub struct CoreHelper;
impl CoreHelper {
#[inline]
pub fn compare_f32(a: f32, b: f32) -> Ordering {
match (a.is_nan(), b.is_nan()) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Greater,
(false, true) => Ordering::Less,
(false, false) => a.total_cmp(&b),
}
}
#[inline]
pub fn compare_f64(a: f64, b: f64) -> Ordering {
match (a.is_nan(), b.is_nan()) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Greater,
(false, true) => Ordering::Less,
(false, false) => a.total_cmp(&b),
}
}
#[inline]
pub fn max_f32(a: f32, b: f32) -> f32 {
if a.is_nan() {
a
} else if b.is_nan() {
b
} else if a == 0.0 && b == 0.0 {
if a.is_sign_positive() { a } else { b }
} else if a >= b {
a
} else {
b
}
}
#[inline]
pub fn min_f32(a: f32, b: f32) -> f32 {
if a.is_nan() {
a
} else if b.is_nan() {
b
} else if a == 0.0 && b == 0.0 {
if a.is_sign_positive() { b } else { a }
} else if a <= b {
a
} else {
b
}
}
#[inline]
pub fn max_f64(a: f64, b: f64) -> f64 {
if a.is_nan() {
a
} else if b.is_nan() {
b
} else if a == 0.0 && b == 0.0 {
if a.is_sign_positive() { a } else { b }
} else if a >= b {
a
} else {
b
}
}
#[inline]
pub fn min_f64(a: f64, b: f64) -> f64 {
if a.is_nan() {
a
} else if b.is_nan() {
b
} else if a == 0.0 && b == 0.0 {
if a.is_sign_positive() { b } else { a }
} else if a <= b {
a
} else {
b
}
}
#[inline]
pub fn hash_bits_f32_for_primitive_eq(value: f32) -> u32 {
BitUtil::float_to_int_bits(if value == 0.0 { 0.0 } else { value }) as u32
}
#[inline]
pub fn hash_bits_f64_for_primitive_eq(value: f64) -> u64 {
BitUtil::double_to_long_bits(if value == 0.0 { 0.0 } else { value }) as u64
}
#[inline]
pub fn array_equals_f32(a: &[f32], b: &[f32]) -> bool {
if a.len() != b.len() {
return false;
}
if a.is_empty() {
return true;
}
if !array_element_equals_f32(a[0], b[0]) {
return false;
}
if std::ptr::eq(a, b) {
return true;
}
if a.len() < 32 {
return a[1..]
.iter()
.zip(&b[1..])
.all(|(&a, &b)| array_element_equals_f32(a, b));
}
array_equals_f32_wide(a, b)
}
#[inline]
pub fn array_equals_f64(a: &[f64], b: &[f64]) -> bool {
if a.len() != b.len() {
return false;
}
if a.is_empty() {
return true;
}
if !array_element_equals_f64(a[0], b[0]) {
return false;
}
if std::ptr::eq(a, b) {
return true;
}
if a.len() < 16 {
return a[1..]
.iter()
.zip(&b[1..])
.all(|(&a, &b)| array_element_equals_f64(a, b));
}
array_equals_f64_wide(a, b)
}
pub const CLONE_WARRING: &'static str = "does not implement the Clone logic.
The purpose of implementing the Clone trait is to make it could be used with Cow";
pub fn check_from_index_size(from_index: usize, size: usize, length: usize) -> Result<usize> {
if from_index > length || size > length - from_index {
Err(LuceneError::array_index_out_of_bounds(format!(
"size: {size} is too large, from_index: {from_index}, length: {length}"
)))
} else {
Ok(from_index)
}
}
pub fn check_from_to_index(from_index: usize, to_index: usize, length: usize) -> Result<usize> {
if from_index > to_index || to_index > length {
return Err(LuceneError::array_index_out_of_bounds(format!(
"index out of bounds: from_index={from_index} to_index={to_index} length={length}"
)));
}
Ok(from_index)
}
pub fn check_index<I>(index: I, length: I) -> Result<I>
where
I: PrimInt + std::fmt::Display,
{
if index < I::zero() || length < I::zero() || index >= length {
return Err(LuceneError::array_index_out_of_bounds(format!(
"index out of bounds: index={index} length={length}"
)));
}
Ok(index)
}
pub fn miss_match_u8(a: &[u8], b: &[u8]) -> i32 {
let common_len = a.len().min(b.len());
let mut i = 0;
if common_len >= 32 {
let prefix_limit = common_len.min(128);
while i + 32 <= prefix_limit {
let equal = u8x32::from(&a[i..i + 32]).simd_eq(u8x32::from(&b[i..i + 32]));
let mismatch = !equal.to_bitmask();
if mismatch != 0 {
return (i + mismatch.trailing_zeros() as usize) as i32;
}
i += 32;
}
if common_len >= 512 {
while i + 128 <= common_len {
let mismatch = a[i..i + 128]
.iter()
.zip(&b[i..i + 128])
.fold(0u8, |all, (&a, &b)| all | (a ^ b));
if mismatch != 0 {
break;
}
i += 128;
}
}
while i + 32 <= common_len {
let equal = u8x32::from(&a[i..i + 32]).simd_eq(u8x32::from(&b[i..i + 32]));
let mismatch = !equal.to_bitmask();
if mismatch != 0 {
return (i + mismatch.trailing_zeros() as usize) as i32;
}
i += 32;
}
}
while i < common_len && a[i] == b[i] {
i += 1;
}
if i < common_len || a.len() != b.len() {
i as i32
} else {
-1
}
}
pub fn miss_match_i32(a: &[i32], b: &[i32]) -> i32 {
let common_len = a.len().min(b.len());
let mut i = 0;
while i + 8 <= common_len {
let equal = i32x8::from(&a[i..i + 8]).simd_eq(i32x8::from(&b[i..i + 8]));
let mismatch = (!equal.to_bitmask()) & 0xff;
if mismatch != 0 {
return (i + mismatch.trailing_zeros() as usize) as i32;
}
i += 8;
}
while i < common_len && a[i] == b[i] {
i += 1;
}
if i < common_len || a.len() != b.len() {
i as i32
} else {
-1
}
}
pub fn take_and_reset<T, F>(target: &mut T, reset_fn: F) -> T
where
F: FnOnce(&T) -> T,
{
let replacement = reset_fn(target);
std::mem::replace(target, replacement)
}
pub fn calculate_hash<T>(value: &T) -> u64
where
T: Hash + ?Sized,
{
let mut hasher = DefaultHasher::new();
value.hash(&mut hasher);
hasher.finish()
}
}
pub trait ToInt {
fn to_int(&self) -> i32;
}
impl ToInt for Ordering {
fn to_int(&self) -> i32 {
match self {
Ordering::Less => -1,
Ordering::Equal => 0,
Ordering::Greater => 1,
}
}
}
pub trait OptionTakeExt<T> {
fn take_do_return<R, F>(&mut self, f: F) -> Result<R>
where
F: FnOnce(&mut T) -> Result<R>;
}
impl<T> OptionTakeExt<T> for Option<T> {
fn take_do_return<R, F>(&mut self, f: F) -> Result<R>
where
F: FnOnce(&mut T) -> Result<R>,
{
let mut val = self
.take()
.ok_or_else(|| LuceneError::illegal_state("Option was None"))?;
let res = f(&mut val);
*self = Some(val);
res
}
}
pub trait BitSetExt {
fn next_set_bit(&self, from: usize) -> i32;
}
impl BitSetExt for BitSet {
fn next_set_bit(&self, from: usize) -> i32 {
const BITS_PER_BLOCK: usize = u32::BITS as usize;
let blocks = self.get_ref().storage();
let mut block_index = from / BITS_PER_BLOCK;
if block_index >= blocks.len() {
return -1;
}
let mut block = blocks[block_index] & (u32::MAX << (from % BITS_PER_BLOCK));
while block == 0 {
block_index += 1;
if block_index == blocks.len() {
return -1;
}
block = blocks[block_index];
}
(block_index * BITS_PER_BLOCK + block.trailing_zeros() as usize) as i32
}
}
pub trait OutputIdentity {
fn is_same_reference(&self, other: &Self) -> bool;
}
impl OutputIdentity for Arc<i64> {
fn is_same_reference(&self, other: &Self) -> bool {
Arc::ptr_eq(self, other)
}
}
impl OutputIdentity for BytesRef<Arc<Vec<u8>>> {
fn is_same_reference(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.bytes, &other.bytes)
&& self.offset == other.offset
&& self.length == other.length
}
}
impl OutputIdentity for IntsRef<Arc<Vec<i32>>> {
fn is_same_reference(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.ints, &other.ints)
&& self.offset == other.offset
&& self.length == other.length
}
}
pub trait HashCode {
fn hash_code(&self) -> i32;
}
impl HashCode for i32 {
fn hash_code(&self) -> i32 {
*self
}
}
impl HashCode for i64 {
fn hash_code(&self) -> i32 {
let value = *self as u64;
let high = value >> 32;
let mixed = value ^ high;
(mixed & 0xFFFF_FFFF) as i32
}
}
impl HashCode for Arc<i64> {
fn hash_code(&self) -> i32 {
(**self).hash_code()
}
}
#[derive(Clone)]
pub struct IdentityArc<T> {
pub object: Arc<T>,
}
impl<T> IdentityArc<T> {
pub fn new(object: Arc<T>) -> Self {
Self { object }
}
}
impl<T> PartialEq for IdentityArc<T> {
fn eq(&self, other: &Self) -> bool {
Arc::as_ptr(&self.object) == Arc::as_ptr(&other.object)
}
}
impl<T> Eq for IdentityArc<T> {}
impl<T> Hash for IdentityArc<T> {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
Arc::as_ptr(&self.object).hash(state)
}
}
pub trait TryIntoInt<T> {
fn try_convert(self) -> Result<T>;
}
macro_rules! impl_try_convert {
($src:ty => $dst:ty) => {
impl TryIntoInt<$dst> for $src {
#[inline]
fn try_convert(self) -> Result<$dst> {
<$dst>::try_from(self).map_err(|_| {
LuceneError::illegal_state(format!(
"value {} does not fit into {}",
self,
stringify!($dst)
))
})
}
}
};
}
impl_try_convert!(usize => i32);
impl_try_convert!(usize => i64);
impl_try_convert!(usize => u64);
impl_try_convert!(usize => u32);
impl_try_convert!(u64 => i64);
impl_try_convert!(u64 => usize);
impl_try_convert!(i64 => i32);
impl_try_convert!(i64 => usize);
impl_try_convert!(i32 => usize);
impl_try_convert!(u32 => i32);
impl_try_convert!(i64 => u8);
pub trait HasIdentity {
fn identity(&self) -> &Identity;
#[inline]
fn is_same_identity<T>(&self, other: &T) -> bool
where
Self: Sized,
T: HasIdentity + ?Sized,
{
self.identity() == other.identity()
}
}
impl<T> HasIdentity for Arc<T>
where
T: HasIdentity + ?Sized,
{
fn identity(&self) -> &Identity {
(**self).identity()
}
}
impl<T> HasIdentity for &T
where
T: HasIdentity + ?Sized,
{
fn identity(&self) -> &Identity {
(**self).identity()
}
}
#[macro_export]
macro_rules! impl_from_for_enum {
($enum_ty:ident, $( $src_ty:ty => $variant:ident ),+ $(,)?) => {
$(
impl From<$src_ty> for $enum_ty {
fn from(v: $src_ty) -> Self {
$enum_ty::$variant(v)
}
}
)+
};
}