summaryrefslogtreecommitdiff
path: root/std/slice.zc
diff options
context:
space:
mode:
Diffstat (limited to 'std/slice.zc')
-rw-r--r--std/slice.zc40
1 files changed, 40 insertions, 0 deletions
diff --git a/std/slice.zc b/std/slice.zc
index 778c6ed..c757fbd 100644
--- a/std/slice.zc
+++ b/std/slice.zc
@@ -1,10 +1,50 @@
+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(ptr: T*, len: usize) -> Slice<T> {
+ return Slice<T> { data: ptr, len: len };
+ }
+
+ // Alias for backwards compatibility with std/mem.zc
+ fn new(data: T*, len: usize) -> Slice<T> {
+ return Slice<T> { data: data, 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;
}