diff --git a/array/dynamic_array.jai b/array/dynamic_array.jai index 12a812f..12bb9da 100644 --- a/array/dynamic_array.jai +++ b/array/dynamic_array.jai @@ -29,6 +29,15 @@ reset :: inline (arr: *[..]$T) { arr.count = 0; } +find :: (a: [..]$T, $predicate: (T) -> bool) -> T, bool, int { + for a if inline predicate(it) return it, true, it_index; + return mem.undefined_of(T), false, -1; +} + +find_pointer :: (a: *[..]$T, $predicate: (T) -> bool) -> *T, bool, int { + for * a if inline predicate(it.*) return it, true, it_index; + return null, false, -1; +} #scope_file; diff --git a/array/stable_array.jai b/array/stable_array.jai index ee772d6..cf0306d 100644 --- a/array/stable_array.jai +++ b/array/stable_array.jai @@ -51,6 +51,16 @@ reset :: (a: *Stable_Array) { a.chunks.count = 0; } +find :: (a: Stable_Array, $predicate: (a.T) -> bool) -> a.T, bool, int { + for a if inline predicate(it) return it, true, it_index; + return mem.undefined_of(a.T), false, -1; +} + +find_pointer :: (a: *Stable_Array, $predicate: (a.T) -> bool) -> *a.T, bool, int { + for * a if inline predicate(it.*) return it, true, it_index; + return null, false, -1; +} + operator [] :: (a: Stable_Array, index: int, loc := #caller_location) -> a.T #no_abc { cidx := index / a.items_per_chunk; iidx := index % a.items_per_chunk; @@ -164,6 +174,17 @@ try_lazy_init :: (a: *Stable_Array) { for * a it.* = 1; for a test.expect(t, it == 1); + + ptr, ok, idx := find_pointer(*a, v => v == 1); + test.expect(t, ok); + test.expect(t, idx == 0); + test.expect(t, ptr == *a[0]); + + a[a.count - 1] = -1; + + _, ok, idx = find(a, v => v == -1); + test.expect(t, ok); + test.expect(t, idx == a.count - 1); }); test.run("stability", t => { diff --git a/array/static_array.jai b/array/static_array.jai index 117589b..4c241f3 100644 --- a/array/static_array.jai +++ b/array/static_array.jai @@ -33,6 +33,16 @@ reset :: inline (a: *Static_Array) #no_abc { a.count = 0; } +find :: (a: Static_Array, $predicate: (a.T) -> bool) -> a.T, bool, int { + for a if inline predicate(it) return it, true, it_index; + return mem.undefined_of(a.T), false, -1; +} + +find_pointer :: (a: *Static_Array, $predicate: (a.T) -> bool) -> *a.T, bool, int { + for * a if inline predicate(it.*) return it, true, it_index; + return null, false, -1; +} + operator [] :: inline (a: Static_Array, $$index: int, loc := #caller_location) -> a.T #no_abc { meta.check_bounds(index, a.count, loc = loc); return a.items[index]; @@ -97,5 +107,11 @@ basic :: #import "Basic"; // @future append(*a, 10, 20, 30); test.expect(t, a.count == 3, "count: %", a.count); + + _, ok := find(a, v => v == 10); + test.expect(t, ok); + + _, ok = find_pointer(*a, v => v == 20); + test.expect(t, ok); }); } diff --git a/memory/module.jai b/memory/module.jai index a488f0d..f3af797 100644 --- a/memory/module.jai +++ b/memory/module.jai @@ -18,8 +18,13 @@ default_of :: ($T: Type) -> T #expand { return default; } +undefined_of :: ($T: Type) -> T #expand { + uninit: T = ---; + return uninit; +} + zero_of :: ($T: Type) -> T #expand { - zero: T = ---; + zero := undefined_of(T); memset(*zero, 0, size_of(T)); return zero; }