Add range_cache.cpp

This commit is contained in:
gingerBill
2019-10-26 14:29:04 +01:00
parent 1734eb949f
commit d808f9eccf
3 changed files with 88 additions and 3 deletions
-1
View File
@@ -39,7 +39,6 @@ set linker_settings=%libs% %linker_flags%
del *.pdb > NUL 2> NUL
del *.ilk > NUL 2> NUL
cl %compiler_settings% "src\main.cpp" ^
/link %linker_settings% -OUT:%exe_name% ^
&& odin run examples/demo/demo.odin
+18 -2
View File
@@ -50,9 +50,9 @@ extra_general_stuff :: proc() {
i := i32(137);
ptr := &i;
_ = (^f32)(ptr);
_ = (^f32)(ptr); // Call-based syntax
// ^f32(ptr) == ^(f32(ptr))
_ = cast(^f32)ptr;
_ = cast(^f32)ptr; // Operator-based syntax
_ = (^f32)(ptr)^;
_ = (cast(^f32)ptr)^;
@@ -1188,6 +1188,21 @@ where_clauses :: proc() {
}
}
ranged_fields_for_array_compound_literals :: proc() {
fmt.println("\n#ranged fields for array compound literals");
{ // Normal Array Literal
foo := [?]int{1, 4, 9, 16};
fmt.println(foo);
}
i := 2;
foo := [?]int {
0 = 123,
5..9 = 54,
10..<16 = i*3 + (i-1)*2,
};
fmt.println(foo); // [123, 0, 0, 0, 0, 54, 54, 54, 54, 54, 8, 8, 8, 8, 8]
}
main :: proc() {
when true {
extra_general_stuff();
@@ -1210,6 +1225,7 @@ main :: proc() {
quaternions();
inline_for_statement();
where_clauses();
ranged_fields_for_array_compound_literals();
}
}
+70
View File
@@ -0,0 +1,70 @@
// Integers only
struct RangeValue {
i64 lo;
i64 hi;
};
struct RangeCache {
Array<RangeValue> ranges;
};
RangeCache range_cache_make(gbAllocator a) {
RangeCache cache = {};
array_init(&cache.ranges, a);
return cache;
}
void range_cache_destroy(RangeCache *c) {
array_free(&c->ranges);
}
bool range_cache_add_index(RangeCache *c, i64 index) {
for_array(i, c->ranges) {
RangeValue v = c->ranges[i];
if (v.lo <= index && index <= v.hi) {
return false;
}
}
RangeValue v = {index, index};
array_add(&c->ranges, v);
return true;
}
bool range_cache_add_range(RangeCache *c, i64 lo, i64 hi) {
GB_ASSERT(lo <= hi);
for_array(i, c->ranges) {
RangeValue v = c->ranges[i];
if (hi < v.lo) {
continue;
}
if (lo > v.hi) {
continue;
}
if (v.hi < hi) {
v.hi = hi;
}
if (lo < v.lo) {
v.lo = lo;
}
c->ranges[i] = v;
return false;
}
RangeValue v = {lo, hi};
array_add(&c->ranges, v);
return true;
}
bool range_cache_index_exists(RangeCache *c, i64 index) {
for_array(i, c->ranges) {
RangeValue v = c->ranges[i];
if (v.lo <= index && index <= v.hi) {
return true;
}
}
return false;
}