-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
159 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "polars-utils" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use crate::error::*; | ||
|
||
unsafe fn index_of_unchecked<T>(slice: &[T], item: &T) -> usize { | ||
(item as *const _ as usize - slice.as_ptr() as usize) / std::mem::size_of::<T>() | ||
} | ||
|
||
fn index_of<T>(slice: &[T], item: &T) -> Option<usize> { | ||
debug_assert!(std::mem::size_of::<T>() > 0); | ||
let ptr = item as *const T; | ||
unsafe { | ||
if slice.as_ptr() < ptr && slice.as_ptr().add(slice.len()) > ptr { | ||
Some(index_of_unchecked(slice, item)) | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
pub struct Node(pub usize); | ||
|
||
impl Default for Node { | ||
fn default() -> Self { | ||
Node(usize::MAX) | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct Arena<T> { | ||
items: Vec<T>, | ||
} | ||
|
||
impl<T> Default for Arena<T> { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
/// Simple Arena implementation | ||
/// Allocates memory and stores item in a Vec. Only deallocates when being dropped itself. | ||
impl<T> Arena<T> { | ||
pub fn add(&mut self, val: T) -> Node { | ||
let idx = self.items.len(); | ||
self.items.push(val); | ||
Node(idx) | ||
} | ||
|
||
pub fn pop(&mut self) -> Option<T> { | ||
self.items.pop() | ||
} | ||
|
||
pub fn len(&self) -> usize { | ||
self.items.len() | ||
} | ||
|
||
pub fn is_empty(&self) -> bool { | ||
self.items.is_empty() | ||
} | ||
|
||
pub fn new() -> Self { | ||
Arena { items: vec![] } | ||
} | ||
|
||
pub fn with_capacity(cap: usize) -> Self { | ||
Arena { | ||
items: Vec::with_capacity(cap), | ||
} | ||
} | ||
|
||
pub fn get_node(&self, val: &T) -> Option<Node> { | ||
index_of(&self.items, val).map(Node) | ||
} | ||
|
||
#[inline] | ||
pub fn get(&self, idx: Node) -> &T { | ||
debug_assert!(idx.0 < self.items.len()); | ||
unsafe { self.items.get_unchecked(idx.0) } | ||
} | ||
|
||
#[inline] | ||
pub fn get_mut(&mut self, idx: Node) -> &mut T { | ||
debug_assert!(idx.0 < self.items.len()); | ||
unsafe { self.items.get_unchecked_mut(idx.0) } | ||
} | ||
|
||
#[inline] | ||
pub fn replace(&mut self, idx: Node, val: T) { | ||
let x = self.get_mut(idx); | ||
*x = val; | ||
} | ||
} | ||
|
||
impl<T: Default> Arena<T> { | ||
#[inline] | ||
pub fn take(&mut self, idx: Node) -> T { | ||
std::mem::take(self.get_mut(idx)) | ||
} | ||
|
||
pub fn replace_with<F>(&mut self, idx: Node, f: F) | ||
where | ||
F: FnOnce(T) -> T, | ||
{ | ||
let val = self.take(idx); | ||
self.replace(idx, f(val)); | ||
} | ||
|
||
pub fn try_replace_with<F>(&mut self, idx: Node, mut f: F) -> Result<()> | ||
where | ||
F: FnMut(T) -> Result<T>, | ||
{ | ||
let val = self.take(idx); | ||
self.replace(idx, f(val)?); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use std::borrow::Cow; | ||
use std::fmt::{Display, Formatter}; | ||
|
||
type ErrString = Cow<'static, str>; | ||
|
||
#[derive(Debug)] | ||
pub enum PolarsUtilsError { | ||
ComputeError(ErrString), | ||
} | ||
|
||
impl Display for PolarsUtilsError { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
PolarsUtilsError::ComputeError(s) => { | ||
let s = s.as_ref(); | ||
write!(f, "{}", s) | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub type Result<T> = std::result::Result<T, PolarsUtilsError>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod arena; | ||
mod error; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.