<commit_before>/*
 * Copyright (c) 2020
 * See LICENSE for details.
 *
 * misscelan
 */

#include <assert.h>
#include <string.h>
#include <math.h>

#include "sega3d.h"

#include "sega3d-internal.h"

static struct {
        /* Top pointer to the pool of singles */
        sort_single_t *pool_free_top;
        /* The maximum indexed Z value when adding packets */
        int32_t max_z;
} _state;

static inline void __always_inline
_pool_stack_reset(void)
{
        _state.pool_free_top = &_internal_state->sort_single_pool[0];
}

static inline sort_single_t * __always_inline
_pool_stack_alloc(void)
{
        sort_single_t * const single = _state.pool_free_top;

        _state.pool_free_top++;

        return single;
}

static inline void __always_inline
_state_reset(void)
{
        _pool_stack_reset();

        _state.max_z = 0;
}

void
_internal_sort_init(void)
{
        _state_reset();

        (void)memset(_internal_state->sort_list, 0, sizeof(sort_list_t) * SORT_Z_RANGE);
}

void
_internal_sort_add(void *packet, int32_t pz)
{
        pz = clamp(pz, 0, SORT_Z_RANGE - 1);

        _state.max_z = max(pz, _state.max_z);

        sort_single_t ** const list_head = &_internal_state->sort_list[pz].head;

        sort_single_t * const new_single = _pool_stack_alloc();

        new_single->packet = packet;
        new_single->next_single = (*list_head != NULL) ? *list_head : NULL;

        *list_head = new_single;
}

void
_internal_sort_iterate(sort_iterate_fn_t iterate_fn)
{
        assert(iterate_fn != NULL);

        sort_list_t *list_head;
        list_head = &_internal_state->sort_list[SORT_Z_RANGE - 1];

        for (int32_t i = 0; i < SORT_Z_RANGE; i++, list_head--) {
                const sort_single_t *single;
                single = list_head->head;

                while (single != NULL) {
                        iterate_fn(single);

                        single = single->next_single;
                }

                list_head->head = NULL;
        }

        _state_reset();<commit_msg>Simplify adding packet in sort<commit_after>/*
 * Copyright (c) 2020
 * See LICENSE for details.
 *
 * misscelan
 */

#include <assert.h>
#include <string.h>
#include <math.h>

#include "sega3d.h"

#include "sega3d-internal.h"

static struct {
        /* Top pointer to the pool of singles */
        sort_single_t *pool_free_top;
        /* The maximum indexed Z value when adding packets */
        int32_t max_z;
} _state;

static inline void __always_inline
_pool_stack_reset(void)
{
        _state.pool_free_top = &_internal_state->sort_single_pool[0];
}

static inline sort_single_t * __always_inline
_pool_stack_alloc(void)
{
        sort_single_t * const single = _state.pool_free_top;

        _state.pool_free_top++;

        return single;
}

static inline void __always_inline
_state_reset(void)
{
        _pool_stack_reset();

        _state.max_z = 0;
}

void
_internal_sort_init(void)
{
        _state_reset();

        (void)memset(_internal_state->sort_list, 0, sizeof(sort_list_t) * SORT_Z_RANGE);
}

void
_internal_sort_add(void *packet, int32_t pz)
{
        pz = clamp(pz, 0, SORT_Z_RANGE - 1);

        _state.max_z = max(pz, _state.max_z);

        sort_list_t * const list_head = &_internal_state->sort_list[pz];

        sort_single_t * const new_single = _pool_stack_alloc();

        new_single->packet = packet;
        new_single->next_single = list_head->head;

        list_head->head = new_single;
}

void
_internal_sort_iterate(sort_iterate_fn_t iterate_fn)
{
        assert(iterate_fn != NULL);

        sort_list_t *list_head;
        list_head = &_internal_state->sort_list[SORT_Z_RANGE - 1];

        for (int32_t i = 0; i < SORT_Z_RANGE; i++, list_head--) {
                const sort_single_t *single;
                single = list_head->head;

                while (single != NULL) {
                        iterate_fn(single);

                        single = single->next_single;
                }

                list_head->head = NULL;
        }

        _state_reset();
{'fraction_non_alphanumeric': 0.10457354758961682, 'fraction_numerical': 0.009394313967861558, 'mean_word_length': 2.4819277108433737, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, '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': '1019109', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1370, 'n_words': 274}