Skip to content

Commit

Permalink
fix: devec wrong behaviour when reserve then push (#374)
Browse files Browse the repository at this point in the history
fix: devec wrong behaviour when reserve then push
  • Loading branch information
peter-jerry-ye committed May 10, 2024
1 parent 6874cd7 commit 0cabd27
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
37 changes: 33 additions & 4 deletions devec/devec.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn realloc[T](self : Devec[T]) -> Unit {
let old_cap = self.len
let new_cap = if old_cap == 0 { 8 } else { old_cap * 2 }
let new_buf = UninitializedArray::make(new_cap)
if self.len > 0 {
if old_cap > 0 {
if self.tail >= self.head {
for i = self.head, j = 0; i <= self.tail; i = i + 1, j = j + 1 {
new_buf[j] = self.buf[i]
Expand All @@ -87,9 +87,11 @@ fn realloc[T](self : Devec[T]) -> Unit {
j += 1
}
}
self.tail = self.len - 1
} else {
self.tail = 0
}
self.head = 0
self.tail = self.len - 1
self.buf = new_buf
}

Expand Down Expand Up @@ -166,7 +168,9 @@ pub fn push_front[T](self : Devec[T], value : T) -> Unit {
if self.len == self.buf.length() {
self.realloc()
}
self.head = if self.head > 0 { self.head - 1 } else { self.buf.length() - 1 }
if self.len != 0 {
self.head = (self.head + self.buf.length() - 1) % self.buf.length()
}
self.buf[self.head] = value
self.len += 1
}
Expand All @@ -184,7 +188,9 @@ pub fn push_back[T](self : Devec[T], value : T) -> Unit {
if self.len == self.buf.length() {
self.realloc()
}
self.tail = if self.tail < self.buf.length() - 1 { self.tail + 1 } else { 0 }
if self.len != 0 {
self.tail = (self.tail + self.buf.length() + 1) % self.buf.length()
}
self.buf[self.tail] = value
self.len += 1
}
Expand Down Expand Up @@ -774,3 +780,26 @@ test "as_iter" {
content="6",
)?
}

test "reserve and push" {
// capacity > 0, len = 0
let a : Devec[Option[Int]] = Devec::with_capacity(2)
a.push_back(Some(1))
inspect(a.front(), content="Some(Some(1))")?
inspect(a.back(), content="Some(Some(1))")?
// capacity = 0, len = 0
let b : Devec[Option[Int]] = Devec::with_capacity(0)
b.push_back(Some(1))
inspect(b.front(), content="Some(Some(1))")?
inspect(b.back(), content="Some(Some(1))")?
// capacity > 0, len = 0
let c : Devec[Option[Int]] = Devec::with_capacity(2)
c.push_front(Some(1))
inspect(c.front(), content="Some(Some(1))")?
inspect(c.back(), content="Some(Some(1))")?
// capacity = 0, len = 0
let d : Devec[Option[Int]] = Devec::with_capacity(0)
d.push_front(Some(1))
inspect(d.front(), content="Some(Some(1))")?
inspect(d.back(), content="Some(Some(1))")?
}
1 change: 1 addition & 0 deletions devec/types.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

priv type UninitializedArray[T] Array[UnsafeMaybeUninit[T]]

// head and tail point to non-empty slots. When the buffer is empty, head and tail points to the same slot.
struct Devec[T] {
mut buf : UninitializedArray[T]
mut len : Int
Expand Down

0 comments on commit 0cabd27

Please sign in to comment.