<commit_before>/**
 * @file bitset.h
 * @date 15.10.2004
 * @author <NAME>
 * @brief A bitset implementation.
 */

#ifndef __FIRM_BITSET_H
#define __FIRM_BITSET_H

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>

#include "firm_config.h"
#include "bitfiddle.h"

typedef unsigned int bitset_pos_t;

#include "bitset_std.h"

/*
#if defined(__GNUC__) && defined(__i386__)
#include "bitset_ia32.h"
#endif
*/

typedef struct _bitset_t {
	bitset_pos_t units;
  bitset_pos_t size;
	bitset_unit_t *data;
} bitset_t;

#define BS_UNIT_SIZE sizeof(bitset_unit_t)
#define BS_UNIT_SIZE_BITS (BS_UNIT_SIZE * 8)
#define BS_UNIT_MASK (BS_UNIT_SIZE_BITS - 1)

/**
 * Initialize a bitset.
 * This functions should not be called.
 *
 * Note that this function needs three macros which must be provided by the
 * bitfield implementor:
 * - _bitset_overall_size(size) The overall size that must be
 *   allocated for the bitfield in bytes.
 * - _bitset_units(size) The number of units that will be
 *   present in the bitfield for a given highest bit.
 * - _bitset_data_ptr(data, size) This produces as pointer to the
 *   first unit in the allocated memory area. The main reason for this
 *   macro is, that some bitset implementors want control over memory
 *   alignment.
 *
 * @param area A pointer to memory reserved for the bitset.
 * @param size The size of the bitset in bits.
 * @return A pointer to the initialized bitset.
 */
static INLINE bitset_t *_bitset_prepare(void *area, bitset_pos_t size)
{
	bitset_t *ptr = area;
	memset(area, 0, _bitset_overall_size(sizeof(bitset_t), size));
	ptr->units = _bitset_units(size);
  ptr->size = size;
	ptr->data = _bitset_data_ptr(area, sizeof(bitset_t), size);
	return ptr;
}

/**
 * Mask out all bits, which are only there, because the number
 * of bits in the set didn't match a unit size boundary.
 * @param bs The bitset.
 * @return The masked bitset.
 */
static INLINE bitset_t *_bitset_mask_highest(bitset_t *bs)
{
	bitset_pos_t rest = bs->size & BS_UNIT_MASK;
	if (rest)
		bs->data[bs->units - 1] &= (1 << rest) - 1;
	return bs;
}

/**
 * Get the capacity of the bitset in bits.
 * @param bs The bitset.
 * @return The capacity in bits of the bitset.
 */
#define bitset_capacity(bs) ((bs)->units * BS_UNIT_SIZE_BITS)

/**
 * Get the size of the bitset in bits.
 * @note Note the difference between capacity and size.
 * @param bs The bitset.
 * @return The highest bit which can be set or cleared plus 1.
 */
#define bitset_size(bs)  ((bs)->size)

/**
 * Allocate a bitset on an obstack.
 * @param obst The obstack.
 * @param size The greatest bit that shall be stored in the set.
 * @return A pointer to an empty initialized bitset.
 */
#define bitset_obstack_alloc(obst,size) \
  _bitset_prepare(obstack_alloc(obst, _bitset_overall_size(sizeof(bitset_t), size)), size)

/**
 * Allocate a bitset via malloc.
 * @param size The greatest bit that shall be stored in the set.
 * @return A pointer to an empty initialized bitset.
 */
#define bitset_malloc(size) \
	_bitset_prepare(malloc(_bitset_overall_size(sizeof(bitset_t), size)), size)

/**
 * Free a bitset allocated with bitset_malloc().
 * @param bs The bitset.
 */
#define bitset_free(bs) free(bs)

/**
 * Allocate a bitset on the stack via alloca.
 * @param size The greatest bit that shall be stored in the set.
 * @return A pointer to an empty initialized bitset.<commit_msg>used xmalloc instead of malloc<commit_after>/**
 * @file bitset.h
 * @date 15.10.2004
 * @author <NAME>
 * @brief A bitset implementation.
 */

