mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
* std/lib: simplify String.{,r}index_of and improve speed for the index_of one
Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
* lib/std/collections: add EnumMap.get_ref
Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
---------
Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
51 lines
984 B
C
51 lines
984 B
C
module std::collections::enummap(<Enum, ValueType>);
|
|
|
|
struct EnumMap
|
|
{
|
|
ValueType[Enum.len] values;
|
|
}
|
|
|
|
fn void EnumMap.init(&self, ValueType init_value)
|
|
{
|
|
foreach (&a : self.values)
|
|
{
|
|
*a = init_value;
|
|
}
|
|
}
|
|
|
|
|
|
fn void! EnumMap.to_format(&self, Formatter* formatter) @dynamic
|
|
{
|
|
formatter.print("{ ")!;
|
|
foreach (i, &value : self.values)
|
|
{
|
|
if (i != 0) formatter.print(", ")!;
|
|
formatter.printf("%s: %s", (Enum)i, *value)!;
|
|
}
|
|
formatter.print(" }")!;
|
|
}
|
|
|
|
fn String EnumMap.to_string(&self, Allocator* using = mem::heap()) @dynamic
|
|
{
|
|
return string::printf("%s", *self);
|
|
}
|
|
|
|
fn usz EnumMap.len(&self) @operator(len) @inline
|
|
{
|
|
return self.values.len;
|
|
}
|
|
|
|
fn ValueType EnumMap.get(&self, Enum key) @operator([]) @inline
|
|
{
|
|
return self.values[key.ordinal];
|
|
}
|
|
|
|
fn ValueType* EnumMap.get_ref(&self, Enum key) @operator(&[]) @inline
|
|
{
|
|
return &self.values[key.ordinal];
|
|
}
|
|
|
|
fn void EnumMap.set(&self, Enum key, ValueType value) @operator([]=) @inline
|
|
{
|
|
self.values[key.ordinal] = value;
|
|
} |