mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Reduce memory consumtion. Add "range"
This commit is contained in:
26
lib/std/collections/range.c3
Normal file
26
lib/std/collections/range.c3
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* @checked Type{} < Type{} : "The type must be comparable"
|
||||
* @checked Type{} + (Type)1 : "The type must be possible to add to"
|
||||
**/
|
||||
module std::collections::range<Type>;
|
||||
|
||||
struct Range
|
||||
{
|
||||
Type start;
|
||||
Type end;
|
||||
}
|
||||
|
||||
fn usz Range.len(Range* range) @operator(len)
|
||||
{
|
||||
if (range.end < range.start) return 0;
|
||||
return (usz)(range.end - range.start);
|
||||
}
|
||||
|
||||
/**
|
||||
* @require index < range.len() : "Can't index into an empty range"
|
||||
**/
|
||||
fn Type Range.get(Range* range, usz index) @operator([])
|
||||
{
|
||||
return range.start + (Type)index;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user