Skip to content

Commit

Permalink
0.5.4: Hash variables accept designated initializers. @safemacro over…
Browse files Browse the repository at this point in the history
…rides the need for `@` in macro names. Fixes to macro context evaluation. Updated allocator api. Removed install_win_reqs.bat.
  • Loading branch information
lerno committed Jan 20, 2024
1 parent deb4cc7 commit 104766e
Show file tree
Hide file tree
Showing 80 changed files with 982 additions and 899 deletions.
17 changes: 0 additions & 17 deletions install_win_reqs.bat

This file was deleted.

6 changes: 3 additions & 3 deletions lib/std/collections/bitset.c3
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ struct GrowableBitSet
* @param initial_capacity
* @param [&inout] allocator "The allocator to use, defaults to the heap allocator"
**/
fn GrowableBitSet* GrowableBitSet.new_init(&self, usz initial_capacity = 1, Allocator* allocator = mem::heap())
fn GrowableBitSet* GrowableBitSet.new_init(&self, usz initial_capacity = 1, Allocator* allocator = allocator::heap())
{
self.data.new_init(initial_capacity, allocator);
return self;
Expand All @@ -96,14 +96,14 @@ fn GrowableBitSet* GrowableBitSet.new_init(&self, usz initial_capacity = 1, Allo
* @param initial_capacity
* @param [&inout] allocator "The allocator to use, defaults to the heap allocator"
**/
fn GrowableBitSet* GrowableBitSet.init_new(&self, usz initial_capacity = 1, Allocator* allocator = mem::heap()) @deprecated("Replaced by new_init")
fn GrowableBitSet* GrowableBitSet.init_new(&self, usz initial_capacity = 1, Allocator* allocator = allocator::heap()) @deprecated("Replaced by new_init")
{
return self.new_init(initial_capacity, allocator) @inline;
}

fn GrowableBitSet* GrowableBitSet.temp_init(&self, usz initial_capacity = 1)
{
return self.new_init(initial_capacity, mem::temp()) @inline;
return self.new_init(initial_capacity, allocator::temp()) @inline;
}

fn GrowableBitSet* GrowableBitSet.init_temp(&self, usz initial_capacity = 1) @deprecated("Replaced by temp_init")
Expand Down
2 changes: 1 addition & 1 deletion lib/std/collections/enummap.c3
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn usz! EnumMap.to_format(&self, Formatter* formatter) @dynamic
return n;
}

fn String EnumMap.to_new_string(&self, Allocator* allocator = mem::heap()) @dynamic
fn String EnumMap.to_new_string(&self, Allocator* allocator = allocator::heap()) @dynamic
{
return string::new_format("%s", *self, .allocator = allocator);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/std/collections/enumset.c3
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fn usz! EnumSet.to_format(&set, Formatter* formatter) @dynamic
return n;
}

fn String EnumSet.to_new_string(&set, Allocator* allocator = mem::heap()) @dynamic
fn String EnumSet.to_new_string(&set, Allocator* allocator = allocator::heap()) @dynamic
{
return string::new_format("%s", *set, .allocator = allocator);
}
Expand Down
13 changes: 7 additions & 6 deletions lib/std/collections/linkedlist.c3
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn void LinkedList.push_last(&self, Type value)
* @param [&inout] allocator "The allocator to use, defaults to the heap allocator"
* @return "the initialized list"
**/
fn LinkedList* LinkedList.new_init(&self, Allocator* allocator = mem::heap())
fn LinkedList* LinkedList.new_init(&self, Allocator* allocator = allocator::heap())
{
*self = { .allocator = allocator };
return self;
Expand All @@ -42,14 +42,14 @@ fn LinkedList* LinkedList.new_init(&self, Allocator* allocator = mem::heap())
* @param [&inout] allocator "The allocator to use, defaults to the heap allocator"
* @return "the initialized list"
**/
fn LinkedList* LinkedList.init_new(&self, Allocator* allocator = mem::heap()) @deprecated("Replaced by new_init")
fn LinkedList* LinkedList.init_new(&self, Allocator* allocator = allocator::heap()) @deprecated("Replaced by new_init")
{
return self.new_init(allocator);
}

fn LinkedList* LinkedList.temp_init(&self)
{
return self.new_init(mem::temp()) @inline;
return self.new_init(allocator::temp()) @inline;
}

fn LinkedList* LinkedList.init_temp(&self) @deprecated("Replaced by temp_init")
Expand All @@ -62,12 +62,13 @@ fn LinkedList* LinkedList.init_temp(&self) @deprecated("Replaced by temp_init")
**/
macro void LinkedList.free_node(&self, Node* node) @private
{
self.allocator.free(node);
allocator::free(self.allocator, node);
}

macro Node* LinkedList.alloc_node(&self) @private
{
if (!self.allocator) self.allocator = mem::heap();
return self.allocator.new(Node);
if (!self.allocator) self.allocator = allocator::heap();
return allocator::alloc(self.allocator, Node);
}

fn void LinkedList.link_first(&self, Type value) @private
Expand Down
24 changes: 12 additions & 12 deletions lib/std/collections/list.c3
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ struct List (Printable)
* @param initial_capacity "The initial capacity to reserve"
* @param [&inout] allocator "The allocator to use, defaults to the heap allocator"
**/
fn List* List.new_init(&self, usz initial_capacity = 16, Allocator* allocator = mem::heap())
fn List* List.new_init(&self, usz initial_capacity = 16, Allocator* allocator = allocator::heap())
{
self.allocator = allocator;
self.size = 0;
if (initial_capacity > 0)
{
initial_capacity = math::next_power_of_2(initial_capacity);
self.entries = allocator.alloc_aligned(Type.sizeof * initial_capacity, .alignment = Type[1].alignof)!!;
self.entries = allocator::malloc_aligned(allocator, Type.sizeof * initial_capacity, .alignment = Type[1].alignof)!!;
}
else
{
Expand All @@ -44,7 +44,7 @@ fn List* List.new_init(&self, usz initial_capacity = 16, Allocator* allocator =
* @param initial_capacity "The initial capacity to reserve"
* @param [&inout] allocator "The allocator to use, defaults to the heap allocator"
**/
fn List* List.init_new(&self, usz initial_capacity = 16, Allocator* allocator = mem::heap()) @deprecated("Replaced by new_init")
fn List* List.init_new(&self, usz initial_capacity = 16, Allocator* allocator = allocator::heap()) @deprecated("Replaced by new_init")
{
return self.new_init(initial_capacity, allocator) @inline;
}
Expand All @@ -56,7 +56,7 @@ fn List* List.init_new(&self, usz initial_capacity = 16, Allocator* allocator =
**/
fn List* List.temp_init(&self, usz initial_capacity = 16)
{
return self.new_init(initial_capacity, mem::temp()) @inline;
return self.new_init(initial_capacity, allocator::temp()) @inline;
}

/**
Expand All @@ -72,7 +72,7 @@ fn List* List.init_temp(&self, usz initial_capacity = 16) @deprecated("Replaced
/**
* @require self.size == 0 "The List must be empty"
**/
fn void List.init_wrapping_array(&self, Type[] types, Allocator* allocator = mem::heap())
fn void List.init_wrapping_array(&self, Type[] types, Allocator* allocator = allocator::heap())
{
self.allocator = allocator;
self.size = types.len;
Expand Down Expand Up @@ -100,7 +100,7 @@ fn usz! List.to_format(&self, Formatter* formatter) @dynamic
}
}

fn String List.to_new_string(&self, Allocator* allocator = mem::heap()) @dynamic
fn String List.to_new_string(&self, Allocator* allocator = allocator::heap()) @dynamic
{
return string::new_format("%s", *self, .allocator = allocator);
}
Expand Down Expand Up @@ -164,17 +164,17 @@ fn void List.add_all(&self, List* other_list)
}


fn Type[] List.to_new_array(&self, Allocator* allocator = mem::heap())
fn Type[] List.to_new_array(&self, Allocator* allocator = allocator::heap())
{
if (!self.size) return Type[] {};
Type[] result = allocator.new_array(Type, self.size);
Type[] result = allocator::alloc_array(allocator, Type, self.size);
result[..] = self.entries[:self.size];
return result;
}

fn Type[] List.to_tarray(&self)
{
return self.to_new_array(mem::temp());
return self.to_new_array(allocator::temp());
}

/**
Expand Down Expand Up @@ -277,7 +277,7 @@ fn Type List.get(&self, usz index) @inline
fn void List.free(&self)
{
if (!self.allocator) return;
self.allocator.free_aligned(self.entries);
allocator::free_aligned(self.allocator, self.entries);
self.capacity = 0;
self.size = 0;
self.entries = null;
Expand Down Expand Up @@ -373,9 +373,9 @@ fn void List.reserve(&self, usz min_capacity)
{
if (!min_capacity) return;
if (self.capacity >= min_capacity) return;
if (!self.allocator) self.allocator = mem::heap();
if (!self.allocator) self.allocator = allocator::heap();
min_capacity = math::next_power_of_2(min_capacity);
self.entries = self.allocator.realloc_aligned(self.entries, Type.sizeof * min_capacity, .alignment = Type[1].alignof) ?? null;
self.entries = allocator::realloc_aligned(self.allocator, self.entries, Type.sizeof * min_capacity, .alignment = Type[1].alignof) ?? null;
self.capacity = min_capacity;
}

Expand Down
39 changes: 19 additions & 20 deletions lib/std/collections/map.c3
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ struct HashMap
* @require !self.allocator "Map was already initialized"
* @require capacity < MAXIMUM_CAPACITY "Capacity cannot exceed maximum"
**/
fn HashMap* HashMap.new_init(&self, uint capacity = DEFAULT_INITIAL_CAPACITY, float load_factor = DEFAULT_LOAD_FACTOR, Allocator* allocator = mem::heap())
fn HashMap* HashMap.new_init(&self, uint capacity = DEFAULT_INITIAL_CAPACITY, float load_factor = DEFAULT_LOAD_FACTOR, Allocator* allocator = allocator::heap())
{
capacity = math::next_power_of_2(capacity);
self.allocator = allocator;
self.load_factor = load_factor;
self.threshold = (uint)(capacity * load_factor);
self.table = allocator.new_zero_array(Entry*, capacity);
self.table = allocator::new_array(allocator, Entry*, capacity);
return self;
}

Expand All @@ -43,7 +43,7 @@ fn HashMap* HashMap.new_init(&self, uint capacity = DEFAULT_INITIAL_CAPACITY, fl
* @require !map.allocator "Map was already initialized"
* @require capacity < MAXIMUM_CAPACITY "Capacity cannot exceed maximum"
**/
fn HashMap* HashMap.init_new(&map, uint capacity = DEFAULT_INITIAL_CAPACITY, float load_factor = DEFAULT_LOAD_FACTOR, Allocator* allocator = mem::heap()) @deprecated("Replaced by new_init")
fn HashMap* HashMap.init_new(&map, uint capacity = DEFAULT_INITIAL_CAPACITY, float load_factor = DEFAULT_LOAD_FACTOR, Allocator* allocator = allocator::heap()) @deprecated("Replaced by new_init")
{
return map.new_init(capacity, load_factor, allocator);
}
Expand All @@ -56,7 +56,7 @@ fn HashMap* HashMap.init_new(&map, uint capacity = DEFAULT_INITIAL_CAPACITY, flo
**/
fn HashMap* HashMap.temp_init(&self, uint capacity = DEFAULT_INITIAL_CAPACITY, float load_factor = DEFAULT_LOAD_FACTOR)
{
return self.new_init(capacity, load_factor, mem::temp()) @inline;
return self.new_init(capacity, load_factor, allocator::temp()) @inline;
}

/**
Expand Down Expand Up @@ -85,7 +85,7 @@ fn bool HashMap.is_initialized(&map)
* @param [&inout] allocator "The allocator to use"
* @param [&in] other_map "The map to copy from."
**/
fn HashMap* HashMap.new_init_from_map(&self, HashMap* other_map, Allocator* allocator = mem::heap())
fn HashMap* HashMap.new_init_from_map(&self, HashMap* other_map, Allocator* allocator = allocator::heap())
{
self.new_init(other_map.table.len, other_map.load_factor, allocator);
self.put_all_for_create(other_map);
Expand All @@ -96,7 +96,7 @@ fn HashMap* HashMap.new_init_from_map(&self, HashMap* other_map, Allocator* allo
* @param [&inout] allocator "The allocator to use"
* @param [&in] other_map "The map to copy from."
**/
fn HashMap* HashMap.init_new_from_map(&self, HashMap* other_map, Allocator* allocator = mem::heap()) @deprecated("Replaced by new_init_from_map")
fn HashMap* HashMap.init_new_from_map(&self, HashMap* other_map, Allocator* allocator = allocator::heap()) @deprecated("Replaced by new_init_from_map")

{
return self.new_init_from_map(other_map, allocator) @inline;
Expand All @@ -107,7 +107,7 @@ fn HashMap* HashMap.init_new_from_map(&self, HashMap* other_map, Allocator* allo
**/
fn HashMap* HashMap.temp_init_from_map(&map, HashMap* other_map)
{
return map.new_init_from_map(other_map, mem::temp()) @inline;
return map.new_init_from_map(other_map, allocator::temp()) @inline;
}

/**
Expand Down Expand Up @@ -153,6 +153,7 @@ fn Entry*! HashMap.get_entry(&map, Key key)

/**
* Get the value or update and
* @require $assignable(#expr, Value)
**/
macro Value HashMap.@get_or_set(&map, Key key, Value #expr)
{
Expand Down Expand Up @@ -239,14 +240,14 @@ fn void HashMap.free(&map)

fn Key[] HashMap.key_tlist(&map)
{
return map.key_new_list(mem::temp()) @inline;
return map.key_new_list(allocator::temp()) @inline;
}

fn Key[] HashMap.key_new_list(&map, Allocator* allocator = mem::heap())
fn Key[] HashMap.key_new_list(&map, Allocator* allocator = allocator::heap())
{
if (!map.count) return {};

Key[] list = allocator.new_array(Key, map.count);
Key[] list = allocator::alloc_array(allocator, Key, map.count);
usz index = 0;
foreach (Entry* entry : map.table)
{
Expand Down Expand Up @@ -283,13 +284,13 @@ macro HashMap.@each_entry(map; @body(entry))

fn Value[] HashMap.value_tlist(&map)
{
return map.value_new_list(mem::temp()) @inline;
return map.value_new_list(allocator::temp()) @inline;
}

fn Value[] HashMap.value_new_list(&map, Allocator* allocator = mem::heap())
fn Value[] HashMap.value_new_list(&map, Allocator* allocator = allocator::heap())
{
if (!map.count) return {};
Value[] list = allocator.new_array(Value, map.count);
Value[] list = allocator::alloc_array(allocator, Value, map.count);
usz index = 0;
foreach (Entry* entry : map.table)
{
Expand Down Expand Up @@ -320,11 +321,10 @@ fn bool HashMap.has_value(&map, Value v) @if(VALUE_IS_EQUATABLE)

fn void HashMap.add_entry(&map, uint hash, Key key, Value value, uint bucket_index) @private
{
Entry* entry = map.allocator.new(Entry);
$if COPY_KEYS:
key = key.copy(map.allocator);
$endif
*entry = { .hash = hash, .key = key, .value = value, .next = map.table[bucket_index] };
Entry* entry = allocator::new(map.allocator, Entry, { .hash = hash, .key = key, .value = value, .next = map.table[bucket_index] });
map.table[bucket_index] = entry;
if (map.count++ >= map.threshold)
{
Expand All @@ -341,7 +341,7 @@ fn void HashMap.resize(&map, uint new_capacity) @private
map.threshold = uint.max;
return;
}
Entry*[] new_table = map.allocator.new_zero_array(Entry*, new_capacity);
Entry*[] new_table = allocator::new_array(map.allocator, Entry*, new_capacity);
map.transfer(new_table);
map.table = new_table;
map.free_internal(old_table.ptr);
Expand Down Expand Up @@ -405,7 +405,7 @@ fn void HashMap.put_for_create(&map, Key key, Value value) @private

fn void HashMap.free_internal(&map, void* ptr) @inline @private
{
map.allocator.free(ptr);
allocator::free(map.allocator, ptr);
}

fn bool HashMap.remove_entry_for_key(&map, Key key) @private
Expand Down Expand Up @@ -440,19 +440,18 @@ fn bool HashMap.remove_entry_for_key(&map, Key key) @private
fn void HashMap.create_entry(&map, uint hash, Key key, Value value, int bucket_index) @private
{
Entry *e = map.table[bucket_index];
Entry* entry = map.allocator.new(Entry);
$if COPY_KEYS:
key = key.copy(map.allocator);
$endif
*entry = { .hash = hash, .key = key, .value = value, .next = map.table[bucket_index] };
Entry* entry = allocator::new(map.allocator, Entry, { .hash = hash, .key = key, .value = value, .next = map.table[bucket_index] });
map.table[bucket_index] = entry;
map.count++;
}

fn void HashMap.free_entry(&self, Entry *entry) @local
{
$if COPY_KEYS:
self.allocator.free(entry.key);
allocator::free(self.allocator, entry.key);
$endif
self.free_internal(entry);
}
Expand Down
Loading

0 comments on commit 104766e

Please sign in to comment.