Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions benchmarks/stdlib/collections/hashmap.c3
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ fn void hash_speeds_of_many_random_values() => @pool()
foreach (&v : vwideints) *v = (uint128)random::next(&rand, uint.max);

char[48][] zstrs = allocator::new_array(tmem, char[48], $arrsz)[:$arrsz];
String[$arrsz] strs;

String[] strs = mem::temp_array(String, $arrsz);
foreach (x, &v : zstrs)
{
foreach (&c : (*v)[:random::next(&rand, 48)]) *c = (char)random::next(&rand, char.max);
Expand Down Expand Up @@ -195,7 +196,7 @@ fn void random_access_string_keys() => @pool()
v.tinit();

usz pseudo_checksum = 0;
String[5_000] saved;
String[] saved = mem::temp_array(String, 5_000);

for (usz i = 0; i < saved.len; ++i)
{
Expand Down
38 changes: 38 additions & 0 deletions benchmarks/stdlib/collections/linkedlist.c3
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module linkedlist_benchmarks;

import std::collections::linkedlist;


LinkedList{int} long_list;
const HAY = 2;
const NEEDLE = 1000;

fn void bench_setup() @init
{
set_benchmark_warmup_iterations(3);
set_benchmark_max_iterations(4096);

int[*] haystack = { [0..999] = HAY };
long_list = linkedlist::@new{int}(mem, haystack[..]);
long_list.push(NEEDLE);
long_list.push_all(haystack[..]);
}


// ==============================================================================================
module linkedlist_benchmarks @benchmark;

String die_str = "Failed to find the value `1`. Is something broken?";


fn void foreach_iterator()
{
foreach (v : long_list.array_view()) if (v == NEEDLE) return;
runtime::@kill_benchmark(die_str);
}

fn void foreach_r_iterator()
{
foreach_r (v : long_list.array_view()) if (v == NEEDLE) return;
runtime::@kill_benchmark(die_str);
}
2 changes: 1 addition & 1 deletion benchmarks/stdlib/core/string_trim.c3
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ macro void trim_bench($trim_str, String $target = WHITESPACE_TARGET) => @pool()
$switch:
$case $typeof($trim_str) == String:
s1 = s2.trim($trim_str);
$case $typeof($$trim_str) == AsciiCharset:
$case $typeof($trim_str) == AsciiCharset:
s1 = s2.trim_charset($trim_str);
$default: $error "Unable to determine the right String `trim` operation to use.";
$endswitch
Expand Down
139 changes: 134 additions & 5 deletions lib/std/collections/linkedlist.c3
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,46 @@
// Use of self source code is governed by the MIT license
// a copy of which can be found in the LICENSE_STDLIB file.
module std::collections::linkedlist{Type};
import std::io;

const ELEMENT_IS_EQUATABLE = types::is_equatable_type(Type);

struct Node @private
struct Node
{
Node *next;
Node *prev;
Node* next;
Node* prev;
Type value;
}

struct LinkedList
{
Allocator allocator;
usz size;
Node *_first;
Node *_last;
Node* _first;
Node* _last;
}

fn usz? LinkedList.to_format(&self, Formatter* f) @dynamic
{
usz len = f.print("{ ")!;
for (Node* node = self._first; node != null; node.next)
{
len += f.printf(node.next ? "%s, " : "s", node.value)!;
}
return len + f.print(" }");
}

macro LinkedList @new(Allocator allocator, Type[] #default_values = {})
{
LinkedList new_list;
new_list.init(allocator);
new_list.push_all(#default_values);
return new_list;
}

macro LinkedList @tnew(Type[] #default_values = {})
{
return @new(tmem, #default_values);
}

<*
Expand Down Expand Up @@ -68,6 +92,11 @@ fn void LinkedList.push_front(&self, Type value)
self.size++;
}

fn void LinkedList.push_front_all(&self, Type[] value)
{
foreach_r (v : value) self.push_front(v);
}

fn void LinkedList.push(&self, Type value)
{
Node *last = self._last;
Expand All @@ -85,6 +114,11 @@ fn void LinkedList.push(&self, Type value)
self.size++;
}

fn void LinkedList.push_all(&self, Type[] value)
{
foreach (v : value) self.push(v);
}

fn Type? LinkedList.peek(&self) => self.first() @inline;
fn Type? LinkedList.peek_last(&self) => self.last() @inline;

Expand Down Expand Up @@ -133,6 +167,7 @@ macro Node* LinkedList.node_at_index(&self, usz index)
while (index--) node = node.next;
return node;
}

<*
@require index < self.size
*>
Expand All @@ -141,6 +176,14 @@ fn Type LinkedList.get(&self, usz index)
return self.node_at_index(index).value;
}

<*
@require index < self.size
*>
fn Type* LinkedList.get_ref(&self, usz index)
{
return &self.node_at_index(index).value;
}

<*
@require index < self.size
*>
Expand All @@ -149,6 +192,26 @@ fn void LinkedList.set(&self, usz index, Type element)
self.node_at_index(index).value = element;
}

fn usz? LinkedList.index_of(&self, Type t) @if(ELEMENT_IS_EQUATABLE)
{
for (Node* node = self._first, usz i = 0; node != null; node = node.next, ++i)
{
if (node.value == t) return i;
}
return NOT_FOUND?;
}

fn usz? LinkedList.rindex_of(&self, Type t) @if(ELEMENT_IS_EQUATABLE)
{
for (Node* node = self._last, usz i = self.size - 1; node != null; node = node.prev, --i)
{
if (node.value == t) return i;
if (i == 0) break;
}
return NOT_FOUND?;
}


<*
@require index < self.size
*>
Expand Down Expand Up @@ -334,3 +397,69 @@ fn void LinkedList.unlink(&self, Node* x) @private
self.free_node(x);
self.size--;
}


macro bool LinkedList.eq(&self, other) @operator(==) @if(ELEMENT_IS_EQUATABLE)
{
Node* node1 = self._first;
Node* node2 = other._first;
while (true)
{
if (!node1) return node2 == null;
if (!node2) return false;
if (node1.value != node2.value) return false;
node1 = node1.next;
node2 = node2.next;
}
return true;
}

fn LinkedListArrayView LinkedList.array_view(&self)
{
return { .list = self, .current_node = self._first };
}


struct LinkedListArrayView
{
LinkedList* list;
Node* current_node;
usz current_index;
}

fn usz LinkedListArrayView.len(&self) @operator(len) => self.list.size;

<*
@require index < self.list.size
*>
fn Type LinkedListArrayView.get(&self, usz index) @operator([])
{
return *self.get_ref(index);
}

<*
@require index < self.list.size
*>
fn Type* LinkedListArrayView.get_ref(&self, usz index) @operator(&[])
{
if (index == self.list.size - 1)
{
self.current_node = self.list._last;
self.current_index = index;
}

while (self.current_index != index)
{
switch
{
case index < self.current_index: // reverse iteration
self.current_node = self.current_node.prev;
self.current_index--;
case index > self.current_index:
self.current_node = self.current_node.next;
self.current_index++;
}
}

return &self.current_node.value;
}
2 changes: 0 additions & 2 deletions lib/std/core/array.c3
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ macro bool @all(array, #predicate)
@require @is_valid_list(left) &&& @is_valid_list(right) : "Left and right sides must be integer indexable"
@require @is_valid_operation(left, right, ...#operation) : "The operator must take two parameters matching the elements of the left and right side"
@require @is_valid_fill(left, right, ...fill_with) : "The specified fill value does not match either the left or the right array's underlying type."

*>
macro @zip(Allocator allocator, left, right, #operation = ..., fill_with = ...) @nodiscard
{
Expand Down Expand Up @@ -460,7 +459,6 @@ macro @zip(Allocator allocator, left, right, #operation = ..., fill_with = ...)
@require @is_valid_list(left) &&& @is_valid_list(right) : "Left and right sides must be integer indexable"
@require @is_valid_operation(left, right, ...#operation) : "The operator must take two parameters matching the elements of the left and right side"
@require @is_valid_fill(left, right, ...fill_with) : "The specified fill value does not match either the left or the right array's underlying type."

*>
macro @tzip(left, right, #operation = ..., fill_with = ...) @nodiscard
{
Expand Down
8 changes: 8 additions & 0 deletions lib/std/core/runtime_benchmark.c3
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ long cycle_stop @local;
DString benchmark_log @local;
bool benchmark_warming @local;
uint this_iteration @local;
bool benchmark_stop @local;

macro @start_benchmark()
{
Expand All @@ -69,6 +70,12 @@ macro @end_benchmark()
cycle_stop = $$sysclock();
}

macro @kill_benchmark(String format, ...)
{
@log_benchmark(format, $vasplat);
benchmark_stop = true;
}

macro @log_benchmark(msg, args...) => @pool()
{
if (benchmark_warming) return;
Expand Down Expand Up @@ -133,6 +140,7 @@ fn bool run_benchmarks(BenchmarkUnit[] benchmarks)
@start_benchmark(); // can be overridden by calls inside the unit's func

unit.func() @inline;
if (benchmark_stop) return false;

if (benchmark_nano_seconds == (NanoDuration){}) @end_benchmark(); // only mark when it wasn't already by the unit.func

Expand Down
2 changes: 2 additions & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
- Added generic `InterfaceList` to store a list of values that implement a specific interface
- Added `path::home_directory`, `path::documents_directory`, `path::videos_directory`, `path::pictures_directory`, `path::desktop_directory`, `path::screenshots_directory`,
`path::public_share_directory`, `path::templates_directory`, `path::saved_games_directory`, `path::music_directory`, `path::downloads_directory`.
- Add `LinkedList` array_view to support `[]` and `foreach`/`foreach_r`. #2438
- Make `LinkedList` printable and add `==` operator. #2438

## 0.7.5 Change list

Expand Down
Loading
Loading