<commit_before>#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "set.h"
#include "util.h"
#include "vec.h"

Set *
cloneset(Set *src)
{
	Set *ret;

	ret = make_set();
	memcpy(ret->v, src->v, src->c);

	return ret;
}

int
expandset(Set *A)
{
	size_t c;
	void *tmp;

	c = A->c;
	tmp = realloc(A->v, A->c * 2);
	if (tmp == NULL) die("realloc");

	A->v = tmp;
	memset(A->v + c, 0, c);

	return SUCC;
}

void
free_set(Set *A)
{
	if (!A) return;
	free(A->v);
	free(A);
}

Set *
make_set(void)
{
#define SETSIZ 8
	Set *ret;

	ret = malloc(sizeof *ret);
	if (!ret) die("malloc");
	
	ret->v = calloc(SETSIZ, sizeof *ret->v);
	if (!ret->v) die("calloc");
	ret->c = SETSIZ;

	return ret;
}


size_t
offset(uint32_t i)
{
	size_t ret = 0;

	if (i == 0) return (0);

	do ++ret; while (i >>= 1);

	return ret;
}

void *
set2vec(Set *A)
{
	Set *B;
	uint32_t b;
	size_t i, j, t[32];
	Vector(size_t) *ret;

	ret = calloc(1, sizeof *ret);
	if (!ret) die("calloc");
	make_vector(ret);
	B = cloneset(A);

	for (i = 0; i < A->c; ++i) {
		for (j = 0; B->v[i]; ++j) {
			b = B->v[i] & -B->v[i];
			t[j] = offset (b) + i * 32;
			B->v[i] ^= b;
		}
		while (j) vec_append(ret, t + --j);
	}
	free_set(B);
	return ret;
}

Set *
setaddmemb(Set *A, size_t memb)
{
	if (memb / 32 > A->c) expandset (A);

	A->v[memb / 32] |= BIT(memb % 32);

	return A;
}

Set *
setaddrange(Set *A, size_t beg, size_t end)
{
	size_t i, j;
	uint32_t k, n, premask, postmask;

	if (beg > end) return NULL;

	if (end > A->c) expandset (A);

	for (i = j = 0; i < end; i += 32, ++j) {
		premask = postmask = 0;
		if (i + 31U > beg) {
			k = BIT (beg - i);
			if ( k > 1 ) premask = k - 1;
			n = end - i > 32 ? BIT (31) : BIT (end - i);
			if ( n < BIT (31)) postmask = ~(n - 1);

			A->v[j] = ~(uint32_t)0;
			A->v[j] ^= premask;
			A->v[j] ^= postmask;<commit_msg>style changes and interface bump;<commit_after>#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "set.h"
#include "util.h"
#include "vec.h"

Set *
cloneset(Set *src)
{
	Set *ret;

	ret = make_set();
	memcpy(ret->v, src->v, src->c);

	return ret;
}

int
expandset(Set *A)
{
	size_t c;
	void *tmp;

	c = A->c;
	tmp = realloc(A->v, A->c * 2);
	if (tmp == NULL) die("realloc");

	A->v = tmp;
	memset(A->v + c, 0, c);

	return SUCC;
}

void
free_set(Set *A)
{
	if (!A) return;
	free(A->v);
	free(A);
}

Set *
make_set(void)
{
#define SETSIZ 8
	Set *ret;

	ret = malloc(sizeof *ret);
	if (!ret) die("malloc");
	
	ret->v = calloc(SETSIZ, sizeof *ret->v);
	if (!ret->v) die("calloc");
	ret->c = SETSIZ;

	return ret;
}


size_t
offset(uint32_t i)
{
	size_t ret = 0;

	if (i == 0) return (0);

	do ++ret; while (i >>= 1);

	return ret;
}

void *
set2vec(Set *A)
{
	Set *B;
	uint32_t b;
	size_t i, j, t[32];
	Vector(size_t) *ret;

	make_vector(ret);
	if (!ret) die("make_vector");
	B = cloneset(A);

	for (i = 0; i < A->c; ++i) {
		for (j = 0; B->v[i]; ++j) {
			b = B->v[i] & -B->v[i];
			t[j] = offset (b) + i * 32;
			B->v[i] ^= b;
		}
		while (j) vec_append(ret, t + --j);
	}
	free_set(B);
	return ret;
}

Set *
setaddmemb(Set *A, size_t memb)
{
	if (memb / 32 > A->c) expandset(A);

	A->v[memb / 32] |= BIT(memb % 32);

	return A;
}

Set *
setaddrange(Set *A, size_t beg, size_t end)
{
	size_t i, j;
	uint32_t k, n, premask, postmask;

	if (beg > end) return NULL;

	if (end > A->c) expandset(A);

	for (i = j = 0; i < end; i += 32, ++j) {
		premask = postmask = 0;
		if (i + 31U > beg) {
			k = BIT (beg - i);
			if ( k > 1 ) premask = k - 1;
			n = end - i > 32 ? BIT (31) : BIT (end - i);
			if ( n < BIT (31)) postmask = ~(n - 1);

			A->v[j] = ~(uint32_t)0;
			A->v[j] ^= premask;
			A->v[j] ^= postmask;
{'fraction_non_alphanumeric': 0.22537878787878787, 'fraction_numerical': 0.02408008658008658, 'mean_word_length': 2.8231644260599795, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2022720', 'n_tokens_mistral': 1986, 'n_tokens_neox': 1811, 'n_words': 483}