summaryrefslogtreecommitdiff
path: root/std/vec.zc
diff options
context:
space:
mode:
authorZuhaitz <zuhaitz.zechhub@gmail.com>2026-01-21 19:14:02 +0000
committerGitHub <noreply@github.com>2026-01-21 19:14:02 +0000
commita552bd6b8175fe833e8a77dfb051bf3481df6851 (patch)
treefa8262808bf4ecb589f24f87d2ba6d7f5b8a36b2 /std/vec.zc
parent9e47e8e2d7c7c589c3c158cd5ef3069289709fa8 (diff)
parenta58dd232dc72f20971707c99dfa6266133f70a20 (diff)
Merge pull request #84 from davidscholberg/vec-append
added append function to vec
Diffstat (limited to 'std/vec.zc')
-rw-r--r--std/vec.zc21
1 files changed, 21 insertions, 0 deletions
diff --git a/std/vec.zc b/std/vec.zc
index 2205cca..d310535 100644
--- a/std/vec.zc
+++ b/std/vec.zc
@@ -90,6 +90,19 @@ impl Vec<T> {
self.data = (T*)realloc(self.data, self.cap * sizeof(T));
}
+ fn grow_to_fit(self, new_len: usize) {
+ if self.cap >= new_len {
+ return;
+ }
+
+ if (self.cap == 0) { self.cap = 8; }
+ while self.cap < new_len {
+ self.cap = self.cap * 2;
+ }
+
+ self.data = (T*)realloc(self.data, self.cap * sizeof(T));
+ }
+
fn iterator(self) -> VecIter<T> {
return VecIter<T> {
data: self.data,
@@ -161,6 +174,14 @@ impl Vec<T> {
return item;
}
+ fn append(self, other: Vec<T>) {
+ var new_len = self.len + other.len;
+ self.grow_to_fit(new_len);
+
+ memcpy(self.data + self.len, other.data, other.len * sizeof(T));
+ self.len = new_len;
+ }
+
fn get(self, idx: usize) -> T {
if (idx >= self.len) {
!"Panic: Index out of bounds";