summaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-31 01:15:25 +0000
committerZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-31 01:15:25 +0000
commit856c9fe56b412779e045ef86a767b93d5c7f563b (patch)
tree6f851bb9bf300970c4a0d7186db0de86e510a18d /std
parent03a6a57f500ee4230ce2cee887866b3850ed7ed9 (diff)
Improvements for slice + better iteration for arrays
Diffstat (limited to 'std')
-rw-r--r--std/slice.zc35
1 files changed, 35 insertions, 0 deletions
diff --git a/std/slice.zc b/std/slice.zc
index 778c6ed..7ace396 100644
--- a/std/slice.zc
+++ b/std/slice.zc
@@ -1,10 +1,45 @@
+import "./option.zc"
+
struct Slice<T> {
data: T*;
len: usize;
}
+struct SliceIter<T> {
+ data: T*;
+ count: usize;
+ idx: usize;
+}
+
+impl SliceIter<T> {
+ fn next(self) -> Option<T> {
+ if (self.idx < self.count) {
+ let item = self.data[self.idx];
+ self.idx = self.idx + 1;
+ return Option<T>::Some(item);
+ }
+ return Option<T>::None();
+ }
+
+ fn iterator(self) -> SliceIter<T> {
+ return *self;
+ }
+}
+
impl Slice<T> {
+ fn from_array(arr: T*, len: usize) -> Slice<T> {
+ return Slice<T> { data: arr, len: len };
+ }
+
+ fn iterator(self) -> SliceIter<T> {
+ return SliceIter<T> {
+ data: self.data,
+ count: self.len,
+ idx: 0
+ };
+ }
+
fn length(self) -> usize {
return self.len;
}