Skip to content

Commit

Permalink
add ai generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
luk036 committed Jun 22, 2023
1 parent 54223ed commit 0f8ccc6
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 0 deletions.
73 changes: 73 additions & 0 deletions bpqueue_ai.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use std::collections::LinkedList;
type Item = LinkedList<Vec<i32>>;
const SENTINEL: Item = LinkedList::new();
struct BPQueue {
_bucket: Vec<LinkedList<Vec<i32>>>,
_max: usize,
_offset: i32,
_high: i32,
}
impl BPQueue {
fn new(a: i32, b: i32) -> Self {
assert!(a <= b);
let offset = a - 1;
let high = b - offset;
let mut bucket = Vec::with_capacity((high + 1) as usize);
for i in 0..=high {
bucket.push(LinkedList::new());
bucket[i as usize].push_back(vec![i as i32, 4848]);
}
bucket[0].push_back(SENTINEL);
BPQueue {
_bucket: bucket,
_max: 0,
_offset: offset,
_high: high,
}
}
fn clear(&mut self) {
while self._max > 0 {
self._bucket[self._max].clear();
self._max -= 1;
}
}
fn append(&mut self, mut it: Item, k: i32) {
assert!(k > self._offset);
it.front_mut().unwrap()[0] = k - self._offset;
if self._max < it.front().unwrap()[0] as usize {
self._max = it.front().unwrap()[0] as usize;
}
self._bucket[it.front().unwrap()[0] as usize].push_back(it);
}
}
impl IntoIterator for BPQueue {
type Item = Item;
type IntoIter = BPQueueIterator;
fn into_iter(self) -> Self::IntoIter {
BPQueueIterator {
bpq: self,
curkey: self._max as i32,
curitem: self.bpq._bucket[self.bpq._max].iter(),
}
}
}
struct BPQueueIterator {
bpq: BPQueue,
curkey: i32,
curitem: std::collections::linked_list::Iter<'static, Vec<i32>>,
}
impl Iterator for BPQueueIterator {
type Item = Item;
fn next(&mut self) -> Option<Self::Item> {
while self.curkey > 0 {
match self.curitem.next() {
Some(res) => return Some(res.clone()),
None => {
self.curkey -= 1;
self.curitem = self.bpq._bucket[self.curkey as usize].iter();
}
}
}
None
}
}
114 changes: 114 additions & 0 deletions dllist_ai.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
struct Dllink<T> {
next: Option<Box<Dllink<T>>>,
prev: Option<*mut Dllink<T>>,
data: T,
}

impl<T> Dllink<T> {
fn new(data: T) -> Self {
Dllink {
next: None,
prev: None,
data,
}
}

fn appendleft(&mut self, node: &mut Dllink<T>) {
node.next = self.next.take();
node.prev = Some(self as *mut Dllink<T>);
if let Some(ref mut next) = node.next {
next.prev = Some(node);
}
self.next = Some(Box::new(node));
}
}

struct DllIterator<'a, T> {
link: &'a Dllink<T>,
cur: Option<&'a Dllink<T>>,
}

impl<'a, T> Iterator for DllIterator<'a, T> {
type Item = &'a Dllink<T>;

fn next(&mut self) -> Option<Self::Item> {
if let Some(cur) = self.cur.take() {
if cur as *const Dllink<T> != self.link as *const Dllink<T> {
self.cur = cur.next.as_deref();
return Some(cur);
}
}
None
}
}

struct Dllist<T> {
head: Dllink<T>,
}

impl<T> Dllist<T> {
fn new(data: T) -> Self {
Dllist {
head: Dllink::new(data),
}
}

fn appendleft(&mut self, node: &mut Dllink<T>) {
self.head.appendleft(node);
}

fn pop(&mut self) -> Option<Box<Dllink<T>>> {
self.head.next.take().map(|mut node| {
self.head.next = node.next.take();
if let Some(ref mut next) = node.next {
next.prev = Some(&mut self.head);
}
node
})
}

fn popleft(&mut self) -> Option<Box<Dllink<T>>> {
self.head.next.take().map(|mut node| {
self.head.next = node.next.take();
if let Some(ref mut next) = node.next {
next.prev = Some(&mut self.head);
}
node
})
}

fn is_empty(&self) -> bool {
self.head.next.is_none()
}

fn iter(&self) -> DllIterator<T> {
DllIterator {
link: &self.head,
cur: self.head.next.as_deref(),
}
}
}

fn test_dllink() {
let mut L1 = Dllist::new(99);
let mut L2 = Dllist::new(99);
let mut d = Box::new(Dllink::new(1));
let mut e = Box::new(Dllink::new(2));
let mut f = Box::new(Dllink::new(3));
L1.appendleft(&mut e);
assert!(!L1.is_empty());
L1.appendleft(&mut f);
L1.append(&mut d);
L2.append(L1.pop().unwrap());
L2.append(L1.popleft().unwrap());
assert!(!L1.is_empty());
assert!(!L2.is_empty());
e.detach();
assert!(L1.is_empty());
let mut count = 0;
for _ in L2.iter() {
count += 1;
}
assert_eq!(count, 2);
}

0 comments on commit 0f8ccc6

Please sign in to comment.