Compare commits

..

2 commits

Author SHA1 Message Date
Judah Caruso
1b6d48d14b finish 073 2025-05-27 19:36:43 -06:00
Judah Caruso
796def9236 add find/find_pointer to array types 2025-05-27 19:35:14 -06:00
5 changed files with 53 additions and 2 deletions

2
TODO
View file

@ -2,7 +2,6 @@
[Judah] [Judah]
017 [x] re-implement dynamic arrays (should mirror procedures on 'Static_Array') 017 [x] re-implement dynamic arrays (should mirror procedures on 'Static_Array')
073 [array] standardize api across types (also add 'find' and 'find_pointer')
[Jesse] [Jesse]
011 [math] add more Vec math procedures 011 [math] add more Vec math procedures
@ -84,3 +83,4 @@
034 [x] can we add location info to Allocator_Proc? 034 [x] can we add location info to Allocator_Proc?
012 [map] create a simple arena-backed hash map implementation 'Map(K, V)', should be able to hash a key of any type (must include: get, set, remove, for_expansion) - possibly blocked by 032 012 [map] create a simple arena-backed hash map implementation 'Map(K, V)', should be able to hash a key of any type (must include: get, set, remove, for_expansion) - possibly blocked by 032
065 [array] add dynamic, but stable array implementation (values should not move in memory once appended to the array; should mirror procedures on 'Static_Array') 065 [array] add dynamic, but stable array implementation (values should not move in memory once appended to the array; should mirror procedures on 'Static_Array')
073 [array] standardize api across types (also add 'find' and 'find_pointer')

View file

@ -29,6 +29,15 @@ reset :: inline (arr: *[..]$T) {
arr.count = 0; 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; #scope_file;

View file

@ -51,6 +51,16 @@ reset :: (a: *Stable_Array) {
a.chunks.count = 0; 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 { operator [] :: (a: Stable_Array, index: int, loc := #caller_location) -> a.T #no_abc {
cidx := index / a.items_per_chunk; cidx := index / a.items_per_chunk;
iidx := 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 it.* = 1;
for a test.expect(t, 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 => { test.run("stability", t => {

View file

@ -33,6 +33,16 @@ reset :: inline (a: *Static_Array) #no_abc {
a.count = 0; 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 { operator [] :: inline (a: Static_Array, $$index: int, loc := #caller_location) -> a.T #no_abc {
meta.check_bounds(index, a.count, loc = loc); meta.check_bounds(index, a.count, loc = loc);
return a.items[index]; return a.items[index];
@ -97,5 +107,11 @@ basic :: #import "Basic"; // @future
append(*a, 10, 20, 30); append(*a, 10, 20, 30);
test.expect(t, a.count == 3, "count: %", a.count); 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);
}); });
} }

View file

@ -18,8 +18,13 @@ default_of :: ($T: Type) -> T #expand {
return default; return default;
} }
undefined_of :: ($T: Type) -> T #expand {
uninit: T = ---;
return uninit;
}
zero_of :: ($T: Type) -> T #expand { zero_of :: ($T: Type) -> T #expand {
zero: T = ---; zero := undefined_of(T);
memset(*zero, 0, size_of(T)); memset(*zero, 0, size_of(T));
return zero; return zero;
} }