add find/find_pointer to array types
This commit is contained in:
parent
5f7b1107d1
commit
796def9236
4 changed files with 52 additions and 1 deletions
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 => {
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue