Compare commits
2 commits
1b6d48d14b
...
62f5d8394b
| Author | SHA1 | Date | |
|---|---|---|---|
| 62f5d8394b | |||
| 682b141356 |
3 changed files with 116 additions and 7 deletions
|
|
@ -25,6 +25,18 @@ resize :: inline (arr: *[..]$T, new_size: int) {
|
||||||
basic.array_reserve(arr, new_size,, allocator = arr.allocator);
|
basic.array_reserve(arr, new_size,, allocator = arr.allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
remove_ordered :: inline (arr: *[..]$T, index: int, loc := #caller_location) #no_abc {
|
||||||
|
meta.check_bounds(index, arr.count, loc = loc);
|
||||||
|
memcpy(arr.data + index, arr.data + index + 1, (arr.count - index - 1) * size_of(T));
|
||||||
|
arr.count -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
remove_unordered :: inline (arr: *[..]$T, index: int, loc := #caller_location) #no_abc {
|
||||||
|
meta.check_bounds(index, arr.count, loc = loc);
|
||||||
|
arr.data[index] = arr.data[arr.count - 1];
|
||||||
|
arr.count -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
reset :: inline (arr: *[..]$T) {
|
reset :: inline (arr: *[..]$T) {
|
||||||
arr.count = 0;
|
arr.count = 0;
|
||||||
}
|
}
|
||||||
|
|
@ -40,6 +52,51 @@ find_pointer :: (a: *[..]$T, $predicate: (T) -> bool) -> *T, bool, int {
|
||||||
}
|
}
|
||||||
|
|
||||||
#scope_file;
|
#scope_file;
|
||||||
|
|
||||||
mem :: #import "jc/memory";
|
mem :: #import "jc/memory";
|
||||||
|
meta :: #import "jc/meta";
|
||||||
basic :: #import "Basic"; // @future
|
basic :: #import "Basic"; // @future
|
||||||
|
|
||||||
|
#if RUN_TESTS #run {
|
||||||
|
test.run("remove_ordered", t => {
|
||||||
|
a: [..]int;
|
||||||
|
append(*a, 10, 20, 30);
|
||||||
|
|
||||||
|
remove_ordered(*a, 1);
|
||||||
|
test.expect(t, a.count == 2);
|
||||||
|
test.expect(t, a.data[0] == 10);
|
||||||
|
test.expect(t, a.data[1] == 30);
|
||||||
|
|
||||||
|
remove_ordered(*a, 0);
|
||||||
|
test.expect(t, a.count == 1);
|
||||||
|
test.expect(t, a.data[0] == 30);
|
||||||
|
|
||||||
|
remove_ordered(*a, 0);
|
||||||
|
test.expect(t, a.count == 0);
|
||||||
|
|
||||||
|
append(*a, 10);
|
||||||
|
test.expect(t, a.count == 1);
|
||||||
|
test.expect(t, a.data[0] == 10);
|
||||||
|
});
|
||||||
|
|
||||||
|
test.run("remove_unordered", t => {
|
||||||
|
a: [..]int;
|
||||||
|
append(*a, 10, 20, 30);
|
||||||
|
|
||||||
|
remove_unordered(*a, 1);
|
||||||
|
test.expect(t, a.count == 2);
|
||||||
|
test.expect(t, a.data[0] == 10);
|
||||||
|
test.expect(t, a.data[1] == 30);
|
||||||
|
|
||||||
|
remove_unordered(*a, 0);
|
||||||
|
test.expect(t, a.count == 1);
|
||||||
|
test.expect(t, a.data[0] == 30);
|
||||||
|
|
||||||
|
remove_unordered(*a, 0);
|
||||||
|
test.expect(t, a.count == 0);
|
||||||
|
|
||||||
|
append(*a, 10);
|
||||||
|
test.expect(t, a.count == 1);
|
||||||
|
test.expect(t, a.data[0] == 10);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,18 @@ append :: inline (a: *Static_Array, items: ..a.T) -> *a.T #no_abc {
|
||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
remove_ordered :: inline (a: *Static_Array, index: int, loc := #caller_location) #no_abc {
|
||||||
|
meta.check_bounds(index, a.count, loc = loc);
|
||||||
|
memcpy(a.items.data + index, a.items.data + index + 1, (a.count - index - 1) * size_of(a.T));
|
||||||
|
a.count -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
remove_unordered :: inline (a: *Static_Array, index: int, loc := #caller_location) #no_abc {
|
||||||
|
meta.check_bounds(index, a.count, loc = loc);
|
||||||
|
a.items[index] = a.items[a.count - 1];
|
||||||
|
a.count -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
reset :: inline (a: *Static_Array) #no_abc {
|
reset :: inline (a: *Static_Array) #no_abc {
|
||||||
for 0..a.count - 1 a.items[it] = a.Default;
|
for 0..a.count - 1 a.items[it] = a.Default;
|
||||||
a.count = 0;
|
a.count = 0;
|
||||||
|
|
@ -114,4 +126,44 @@ basic :: #import "Basic"; // @future
|
||||||
_, ok = find_pointer(*a, v => v == 20);
|
_, ok = find_pointer(*a, v => v == 20);
|
||||||
test.expect(t, ok);
|
test.expect(t, ok);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test.run("remove_ordered", (t) => {
|
||||||
|
a: Static_Array(10, int);
|
||||||
|
append(*a, 10, 20, 30);
|
||||||
|
|
||||||
|
remove_ordered(*a, 1);
|
||||||
|
test.expect(t, a.count == 2);
|
||||||
|
test.expect(t, a.items[0] == 10);
|
||||||
|
|
||||||
|
remove_ordered(*a, 0);
|
||||||
|
test.expect(t, a.count == 1);
|
||||||
|
test.expect(t, a.items[0] == 30);
|
||||||
|
|
||||||
|
remove_ordered(*a, 0);
|
||||||
|
test.expect(t, a.count == 0);
|
||||||
|
|
||||||
|
append(*a, 10);
|
||||||
|
test.expect(t, a.count == 1);
|
||||||
|
test.expect(t, a.items[0] == 10);
|
||||||
|
});
|
||||||
|
|
||||||
|
test.run("remove_unordered", (t) => {
|
||||||
|
a: Static_Array(10, int);
|
||||||
|
append(*a, 10, 20, 30);
|
||||||
|
|
||||||
|
remove_unordered(*a, 1);
|
||||||
|
test.expect(t, a.count == 2);
|
||||||
|
test.expect(t, a.items[1] == 30);
|
||||||
|
|
||||||
|
remove_unordered(*a, 0);
|
||||||
|
test.expect(t, a.count == 1);
|
||||||
|
test.expect(t, a.items[0] == 30);
|
||||||
|
|
||||||
|
remove_unordered(*a, 0);
|
||||||
|
test.expect(t, a.count == 0);
|
||||||
|
|
||||||
|
append(*a, 10);
|
||||||
|
test.expect(t, a.count == 1);
|
||||||
|
test.expect(t, a.items[0] == 10);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,8 +45,8 @@ set :: (kv: *Kv, key: kv.Key, value: kv.Value) {
|
||||||
slot.value = value;
|
slot.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// @note(judah): we use 'evict' instead of 'remove' because it's a keyword...
|
// @note(judah): we use 'delete' instead of 'remove' because it's a keyword...
|
||||||
evict :: (kv: *Kv, key: kv.Key) -> kv.Value, bool {
|
delete :: (kv: *Kv, key: kv.Key) -> kv.Value, bool {
|
||||||
slot, ok, idx := find_slot(kv, kv.hash_proc(key));
|
slot, ok, idx := find_slot(kv, kv.hash_proc(key));
|
||||||
if !ok return mem.zero_of(kv.Value), false;
|
if !ok return mem.zero_of(kv.Value), false;
|
||||||
|
|
||||||
|
|
@ -145,7 +145,7 @@ hash :: #import "jc/hash";
|
||||||
}
|
}
|
||||||
|
|
||||||
for 0..ITERATIONS if it % 2 == 0 {
|
for 0..ITERATIONS if it % 2 == 0 {
|
||||||
_, ok := evict(*values, it);
|
_, ok := delete(*values, it);
|
||||||
test.expect(t, ok);
|
test.expect(t, ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -164,12 +164,12 @@ hash :: #import "jc/hash";
|
||||||
test.expect(t, values.count == 3);
|
test.expect(t, values.count == 3);
|
||||||
test.expect(t, values.slots.allocated == values.number_of_items_to_allocate_initially);
|
test.expect(t, values.slots.allocated == values.number_of_items_to_allocate_initially);
|
||||||
|
|
||||||
// evicting something that doesn't exist should do nothing
|
// deleting something that doesn't exist should do nothing
|
||||||
_, ok := evict(*values, 0);
|
_, ok := delete(*values, 0);
|
||||||
test.expect(t, !ok);
|
test.expect(t, !ok);
|
||||||
test.expect(t, values.count == 3);
|
test.expect(t, values.count == 3);
|
||||||
|
|
||||||
evict(*values, 2);
|
delete(*values, 2);
|
||||||
test.expect(t, values.count == 2);
|
test.expect(t, values.count == 2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue