use std::intrinsics;
use std::mem;
use std::cmp::Ordering;
use std::ops::Deref;
use std::sync::atomic::AtomicUsize;
use crate::asbtree::TreeByteSize;
pub enum ActionResult<T> {
Ignore,
Delete,
Upsert(T),
}
pub enum ActionResultType {
Insert,
Update,
Delete,
}
#[derive(Clone, Debug)]
pub struct Entry<K: Clone, V: Clone>(pub K, pub V);
impl<K: Clone, V: Clone> Entry<K, V> {
pub fn new(k: K, v: V) -> Self {
Entry(k, v)
}
pub fn bytes_size(&self) -> u64 {
self.0.tree_bytes_size()
+ self.1.tree_bytes_size()
}
}
impl<K: Ord + Clone, V: Clone> PartialEq for Entry<K, V> {
fn eq(&self, other: &Entry<K, V>) -> bool {
self.0 == other.0
}
}
impl<K: Ord + Clone, V: Clone> PartialOrd for Entry<K, V> {
fn partial_cmp(&self, other: &Entry<K, V>) -> Option<Ordering> {
Some(self.0.cmp(&other.0))
}
}
impl<K: Ord + Clone, V: Clone> Eq for Entry<K, V> {}
impl<K: Ord + Clone, V: Clone> Ord for Entry<K, V> {
fn cmp(&self, other: &Entry<K, V>) -> Ordering {
self.0.cmp(&other.0)
}
}
pub trait ImOrdMap {
type Key: Clone;
type Val: Clone;
fn new() -> Self;
fn from_order(vec: Vec<Entry<Self::Key, Self::Val>>) -> Self;
fn is_empty(&self) -> bool;
fn size(&self) -> usize;
fn bytes_size(&self) -> u64;
fn full_bytes_size(&self) -> u64;
fn has(&self, key: &Self::Key) -> bool;
fn get(&self, key: &Self::Key) -> Option<&Self::Val>;
fn min(&self) -> Option<&Entry<Self::Key, Self::Val>>;
fn max(&self) -> Option<&Entry<Self::Key, Self::Val>>;
fn rank(&self, key: &Self::Key) -> isize;
fn index(&self, order: usize) -> Option<&Entry<Self::Key, Self::Val>>;
fn insert(&self, key: Self::Key, val: Self::Val) -> Option<Self> where Self: Sized;
fn update(&self, key: Self::Key, val: Self::Val, copy: bool) -> Option<(Option<Self::Val>, Self)> where Self: Sized;
fn upsert(&self, key: Self::Key, val: Self::Val, copy: bool) -> (Option<Option<Self::Val>>, Self) where Self: Sized;
fn delete(&self, key: &Self::Key, copy: bool) ->Option<(Option<Self::Val>, Self)> where Self: Sized;
fn remove(&self, order: usize, copy: bool) -> Option<(Option<Entry<Self::Key, Self::Val>>, Self)> where Self: Sized;
fn pop_min(&self, copy: bool) -> Option<(Option<Entry<Self::Key, Self::Val>>, Self)> where Self: Sized;
fn pop_max(&self, copy: bool) -> Option<(Option<Entry<Self::Key, Self::Val>>, Self)> where Self: Sized;
fn action<F>(&self, key: &Self::Key, func: &mut F) -> Option<(ActionResultType, Self)> where F: FnMut(Option<&Self::Val>) -> ActionResult<Self::Val>, Self: Sized;
fn map<F>(&self, func: &mut F) -> Self where F: FnMut(&Entry<Self::Key, Self::Val>) -> ActionResult<Self::Val>, Self: Sized;
}
pub trait Iter<'a>: ImOrdMap{
type K: 'a + Clone + Ord;
type V: 'a + Clone;
type IterType: Iterator<Item= &'a Entry<Self::K, Self::V>> + Send + 'a;
fn iter(&self, key: Option<&Self::Key>, descending: bool) -> Self::IterType;
}
pub struct OrdMap<T:Clone> {
root: T,
}
impl<T: Clone> Clone for OrdMap<T> {
fn clone(&self) -> Self {
OrdMap { root: self.root.clone() }
}
}
impl<T: Clone> AsRef<T> for OrdMap<T> {
fn as_ref(&self) -> &T {
&self.root
}
}
impl<T: Clone> Deref for OrdMap<T> {
type Target = T;
fn deref(&self) -> &T {
&self.root
}
}
pub struct Keys<'a, T: Iter<'a>>{
inner: T::IterType
}
unsafe impl<'a, T: Iter<'a>> Send for Keys<'a, T> {}
impl<'a, T: Iter<'a>> Iterator for Keys<'a, T>{
type Item = &'a T::K;
fn next(&mut self) -> Option<Self::Item>{
self.inner.next().map(|Entry(k, _)| k)
}
}
pub struct Values<'a, T: Iter<'a>>{
inner: T::IterType
}
impl<'a, T: Iter<'a>> Iterator for Values<'a, T>{
type Item = &'a T::V;
fn next(&mut self) -> Option<Self::Item>{
self.inner.next().map(|Entry(_, v)| v)
}
}
impl<'a, T: ImOrdMap + Clone + Iter<'a>> OrdMap<T> {
pub fn new(map: T) -> Self {
OrdMap {
root: map,
}
}
pub fn ptr_eq(&self, old: &Self) -> bool {
unsafe { intrinsics::atomic_load_relaxed(&self.root as *const T as *const usize) == intrinsics::atomic_load_relaxed(&old.root as *const T as *const usize) }
}
pub fn root(&self) -> &T {
&self.root
}
pub fn is_empty(&self) -> bool {
self.root.is_empty()
}
pub fn size(&self) -> usize {
self.root.size()
}
pub fn has(&self, key: &T::Key) -> bool {
self.root.has(key)
}
pub fn get(&self, key: &T::Key) -> Option<&T::Val> {
self.root.get(key)
}
pub fn min(&self) -> Option<&Entry<T::Key, T::Val>> {
self.root.min()
}
pub fn max(&self) -> Option<&Entry<T::Key, T::Val>> {
self.root.max()
}
pub fn rank(&self, key: &T::Key) -> isize {
self.root.rank(key)
}
pub fn index(&self, i: usize) -> Option<&Entry<T::Key, T::Val>> {
self.root.index(i)
}
pub fn keys(&self, key: Option<&T::Key>, descending: bool) -> Keys<'a, T> {
Keys{
inner: self.root.iter(key, descending)
}
}
pub fn values(&self, key: Option<&T::Key>, descending: bool) -> Values<'a, T> {
Values{
inner: self.root.iter(key, descending)
}
}
pub fn iter(&self, key: Option<&T::Key>, descending: bool) -> T::IterType {
self.root.iter(key, descending)
}
pub fn insert(&mut self, key: T::Key, value: T::Val) -> bool {
match self.root.insert(key, value) {
Some(root) => {
self.root = root;
true
},
_ => false,
}
}
pub fn update(&mut self, key: T::Key, value: T::Val, copy: bool) -> Option<Option<T::Val>> {
match self.root.update(key, value, copy) {
Some((r, root)) => {
self.root = root;
Some(r)
},
_ => None,
}
}
pub fn upsert(&mut self, key: T::Key, value: T::Val, copy: bool) -> Option<Option<T::Val>> {
let (r, root) = self.root.upsert(key, value, copy);
self.root = root;
r
}
pub fn delete(&mut self, key: &T::Key, copy: bool) -> Option<Option<T::Val>> {
match self.root.delete(key, copy) {
Some((r, root)) => {
self.root = root;
Some(r)
},
_ => None,
}
}
pub fn remove(&mut self, i: usize, copy: bool) ->Option<Option<Entry<T::Key, T::Val>>> {
match self.root.remove(i, copy) {
Some((r, root)) => {
self.root = root;
Some(r)
},
_ => None,
}
}
pub fn pop_min(&mut self, copy: bool) ->Option<Option<Entry<T::Key, T::Val>>> {
match self.root.pop_min(copy) {
Some((r, root)) => {
self.root = root;
Some(r)
},
_ => None,
}
}
pub fn pop_max(&mut self, copy: bool) -> Option<Option<Entry<T::Key, T::Val>>> {
match self.root.pop_max(copy) {
Some((r, root)) => {
self.root = root;
Some(r)
},
_ => None,
}
}
pub fn action<F>(&mut self, key: &T::Key, func: &mut F) -> Option<ActionResultType> where F: FnMut(Option<&T::Val>) -> ActionResult<T::Val> {
match self.root.action(key, func) {
Some((r, root)) => {
self.root = root;
Some(r)
},
_ => None,
}
}
pub fn map<F>(&mut self, func: &mut F) where F: FnMut(&Entry<T::Key, T::Val>) -> ActionResult<T::Val> {
self.root = self.root.map(func);
}
pub fn safe_insert(&mut self, key: &T::Key, value: T::Val) -> bool {
let mut old = &self.root;
loop {
match old.insert(key.clone(), value.clone()) {
Some(root) => unsafe {match intrinsics::atomic_cxchg_relaxed_seqcst(&self.root as *const T as *mut usize, *(old as *const T as *const usize), *(&root as *const T as *const usize)) {
(_, true) => {
return true
}
(val, _) => old = &*(val as *const T),
}},
_ => return false,
}
}
}
pub fn safe_update(&mut self, key: &T::Key, value: T::Val, copy: bool) -> Option<Option<T::Val>> {
let mut old = &self.root;
loop {
match old.update(key.clone(), value.clone(), copy) {
Some((r, root)) => unsafe {match intrinsics::atomic_cxchg_relaxed_seqcst(&self.root as *const T as *mut usize, *(old as *const T as *const usize), *(&root as *const T as *const usize)) {
(_, true) =>{
mem::forget(root);
return Some(r)
}
(val, _) => old = &*(val as *const T),
}},
_ => return None,
}
}
}
pub fn safe_upsert(&mut self, key: &T::Key, value: T::Val, copy: bool) -> Option<Option<T::Val>> {
let mut old = &self.root;
loop {
let (r, root) = old.upsert(key.clone(), value.clone(), copy);
unsafe {match intrinsics::atomic_cxchg_relaxed_seqcst(&self.root as *const T as *mut usize, *(old as *const T as *const usize), *(&root as *const T as *const usize)) {
(_, true) =>{
mem::forget(root);
return r
}
(val, _) => old = &*(val as *const T),
}}
}
}
pub fn safe_delete(&mut self, key: &T::Key, copy: bool) -> Option<Option<T::Val>> {
let mut old = &self.root;
loop {
match old.delete(key, copy) {
Some((r, root)) => unsafe {match intrinsics::atomic_cxchg_relaxed_seqcst(&self.root as *const T as *mut usize, *(old as *const T as *const usize), *(&root as *const T as *const usize)) {
(_, true) =>{
mem::forget(root);
return Some(r)
}
(val, _) => old = &*(val as *const T),
}},
_ => return None,
}
}
}
pub fn safe_remove(&mut self, i: usize, copy: bool) ->Option<Option<Entry<T::Key, T::Val>>> {
let mut old = &self.root;
loop {
match old.remove(i, copy) {
Some((r, root)) => unsafe {match intrinsics::atomic_cxchg_relaxed_seqcst(&self.root as *const T as *mut usize, *(old as *const T as *const usize), *(&root as *const T as *const usize)) {
(_, true) =>{
mem::forget(root);
return Some(r)
}
(val, _) => old = &*(val as *const T),
}},
_ => return None,
}
}
}
pub fn safe_pop_min(&mut self, copy: bool) ->Option<Option<Entry<T::Key, T::Val>>> {
let mut old = &self.root;
loop {
match old.pop_min(copy) {
Some((r, root)) => unsafe {match intrinsics::atomic_cxchg_relaxed_seqcst(&self.root as *const T as *mut usize, *(old as *const T as *const usize), *(&root as *const T as *const usize)) {
(_, true) =>{
mem::forget(root);
return Some(r)
}
(val, _) => old = &*(val as *const T),
}},
_ => return None,
}
}
}
pub fn safe_pop_max(&mut self, copy: bool) -> Option<Option<Entry<T::Key, T::Val>>> {
let mut old = &self.root;
loop {
match old.pop_max(copy) {
Some((r, root)) => unsafe {match intrinsics::atomic_cxchg_relaxed_seqcst(&self.root as *const T as *mut usize, *(old as *const T as *const usize), *(&root as *const T as *const usize)) {
(_, true) =>{
mem::forget(root);
return Some(r)
}
(val, _) => old = &*(val as *const T),
}},
_ => return None,
}
}
}
pub fn safe_action<F>(&mut self, key: &T::Key, func: &mut F) -> Option<ActionResultType> where F: FnMut(Option<&T::Val>) -> ActionResult<T::Val> {
let mut old = &self.root;
loop {
match old.action(key, func) {
Some((r, root)) => unsafe {match intrinsics::atomic_cxchg_relaxed_seqcst(&self.root as *const T as *mut usize, *(old as *const T as *const usize), *(&root as *const T as *const usize)) {
(_, true) =>{
mem::forget(root);
return Some(r)
}
(val, _) => old = &*(val as *const T),
}},
_ => return None,
}
}
}
}