Compare commits
2 commits
5f7b1107d1
...
1b6d48d14b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1b6d48d14b | ||
|
|
796def9236 |
5 changed files with 53 additions and 2 deletions
2
TODO
2
TODO
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
[Judah]
|
||||
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]
|
||||
011 [math] add more Vec math procedures
|
||||
|
|
@ -84,3 +83,4 @@
|
|||
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
|
||||
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')
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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 => {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue