~sircmpwn/hare-dev

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
2 2

[PATCH hare] slices: add static_* functions

Details
Message ID
<20220509223834.19331-1-sebastian@sebsite.pw>
DKIM signature
pass
Download raw message
Patch: +66 -9
deletefrom has also been renamed to static_deletefrom, and a new
deletefrom function which reallocates memory has been added.
Documentation for the non-static variants has been updated to clarify
allocation behavior.

Signed-off-by: Sebastian <sebastian@sebsite.pw>
---
 slices/void.ha | 75 ++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 66 insertions(+), 9 deletions(-)

diff --git a/slices/void.ha b/slices/void.ha
index 9a591092..a539fcbb 100644
--- a/slices/void.ha
+++ b/slices/void.ha
@@ -3,7 +3,7 @@
use rt;
use types;

// Appends an item, or multiple items, to a slice.
// Appends an item, or multiple items, to a slice, reallocating if necessary.
export fn appendto(sl: *[]void, itemsz: size, items: const *void...) void = {
	const repr = sl: *types::slice;
	insertinto(sl, itemsz, repr.length, items...);
@@ -21,7 +21,19 @@ export fn appendto(sl: *[]void, itemsz: size, items: const *void...) void = {
	free(input);
};

// Inserts an item, or multiple items, to a slice, in O(n) time.
// Appends an item, or multiple items, to a slice. Aborts if the slice's
// capacity isn't large enough to fit the items.
export fn static_appendto(
	sl: *[]void,
	itemsz: size,
	items: const *void...
) void = {
	const repr = sl: *types::slice;
	static_insertinto(sl, itemsz, repr.length, items...);
};

// Inserts an item, or multiple items, to a slice, in O(n) time, reallocating if
// necessary.
export fn insertinto(
	sl: *[]void,
	itemsz: size,
@@ -51,17 +63,44 @@ export fn insertinto(
	free(input);
};

// Deletes a range of items from a slice, in O(n) time, without freeing memory.
export fn deletefrom(sl: *[]void, itemsz: size, start: size, end: size) void = {
	assert(start <= end);
	if (start == end) {
// Inserts an item, or multiple items, into a slice, in O(n) time. Aborts if the
// slice's capacity isn't large enough to fit the items.
export fn static_insertinto(
	sl: *[]void,
	itemsz: size,
	idx: size,
	items: const *void...
) void = {
	if (len(items) == 0) {
		return;
	};
	let sl = sl: *types::slice;
	sl.length += len(items);
	assert(sl.length <= sl.capacity,
		"static insert/append exceeds slice capacity");
	let data = sl.data: *[*]u8;
	rt::memmove(&data[start * itemsz], &data[end * itemsz],
		(sl.length - start) * itemsz);
	sl.length -= end - start;
	rt::memmove(&data[(idx + len(items)) * itemsz], &data[idx * itemsz],
		(sl.length - len(items) - idx) * itemsz);
	for (let i = 0z; i < len(items); i += 1) {
		rt::memcpy(&data[(idx + i) * itemsz], items[i], itemsz);
	};
};

// Deletes a range of items from a slice, in O(n) time. The slice may be
// reallocated. Reallocation will never fail.
export fn deletefrom(sl: *[]void, itemsz: size, start: size, end: size) void = {
	static_deletefrom(sl, itemsz, start, end);
	let sl = sl: *types::slice;
	if (sl.length <= sl.capacity / 2) {
		// TODO: switch to using alloc() once it's possible to handle
		// copy allocation errors
		match (rt::realloc(sl.data, sl.length * itemsz)) {
		case null => void;
		case let p: *void =>
			sl.data = p;
			sl.capacity = sl.length;
		};
	};
};

@test fn deletefrom() void = {
@@ -74,6 +113,24 @@ export fn deletefrom(sl: *[]void, itemsz: size, start: size, end: size) void = {
	free(input);
};

// Deletes a range of items from a slice, in O(n) time, without freeing memory.
export fn static_deletefrom(
	sl: *[]void,
	itemsz: size,
	start: size,
	end: size,
) void = {
	assert(start <= end);
	if (start == end) {
		return;
	};
	let sl = sl: *types::slice;
	let data = sl.data: *[*]u8;
	rt::memmove(&data[start * itemsz], &data[end * itemsz],
		(sl.length - start) * itemsz);
	sl.length -= end - start;
};

// Swaps two elements of a slice.
export fn swap(sl: []void, itemsz: size, a: size, b: size) void = {
	assert(a < len(sl) && b < len(sl));
-- 
2.35.1

[hare/patches] build success

builds.sr.ht <builds@sr.ht>
Details
Message ID
<CJVLB9RHQ6AG.2FZBGDKQS3MW6@cirno>
In-Reply-To
<20220509223834.19331-1-sebastian@sebsite.pw> (view parent)
DKIM signature
missing
Download raw message
hare/patches: SUCCESS in 1m31s

[slices: add static_* functions][0] from [Sebastian][1]

[0]: https://lists.sr.ht/~sircmpwn/hare-dev/patches/32134
[1]: sebastian@sebsite.pw

✓ #754787 SUCCESS hare/patches/alpine.yml  https://builds.sr.ht/~sircmpwn/job/754787
✓ #754788 SUCCESS hare/patches/freebsd.yml https://builds.sr.ht/~sircmpwn/job/754788
Details
Message ID
<CJYO1N1U1NJ0.2B7NGI9RG32GR@taiga>
In-Reply-To
<20220509223834.19331-1-sebastian@sebsite.pw> (view parent)
DKIM signature
fail
Download raw message
DKIM signature: fail
Thanks!

To git@git.sr.ht:~sircmpwn/hare
   7b323f92..ac7517aa  master -> master
Reply to thread Export thread (mbox)