#ifndef __FIRM_BITSET_H
#define __FIRM_BITSET_H

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>

#include "firm_config.h"
#include "bitfiddle.h"

typedef unsigned int bitset_pos_t;

#include "bitset_std.h"

/*
#if defined(__GNUC__) && defined(__i386__)
#include "bitset_ia32.h"
#endif
*/

typedef struct _bitset_t {
	bitset_pos_t units;
  bitset_pos_t size;
	bitset_unit_t *data;
} bitset_t;

#define BS_UNIT_SIZE sizeof(bitset_unit_t)
#define BS_UNIT_SIZE_BITS (BS_UNIT_SIZE * 8)
#define BS_UNIT_MASK (BS_UNIT_SIZE_BITS - 1)

/**
 * Initialize a bitset.
 * This functions should not be called.
 *
 * Note that this function needs three macros which must be provided by the
 * bitfield implementor:
 * - _bitset_overall_size(size) The overall size that must be
 *   allocated for the bitfield in bytes.
 * - _bitset_units(size) The number of units that will be
 *   present in the bitfield for a given highest bit.
 * - _bitset_data_ptr(data, size) This produces as pointer to the
 *   first unit in the allocated memory area. The main reason for this
 *   macro is, that some bitset implementors want control over memory
 *   alignment.
 *
 * @param area A pointer to memory reserved for the bitset.
 * @param size The size of the bitset in bits.
 * @return A pointer to the initialized bitset.
 */
static INLINE bitset_t *_bitset_prepare(void *area, bitset_pos_t size)
{
	bitset_t *ptr = area;
	memset(area, 0, _bitset_overall_size(sizeof(bitset_t), size));
	ptr->units = _bitset_units(size);
  ptr->size = size;
	ptr->data = _bitset_data_ptr(area, sizeof(bitset_t), size);
	return ptr;
}

/**
 * Mask out all bits, which are only there, because the number
 * of bits in the set didn't match a unit size boundary.
 * @param bs The bitset.
 * @return The masked bitset.
 */
static INLINE bitset_t *_bitset_mask_highest(bitset_t *bs)
{
	bitset_pos_t rest = bs->size & BS_UNIT_MASK;
	if (rest)
		bs->data[bs->units - 1] &= (1 << rest) - 1;
	return bs;
}

/**
 * Get the capacity of the bitset in bits.
 * @param bs The bitset.
 * @return The capacity in bits of the bitset.
 */
#define bitset_capacity(bs) ((bs)->units * BS_UNIT_SIZE_BITS)

/**
 * Get the size of the bitset in bits.
 * @note Note the difference between capacity and size.
 * @param bs The bitset.
 * @return The highest bit which can be set or cleared plus 1.
 */
#define bitset_size(bs)  ((bs)->size)

/**
 * Allocate a bitset on an obstack.
 * @param obst The obstack.
 * @param size The greatest bit that shall be stored in the set.
 * @return A pointer to an empty initialized bitset.
 */
#define bitset_obstack_alloc(obst,size) \
  _bitset_prepare(obstack_alloc(obst, _bitset_overall_size(sizeof(bitset_t), size)), size)

/**
 * Allocate a bitset via malloc.
 * @param size The greatest bit that shall be stored in the set.
 * @return A pointer to an empty initialized bitset.
 */
#define bitset_malloc(size) \
	_bitset_prepare(xmalloc(_bitset_overall_size(sizeof(bitset_t), size)), size)

/**
 * Free a bitset allocated with bitset_malloc().
 * @param bs The bitset.
 */
#define bitset_free(bs) free(bs)

/**
 * Allocate a bitset on the stack via alloca.
 * @param size The greatest bit that shall be stored in the set.
 * @return A pointer to an empty initialized bitset.
{'fraction_non_alphanumeric': 0.10404454865181711, 'fraction_numerical': 0.005861664712778429, 'mean_word_length': 4.066815144766147, 'pattern_counts': {'":': 0, '<': 17, '<?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': '6837668', 'n_tokens_mistral': 2391, 'n_tokens_neox': 2319, 'n_words': 821}