Skip to content

Commit

Permalink
std::vec: add the fit method to the Vec[T] structure
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Jan 30, 2024
1 parent 2c2a139 commit f93d32e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion std/internal/dynar/dynar.jule
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ impl Dynar {
ret true
}
unsafe {
let size = if self.buff.len <= n { self.buff.len } else { n }
if self.buff.len > 0 {
copy[T](new_heap, self.buff.heap, self.buff.len)
copy[T](new_heap, self.buff.heap, size)
}
unsafe {
integ::delete_array[T](self.buff.heap)
Expand Down
11 changes: 10 additions & 1 deletion std/vec/vec.jule
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const GROW_FACTOR = 2
//
// Deallocates itself when destroyed.
//
// Vectors aren't use shared allocation between them.
// Vectors aren't use shared allocation between themselves.
// Allocates new space and copies (not deep copy) items into space.
pub struct Vec[T] {
mem: Dynar[T]
Expand Down Expand Up @@ -55,6 +55,15 @@ impl Vec {
self.mem.dispose()
}

// Set capacity to length.
// Removes additional capacity that waiting to use.
// Allocates new memory to cut additional capacity.
pub fn fit(mut self) {
if self.len() != self.cap() {
self.resize(self.len())
}
}

// Returns length.
pub fn len(self): int {
ret self.mem.buff.len
Expand Down

0 comments on commit f93d32e

Please sign in to comment.