mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
remove bitset
This commit is contained in:
parent
dc22ec9035
commit
c52b222e35
5 changed files with 0 additions and 379 deletions
|
@ -1,73 +0,0 @@
|
|||
/** A datastructure storing bitsets of arbitrary dimensions.
|
||||
*
|
||||
* @file
|
||||
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
||||
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
|
||||
* @license GNU General Public License (version 3)
|
||||
*
|
||||
* VILLAScommon
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*********************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct bitset {
|
||||
char *set;
|
||||
size_t dimension;
|
||||
};
|
||||
|
||||
/** Allocate memory for a new betset */
|
||||
int bitset_init(struct bitset *b, size_t dim);
|
||||
|
||||
/** Release memory of bit set */
|
||||
int bitset_destroy(struct bitset *b);
|
||||
|
||||
void bitset_set_value(struct bitset *b, uintmax_t val);
|
||||
uintmax_t bitset_get_value(struct bitset *b);
|
||||
|
||||
/** Return the number of bits int the set which are set (1) */
|
||||
size_t bitset_count(struct bitset *b);
|
||||
|
||||
/** Set a single bit in the set */
|
||||
int bitset_set(struct bitset *b, size_t bit);
|
||||
|
||||
/** Clear a single bit in the set */
|
||||
int bitset_clear(struct bitset *b, size_t bit);
|
||||
|
||||
/** Set all bits in the set */
|
||||
void bitset_set_all(struct bitset *b);
|
||||
|
||||
/** Clear all bits in the set */
|
||||
void bitset_clear_all(struct bitset *b);
|
||||
|
||||
/** Test if a single bit in the set is set */
|
||||
int bitset_test(struct bitset *b, size_t bit);
|
||||
|
||||
/** Compare two bit sets bit-by-bit */
|
||||
int bitset_cmp(struct bitset *a, struct bitset *b);
|
||||
|
||||
/** Return an human readable representation of the bit set */
|
||||
char *bitset_dump(struct bitset *b);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
|
@ -22,7 +22,6 @@
|
|||
|
||||
add_library(villas-common SHARED
|
||||
advio.cpp
|
||||
bitset.cpp
|
||||
buffer.cpp
|
||||
json_buffer.cpp
|
||||
compat.cpp
|
||||
|
|
|
@ -1,170 +0,0 @@
|
|||
/** A datastructure storing bitsets of arbitrary dimensions.
|
||||
*
|
||||
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
||||
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
|
||||
* @license GNU General Public License (version 3)
|
||||
*
|
||||
* VILLAScommon
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*********************************************************************************/
|
||||
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include <villas/bitset.h>
|
||||
#include <villas/utils.h>
|
||||
|
||||
#define bitset_mask(b) (1 << ((b) % CHAR_BIT))
|
||||
#define bitset_slot(b) ((b) / CHAR_BIT)
|
||||
#define bitset_nslots(nb) ((nb + CHAR_BIT - 1) / CHAR_BIT)
|
||||
|
||||
int bitset_init(struct bitset *b, size_t dim)
|
||||
{
|
||||
int s = bitset_nslots(dim);
|
||||
|
||||
b->set = (char *) alloc(s * CHAR_BIT);
|
||||
b->dimension = dim;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bitset_destroy(struct bitset *b)
|
||||
{
|
||||
free(b->set);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void bitset_set_value(struct bitset *b, uintmax_t val)
|
||||
{
|
||||
bitset_clear_all(b);
|
||||
|
||||
for (size_t i = 0; i < b->dimension; i++) {
|
||||
if (val & (1 << i))
|
||||
bitset_set(b, i);
|
||||
}
|
||||
}
|
||||
|
||||
uintmax_t bitset_get_value(struct bitset *b)
|
||||
{
|
||||
uintmax_t v = 0;
|
||||
|
||||
for (size_t i = 0; i < b->dimension; i++)
|
||||
v += bitset_test(b, i) << i;
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
size_t bitset_count(struct bitset *b)
|
||||
{
|
||||
size_t cnt = 0;
|
||||
|
||||
for (size_t i = 0; i < b->dimension; i++)
|
||||
cnt += bitset_test(b, i);
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
int bitset_set(struct bitset *b, size_t bit)
|
||||
{
|
||||
int s = bitset_slot(bit);
|
||||
char m = bitset_mask(bit);
|
||||
|
||||
if (bit >= b->dimension)
|
||||
return -1;
|
||||
|
||||
b->set[s] |= m;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bitset_clear(struct bitset *b, size_t bit)
|
||||
{
|
||||
int s = bitset_slot(bit);
|
||||
char m = bitset_mask(bit);
|
||||
|
||||
if (bit >= b->dimension)
|
||||
return -1;
|
||||
|
||||
b->set[s] &= ~m;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bitset_test(struct bitset *b, size_t bit)
|
||||
{
|
||||
int s = bitset_slot(bit);
|
||||
char m = bitset_mask(bit);
|
||||
|
||||
if (bit >= b->dimension)
|
||||
return -1;
|
||||
|
||||
return b->set[s] & m ? 1 : 0;
|
||||
}
|
||||
|
||||
void bitset_set_all(struct bitset *b)
|
||||
{
|
||||
int s = b->dimension / CHAR_BIT;
|
||||
|
||||
if (b->dimension % CHAR_BIT) {
|
||||
char m = (1 << (b->dimension % CHAR_BIT)) - 1;
|
||||
|
||||
b->set[s] |= m;
|
||||
}
|
||||
|
||||
memset(b->set, ~0, s);
|
||||
}
|
||||
|
||||
void bitset_clear_all(struct bitset *b)
|
||||
{
|
||||
int s = b->dimension / CHAR_BIT;
|
||||
|
||||
/* Clear last byte */
|
||||
if (b->dimension % CHAR_BIT) {
|
||||
char m = (1 << (b->dimension % CHAR_BIT)) - 1;
|
||||
|
||||
b->set[s] &= ~m;
|
||||
}
|
||||
|
||||
memset(b->set, 0x00, s);
|
||||
}
|
||||
|
||||
int bitset_cmp(struct bitset *a, struct bitset *b)
|
||||
{
|
||||
int s = a->dimension / CHAR_BIT;
|
||||
|
||||
if (a->dimension != b->dimension)
|
||||
return -1;
|
||||
|
||||
/* Compare last byte with mask */
|
||||
if (a->dimension % CHAR_BIT) {
|
||||
char m = (1 << (a->dimension % CHAR_BIT)) - 1;
|
||||
|
||||
if ((a->set[s] & m) != (b->set[s] & m))
|
||||
return -1;
|
||||
}
|
||||
|
||||
return memcmp(a->set, b->set, s);
|
||||
}
|
||||
|
||||
char * bitset_dump(struct bitset *b)
|
||||
{
|
||||
char *str = NULL;
|
||||
|
||||
for (unsigned i = 0; i < b->dimension; i++)
|
||||
strcatf(&str, "%d", bitset_test(b, i));
|
||||
|
||||
return str;
|
||||
}
|
|
@ -23,7 +23,6 @@
|
|||
add_executable(unit-tests-common
|
||||
advio.cpp
|
||||
json_buffer.cpp
|
||||
bitset.cpp
|
||||
graph.cpp
|
||||
hist.cpp
|
||||
kernel.cpp
|
||||
|
|
|
@ -1,134 +0,0 @@
|
|||
/** Unit tests for advio
|
||||
*
|
||||
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
||||
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
|
||||
* @license GNU General Public License (version 3)
|
||||
*
|
||||
* VILLAScommon
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*********************************************************************************/
|
||||
|
||||
#include <criterion/criterion.h>
|
||||
|
||||
#include <villas/bitset.h>
|
||||
#include <villas/utils.h>
|
||||
|
||||
#define LEN 1027
|
||||
|
||||
TestSuite(bitset,
|
||||
.description = "Bitset datastructure"
|
||||
);
|
||||
|
||||
Test(bitset, simple)
|
||||
{
|
||||
int ret;
|
||||
struct bitset bs;
|
||||
|
||||
int bits[] = { 23, 223, 25, 111, 252, 86, 222, 454, LEN-1 };
|
||||
|
||||
ret = bitset_init(&bs, LEN);
|
||||
cr_assert_eq(ret, 0);
|
||||
|
||||
for (unsigned i = 0; i < ARRAY_LEN(bits); i++) {
|
||||
bitset_set(&bs, bits[i]);
|
||||
cr_assert_eq(ret, 0);
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < ARRAY_LEN(bits); i++) {
|
||||
ret = bitset_test(&bs, bits[i]);
|
||||
cr_assert_eq(ret, 1, "Failed at bit %d", i);
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < ARRAY_LEN(bits); i++) {
|
||||
ret = bitset_clear(&bs, bits[i]);
|
||||
cr_assert_eq(ret, 0, "Failed at bit %d", i);
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < LEN; i++) {
|
||||
ret = bitset_test(&bs, i);
|
||||
cr_assert_eq(ret, 0);
|
||||
}
|
||||
|
||||
ret = bitset_destroy(&bs);
|
||||
cr_assert_eq(ret, 0);
|
||||
}
|
||||
|
||||
Test(bitset, outofbounds)
|
||||
{
|
||||
int ret;
|
||||
struct bitset bs;
|
||||
|
||||
ret = bitset_init(&bs, LEN);
|
||||
cr_assert_eq(ret, 0);
|
||||
|
||||
ret = bitset_set(&bs, LEN+1);
|
||||
cr_assert_eq(ret, -1);
|
||||
|
||||
ret = bitset_test(&bs, LEN+1);
|
||||
cr_assert_eq(ret, -1);
|
||||
|
||||
ret = bitset_destroy(&bs);
|
||||
cr_assert_eq(ret, 0);
|
||||
}
|
||||
|
||||
Test(bitset, cmp)
|
||||
{
|
||||
int ret;
|
||||
struct bitset bs1, bs2;
|
||||
|
||||
ret = bitset_init(&bs1, LEN);
|
||||
cr_assert_eq(ret, 0);
|
||||
|
||||
ret = bitset_init(&bs2, LEN);
|
||||
cr_assert_eq(ret, 0);
|
||||
|
||||
ret = bitset_set(&bs1, 525);
|
||||
cr_assert_eq(ret, 0);
|
||||
|
||||
ret = bitset_set(&bs2, 525);
|
||||
cr_assert_eq(ret, 0);
|
||||
|
||||
ret = bitset_cmp(&bs1, &bs2);
|
||||
cr_assert_eq(ret, 0);
|
||||
|
||||
ret = bitset_clear(&bs2, 525);
|
||||
cr_assert_eq(ret, 0);
|
||||
|
||||
ret = bitset_cmp(&bs1, &bs2);
|
||||
cr_assert_neq(ret, 0);
|
||||
|
||||
ret = bitset_destroy(&bs1);
|
||||
cr_assert_eq(ret, 0);
|
||||
|
||||
ret = bitset_destroy(&bs2);
|
||||
cr_assert_eq(ret, 0);
|
||||
}
|
||||
|
||||
Test(bitset, all)
|
||||
{
|
||||
int ret;
|
||||
struct bitset bs;
|
||||
|
||||
ret = bitset_init(&bs, LEN);
|
||||
cr_assert_eq(ret, 0);
|
||||
|
||||
for (unsigned i = 0; i < LEN; i++) {
|
||||
bitset_test(&bs, i);
|
||||
cr_assert_eq(ret, 0);
|
||||
}
|
||||
|
||||
ret = bitset_destroy(&bs);
|
||||
cr_assert_eq(ret, 0);
|
||||
}
|
Loading…
Add table
Reference in a new issue