<commit_before># -*- cython -*-
"""
List of sets of integers, low-level C implementation

Works similarly as

    setlist = [set() for j in range(n)]

but with integer values.

"""

cimport libc.stdlib
cimport numpy as np
import numpy as np

cdef struct setlist_t:
    size_t n
    size_t *sizes
    size_t *alloc_sizes
    int **sets

cdef inline int init(setlist_t *setlist, size_t n, size_t size_guess) except -1:
    """
    Initialise a list of `n` sets with a given guessed size
    """
    cdef int j

    setlist.n = n

    setlist.sets = <int**>libc.stdlib.malloc(sizeof(int*) * n)
    if setlist.sets == NULL:
        raise MemoryError

    setlist.sizes = <size_t*>libc.stdlib.malloc(sizeof(size_t) * n)
    if setlist.sizes == NULL:
        libc.stdlib.free(<void*>setlist.sets)
        raise MemoryError

    setlist.alloc_sizes = <size_t*>libc.stdlib.malloc(sizeof(size_t) * n)
    if setlist.alloc_sizes == NULL:
        libc.stdlib.free(<void*>setlist.sets)
        libc.stdlib.free(<void*>setlist.sizes)
        raise MemoryError

    for j in range(n):
        setlist.sizes[j] = 0
        setlist.alloc_sizes[j] = size_guess
        setlist.sets[j] = <int*>libc.stdlib.malloc(sizeof(int) * size_guess)
        if setlist.sets[j] == NULL:
            for i in range(j):
                libc.stdlib.free(<void*>setlist.sets[i])
            libc.stdlib.free(<void*>setlist.sets)
            libc.stdlib.free(<void*>setlist.sizes)
            libc.stdlib.free(<void*>setlist.alloc_sizes)
            raise MemoryError

    return 0

cdef inline void free(setlist_t *setlist):
    """
    Free the set list
    """

    cdef int j
    for j in range(setlist.n):
        libc.stdlib.free(<void*>setlist.sets[j])
    libc.stdlib.free(<void*>setlist.sets)
    libc.stdlib.free(<void*>setlist.sizes)
    libc.stdlib.free(<void*>setlist.alloc_sizes)
    setlist.sets = NULL
    setlist.sizes = NULL
    setlist.alloc_sizes = NULL
    setlist.n = 0

cdef inline int add(setlist_t *setlist, int n, int value) nogil:
    """
    Add a value to set `n`
    """

    cdef size_t i, sz
    cdef int *p

    if n < 0 or n >= setlist.n:
        return 1

    for i in range(setlist.sizes[n]):
        if setlist.sets[n][i] == value:
            return 0

    if setlist.sizes[n] >= setlist.alloc_sizes[n]:
        sz = 2*setlist.alloc_sizes[n] + 1
        p = <int*>libc.stdlib.realloc(<void*>setlist.sets[n], sz * sizeof(int))
        if p == NULL:<commit_msg>MAINT: Cleanup unneeded void* cast in setlist.pxd<commit_after># -*- cython -*-
"""
List of sets of integers, low-level C implementation

Works similarly as

    setlist = [set() for j in range(n)]

but with integer values.

"""

cimport libc.stdlib
cimport numpy as np
import numpy as np

cdef struct setlist_t:
    size_t n
    size_t *sizes
    size_t *alloc_sizes
    int **sets

cdef inline int init(setlist_t *setlist, size_t n, size_t size_guess) except -1:
    """
    Initialise a list of `n` sets with a given guessed size
    """
    cdef int j

    setlist.n = n

    setlist.sets = <int**>libc.stdlib.malloc(sizeof(int*) * n)
    if setlist.sets == NULL:
        raise MemoryError("Failed to allocate memory in setlist.init()")

    setlist.sizes = <size_t*>libc.stdlib.malloc(sizeof(size_t) * n)
    if setlist.sizes == NULL:
        libc.stdlib.free(setlist.sets)
        raise MemoryError("Failed to allocate memory in setlist.init()")

    setlist.alloc_sizes = <size_t*>libc.stdlib.malloc(sizeof(size_t) * n)
    if setlist.alloc_sizes == NULL:
        libc.stdlib.free(setlist.sets)
        libc.stdlib.free(setlist.sizes)
        raise MemoryError("Failed to allocate memory in setlist.init()")

    for j in range(n):
        setlist.sizes[j] = 0
        setlist.alloc_sizes[j] = size_guess
        setlist.sets[j] = <int*>libc.stdlib.malloc(sizeof(int) * size_guess)
        if setlist.sets[j] == NULL:
            for i in range(j):
                libc.stdlib.free(setlist.sets[i])
            libc.stdlib.free(setlist.sets)
            libc.stdlib.free(setlist.sizes)
            libc.stdlib.free(setlist.alloc_sizes)
            raise MemoryError("Failed to allocate memory in setlist.init()")

    return 0

cdef inline void free(setlist_t *setlist):
    """
    Free the set list
    """

    cdef int j
    for j in range(setlist.n):
        libc.stdlib.free(setlist.sets[j])
    libc.stdlib.free(setlist.sets)
    libc.stdlib.free(setlist.sizes)
    libc.stdlib.free(setlist.alloc_sizes)
    setlist.sets = NULL
    setlist.sizes = NULL
    setlist.alloc_sizes = NULL
    setlist.n = 0

cdef inline int add(setlist_t *setlist, int n, int value) nogil:
    """
    Add a value to set `n`
    """

    cdef size_t i, sz
    cdef int *p

    if n < 0 or n >= setlist.n:
        return 1

    for i in range(setlist.sizes[n]):
        if setlist.sets[n][i] == value:
            return 0

    if setlist.sizes[n] >= setlist.alloc_sizes[n]:
        sz = 2*setlist.alloc_sizes[n] + 1
        p = <int*>libc.stdlib.realloc(<void*>setlist.sets[n], sz * sizeof(int))
        if p == NULL:
{'fraction_non_alphanumeric': 0.12502464997042004, 'fraction_numerical': 0.0035495957404851116, 'mean_word_length': 2.517337031900139, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 30, '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': '5970424', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1757, 'n_words': 435}