Skip to content

Commit

Permalink
fix: buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-jerry-ye committed May 11, 2024
1 parent 65c454b commit 9455b57
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
22 changes: 11 additions & 11 deletions examples/buffer/lib/buffer.mbt
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
pub struct Buffer[T] {
pub struct SBuffer[T] {
mut cap : Int
mut len : Int
mut data : Array[T]
}

pub fn Buffer::new[T : Default](capacity : Int) -> Buffer[T] {
pub fn SBuffer::new[T : Default](capacity : Int) -> SBuffer[T] {
{ cap: capacity, len: 0, data: Array::make(capacity, T::default()) }
}

pub fn capacity[T](self : Buffer[T]) -> Int {
pub fn capacity[T](self : SBuffer[T]) -> Int {
self.cap
}

pub fn length[T](self : Buffer[T]) -> Int {
pub fn length[T](self : SBuffer[T]) -> Int {
self.len
}

pub fn op_get[T](self : Buffer[T], i : Int) -> Option[T] {
pub fn op_get[T](self : SBuffer[T], i : Int) -> Option[T] {
if i < self.cap {
Some(self.data[i])
} else {
None
}
}

fn expand_size[T : Default](self : Buffer[T]) {
fn expand_size[T : Default](self : SBuffer[T]) -> Unit {
let new_capacity = if self.cap != 0 {
self.cap * 2
} else {
Expand All @@ -40,15 +40,15 @@ fn expand_size[T : Default](self : Buffer[T]) {
self.data = new_data
}

pub fn append[T : Default](self : Buffer[T], value : T) {
pub fn append[T : Default](self : SBuffer[T], value : T) -> Unit {
if self.len >= self.cap {
self.expand_size()
}
self.data[self.len] = value
self.len = self.len + 1
}

pub fn truncate[T : Default](self : Buffer[T], another : Buffer[T]) {
pub fn truncate[T : Default](self : SBuffer[T], another : SBuffer[T]) -> Unit {
let mut index = 0
while index < another.len {
if self.len >= self.cap {
Expand All @@ -60,7 +60,7 @@ pub fn truncate[T : Default](self : Buffer[T], another : Buffer[T]) {
}
}

pub fn clear[T : Default](self : Buffer[T]) {
pub fn clear[T : Default](self : SBuffer[T]) -> Unit {
let mut index = 0
while index < self.len {
self.data[index] = T::default()
Expand All @@ -69,13 +69,13 @@ pub fn clear[T : Default](self : Buffer[T]) {
self.len = 0
}

pub fn reset[T : Default](self : Buffer[T], capacity : Int) {
pub fn reset[T : Default](self : SBuffer[T], capacity : Int) -> Unit {
self.cap = capacity
self.len = 0
self.data = Array::make(capacity, T::default())
}

pub fn println[T : Show](self : Buffer[T]) {
pub fn println[T : Show](self : SBuffer[T]) -> Unit {
let mut index = 0
print('[')
while index < self.len {
Expand Down
8 changes: 3 additions & 5 deletions examples/buffer/main/main.mbt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
fn init {
let buf : @lib.Buffer[Int] = @lib.Buffer::new(5)
fn main {
let buf : @lib.SBuffer[Int] = @lib.SBuffer::new(5)
println(buf.capacity()) // 5
let mut index = 0
while index < 8 {
for index = 0; index < 8; index = index + 1 {
buf.append(index)
index = index + 1
}
println(buf.capacity()) // 10
println(buf.length()) // 8
Expand Down
2 changes: 1 addition & 1 deletion examples/buffer/main/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "main",
"is_main": true,
"import": {
"buffer/lib": ""
}
Expand Down

0 comments on commit 9455b57

Please sign in to comment.