1
0
Fork 0
forked from wry/wry

autocommit 2022-01-08 16:57:40 CET

This commit is contained in:
Julian Orth 2022-01-08 16:57:40 +01:00
parent f8e7557d1d
commit 33549184d4
42 changed files with 2072 additions and 190 deletions

View file

@ -28,6 +28,14 @@ impl<K: Eq + Hash, V: Clone> CopyHashMap<K, V> {
self.map.borrow_mut().get(k).cloned()
}
pub fn get_or_insert_default(&self, k: K) -> V
where
V: Default,
{
let mut map = self.map.borrow_mut();
map.entry(k).or_insert_with(|| Default::default()).clone()
}
pub fn remove(&self, k: &K) -> Option<V> {
self.map.borrow_mut().remove(k)
}

View file

@ -34,11 +34,11 @@ impl<T> LinkedList<T> {
}
}
pub fn prepend(&self, t: T) -> Node<T> {
pub fn add_last(&self, t: T) -> Node<T> {
self.root.prepend(t)
}
pub fn append(&self, t: T) -> Node<T> {
pub fn add_first(&self, t: T) -> Node<T> {
self.root.append(t)
}
@ -53,6 +53,18 @@ impl<T> LinkedList<T> {
}
}
}
pub fn rev_iter(&self) -> RevLinkedListIter<T> {
unsafe {
let root = self.root.data.as_ref();
root.rc.fetch_add(1);
root.prev.get().as_ref().rc.fetch_add(1);
RevLinkedListIter {
root: self.root.data,
next: root.prev.get(),
}
}
}
}
pub struct LinkedListIter<T> {
@ -85,6 +97,36 @@ impl<T> Drop for LinkedListIter<T> {
}
}
pub struct RevLinkedListIter<T> {
root: NonNull<NodeData<T>>,
next: NonNull<NodeData<T>>,
}
impl<T> Iterator for RevLinkedListIter<T> {
type Item = NodeRef<T>;
fn next(&mut self) -> Option<Self::Item> {
if self.root == self.next {
return None;
}
unsafe {
let old_next = self.next;
self.next = old_next.as_ref().prev.get();
self.next.as_ref().rc.fetch_add(1);
Some(NodeRef { data: old_next })
}
}
}
impl<T> Drop for RevLinkedListIter<T> {
fn drop(&mut self) {
unsafe {
dec_ref_count(self.root, 1);
dec_ref_count(self.next, 1);
}
}
}
pub struct Node<T> {
data: NonNull<NodeData<T>>,
}