mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-07 08:08:50 +00:00
Add range_cache.cpp
This commit is contained in:
@@ -39,7 +39,6 @@ set linker_settings=%libs% %linker_flags%
|
|||||||
del *.pdb > NUL 2> NUL
|
del *.pdb > NUL 2> NUL
|
||||||
del *.ilk > NUL 2> NUL
|
del *.ilk > NUL 2> NUL
|
||||||
|
|
||||||
|
|
||||||
cl %compiler_settings% "src\main.cpp" ^
|
cl %compiler_settings% "src\main.cpp" ^
|
||||||
/link %linker_settings% -OUT:%exe_name% ^
|
/link %linker_settings% -OUT:%exe_name% ^
|
||||||
&& odin run examples/demo/demo.odin
|
&& odin run examples/demo/demo.odin
|
||||||
|
|||||||
+18
-2
@@ -50,9 +50,9 @@ extra_general_stuff :: proc() {
|
|||||||
i := i32(137);
|
i := i32(137);
|
||||||
ptr := &i;
|
ptr := &i;
|
||||||
|
|
||||||
_ = (^f32)(ptr);
|
_ = (^f32)(ptr); // Call-based syntax
|
||||||
// ^f32(ptr) == ^(f32(ptr))
|
// ^f32(ptr) == ^(f32(ptr))
|
||||||
_ = cast(^f32)ptr;
|
_ = cast(^f32)ptr; // Operator-based syntax
|
||||||
|
|
||||||
_ = (^f32)(ptr)^;
|
_ = (^f32)(ptr)^;
|
||||||
_ = (cast(^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() {
|
main :: proc() {
|
||||||
when true {
|
when true {
|
||||||
extra_general_stuff();
|
extra_general_stuff();
|
||||||
@@ -1210,6 +1225,7 @@ main :: proc() {
|
|||||||
quaternions();
|
quaternions();
|
||||||
inline_for_statement();
|
inline_for_statement();
|
||||||
where_clauses();
|
where_clauses();
|
||||||
|
ranged_fields_for_array_compound_literals();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user