"@ensure" now correctly only runs on non-optional results. Subtypes now merge to a single type. Beginning deprecation of "std::core::str". Refreshed String functions. Consistent use of ".using" parameter. Functions moved to string methods. Tests on more string methods. Fixes to split, rindex_of.

This commit is contained in:
Christoffer Lerno
2023-03-07 19:32:47 +01:00
committed by Christoffer Lerno
parent 33cc2d889b
commit 89de0a70d2
27 changed files with 410 additions and 245 deletions

View File

@@ -28,9 +28,9 @@ fn void LinkedList.push_last(LinkedList* list, Type value)
list.link_last(value);
}
fn void LinkedList.init(LinkedList* list, Allocator* alloc = mem::heap())
fn void LinkedList.init(LinkedList* list, Allocator* using = mem::heap())
{
*list = { .allocator = alloc };
*list = { .allocator = using };
}
fn void LinkedList.tinit(LinkedList* list) => list.init(mem::temp()) @inline;

View File

@@ -13,16 +13,16 @@ struct List
}
/**
* @require allocator != null "A valid allocator must be provided"
* @require using != null "A valid allocator must be provided"
**/
fn void List.init(List* list, usz initial_capacity = 16, Allocator* allocator = mem::heap())
fn void List.init(List* list, usz initial_capacity = 16, Allocator* using = mem::heap())
{
list.allocator = allocator;
list.allocator = using;
list.size = 0;
if (initial_capacity > 0)
{
initial_capacity = math::next_power_of_2(initial_capacity);
list.entries = malloc_aligned(Type, initial_capacity, .alignment = Type[1].alignof, .using = allocator)!!;
list.entries = malloc_aligned(Type, initial_capacity, .alignment = Type[1].alignof, .using = using)!!;
}
else
{

View File

@@ -20,15 +20,15 @@ struct HashMap
* @require load_factor > 0.0 "The load factor must be higher than 0"
* @require !map.allocator "Map was already initialized"
* @require capacity < MAXIMUM_CAPACITY "Capacity cannot exceed maximum"
* @require allocator != null "The allocator must be non-null"
* @require using != null "The allocator must be non-null"
**/
fn void HashMap.init(HashMap* map, uint capacity = DEFAULT_INITIAL_CAPACITY, float load_factor = DEFAULT_LOAD_FACTOR, Allocator* allocator = mem::heap())
fn void HashMap.init(HashMap* map, uint capacity = DEFAULT_INITIAL_CAPACITY, float load_factor = DEFAULT_LOAD_FACTOR, Allocator* using = mem::heap())
{
capacity = math::next_power_of_2(capacity);
map.allocator = allocator;
map.allocator = using;
map.load_factor = load_factor;
map.threshold = (uint)(capacity * load_factor);
map.table = calloc(Entry*, capacity, .using = allocator);
map.table = calloc(Entry*, capacity, .using = using);
}
/**
@@ -42,9 +42,9 @@ fn void HashMap.tinit(HashMap* map, uint capacity = DEFAULT_INITIAL_CAPACITY, fl
map.init(capacity, load_factor, mem::temp());
}
fn void HashMap.init_from_map(HashMap* map, HashMap* other_map, Allocator* allocator = mem::heap())
fn void HashMap.init_from_map(HashMap* map, HashMap* other_map, Allocator* using = mem::heap())
{
map.init(other_map.table.len, other_map.load_factor, allocator);
map.init(other_map.table.len, other_map.load_factor, using);
map.put_all_for_create(other_map);
}
@@ -164,11 +164,11 @@ fn Key[] HashMap.key_tlist(HashMap* map)
return map.key_list(mem::temp()) @inline;
}
fn Key[] HashMap.key_list(HashMap* map, Allocator* allocator = mem::heap())
fn Key[] HashMap.key_list(HashMap* map, Allocator* using = mem::heap())
{
if (!map.count) return Key[] {};
Key[] list = calloc(Key, map.count, .using = allocator);
Key[] list = calloc(Key, map.count, .using = using);
usz index = 0;
foreach (Entry* entry : map.table)
{
@@ -186,10 +186,10 @@ fn Value[] HashMap.value_tlist(HashMap* map)
return map.value_list(mem::temp()) @inline;
}
fn Value[] HashMap.value_list(HashMap* map, Allocator* allocator = mem::heap())
fn Value[] HashMap.value_list(HashMap* map, Allocator* using = mem::heap())
{
if (!map.count) return Value[] {};
Value[] list = calloc(Value, map.count, .using = allocator);
Value[] list = calloc(Value, map.count, .using = using);
usz index = 0;
foreach (Entry* entry : map.table)
{

View File

@@ -80,10 +80,10 @@ fn void! Object.to_format(Object* o, Formatter* formatter)
}
}
fn Object* new_obj(Allocator* allocator = mem::heap())
fn Object* new_obj(Allocator* using = mem::heap())
{
Object* o = malloc(Object, .using = allocator);
*o = { .allocator = allocator, .type = void.typeid };
Object* o = malloc(Object, .using = using);
*o = { .allocator = using, .type = void.typeid };
return o;
}
@@ -92,31 +92,31 @@ fn Object* new_null()
return &NULL_OBJECT;
}
fn Object* new_int(int128 i, Allocator* allocator = mem::heap())
fn Object* new_int(int128 i, Allocator* using = mem::heap())
{
Object* o = malloc(Object, .using = allocator);
*o = { .i = i, .allocator = allocator, .type = int128.typeid };
Object* o = malloc(Object, .using = using);
*o = { .i = i, .allocator = using, .type = int128.typeid };
return o;
}
macro Object* new_enum(e, Allocator* allocator = mem::heap())
macro Object* new_enum(e, Allocator* using = mem::heap())
{
Object* o = malloc(Object, .using = allocator);
*o = { .i = (int128)e, .allocator = allocator, .type = $typeof(e).typeid };
Object* o = malloc(Object, .using = using);
*o = { .i = (int128)e, .allocator = using, .type = $typeof(e).typeid };
return o;
}
fn Object* new_float(double f, Allocator* allocator = mem::current_allocator())
fn Object* new_float(double f, Allocator* using = mem::current_allocator())
{
Object* o = malloc(Object, .using = allocator);
*o = { .f = f, .allocator = allocator, .type = double.typeid };
Object* o = malloc(Object, .using = using);
*o = { .f = f, .allocator = using, .type = double.typeid };
return o;
}
fn Object* new_string(String s, Allocator* allocator = mem::heap())
fn Object* new_string(String s, Allocator* using = mem::heap())
{
Object* o = malloc(Object, .using = allocator);
*o = { .s = s.copyz(allocator), .allocator = allocator, .type = String.typeid };
Object* o = malloc(Object, .using = using);
*o = { .s = s.copy(using), .allocator = using, .type = String.typeid };
return o;
}
@@ -178,7 +178,7 @@ fn void Object.init_map_if_needed(Object* o) @private
if (o.is_empty())
{
o.type = ObjectInternalMap.typeid;
o.map.init(.allocator = o.allocator);
o.map.init(.using = o.allocator);
}
}
@@ -190,7 +190,7 @@ fn void Object.init_array_if_needed(Object* o) @private
if (o.is_empty())
{
o.type = ObjectInternalList.typeid;
o.array.init(.allocator = o.allocator);
o.array.init(.using = o.allocator);
}
}
@@ -206,7 +206,7 @@ fn void Object.set_object(Object* o, String key, Object* new_object) @private
(void)free(entry.key, .using = o.allocator);
entry.value.free();
}
o.map.set(str::copyz(key, o.map.allocator), new_object);
o.map.set(key.copy(o.map.allocator), new_object);
}
macro Object* object_from_value(value) @private