Skip to content

Commit

Permalink
Merge branch 'cei-lib' into comp-ext-infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
generic-github-user committed Oct 30, 2022
2 parents 20191a0 + ff2fbbd commit 65b8919
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,91 @@ pub fn Sequence.shuffled <T> (values: Sequence<T>) ->
pub fn math.factorial (x : Number) -> Number {
range(1, x+1).product()
}


type Vec<T> : Collection {
data: Array<T; data.length == length>
length: uint
capacity: uint >= length

pub fn new (init_capacity:uint=128) -> Vec<T> {
return Vec {
data: malloc(init_capacity * T.size).cast(Array<T>)
length: 0
capacity: init_capacity
}
}

// Deallocates the buffer associated with this vector
pub mut fn free (self) -> None {
self.data.free()
self.{length, capacity} = 0
return None
}

pub fn iter (self) -> Iterator<Option<T>; .length == self.length> {
return Iterator {
index: 0
next: () => {
return
if index < self.length : Some(self[index++])
else : None
}
}
}

pub mut fn push (self, value: T) -> Vec<T; length == self.length + 1> {
complexity { time: O(1) }

if (self.capacity - self.length) < 16 {
let new_capacity = self.capacity + 128
self.data = realloc(self.data, new_capacity)
self.capacity = new_capacity
}
self[self.length ++] = value
return self
}

pub mut fn pop (self) -> Option<T> {
complexity { time: O(1) }

if self.length > 0 : return Some(self[-- self.length])
else : return None
}

pub mut fn empty (self) -> Vec<T> {
complexity { time: O(1) }
self.length = 0
self
}

pub mut fn remove (self, i: uint) -> Vec<T> {
complexity { time: O(n) }
let value = self[i]
range(i, self.length).each(j => {
self[j] = self[j + 1]
})
self.length --
value
}

pub fn cast (self, Q<.size == T.size>) -> Vec<Q> {
complexity { time: O(1) }
return Vec {
data: self.data.cast(Array<Q>)
length: self.length
capacity: self.capacity
}
}

pub fn len (self) -> uint {
complexity { time: O(1) }
self.length
}

// may or may not be based on
// https://doc.rust-lang.org/src/alloc/vec/mod.rs.html#1961
pub fn is_empty (self) -> bool {
self.len() == 0
}
}

0 comments on commit 65b8919

Please sign in to comment.