Add SmallIntSet

This commit is contained in:
Sven Weidauer 2013-05-05 16:37:40 +02:00
parent 885af65701
commit 770e55e027
2 changed files with 69 additions and 0 deletions

27
SmallIntSet.c Normal file
View file

@ -0,0 +1,27 @@
//
// SmallIntSet.c
// JSParser
//
// Created by Sven Weidauer on 06.04.11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#include "SmallIntSet.h"
#include <stdarg.h>
SmallIntSet_t SmallIntSetWithValues( size_t count, ... )
{
va_list args;
va_start( args, count );
SmallIntSet_t result = SMALLINTSET_EMPTY;
for (size_t i = 0; i < count; ++i) {
uint8_t value = va_arg( args, uint8_t );
result |= SmallIntSetWithValue( value );
}
va_end( args );
return result;
}

42
SmallIntSet.h Normal file
View file

@ -0,0 +1,42 @@
//
// SmallIntSet.h
// JSParser
//
// Created by Sven Weidauer on 06.04.11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#include <stdint.h>
#include <stdbool.h>
#include <limits.h>
#include <assert.h>
#include <unistd.h>
typedef uint64_t SmallIntSet_t;
#define SMALLINTSET_MAX_VALUE ((sizeof(SmallIntSet_t) * CHAR_BIT) - 1)
#define SMALLINTSET_EMPTY ((SmallIntSet_t)0)
#define PURE __attribute__ (( pure ))
PURE static inline SmallIntSet_t SmallIntSetWithValue( uint8_t value )
{
assert( 0 <= value && value <= SMALLINTSET_MAX_VALUE );
return ((SmallIntSet_t)1) << value;
}
PURE static inline SmallIntSet_t SmallIntSetByAddingValue( SmallIntSet_t set, uint8_t value )
{
return set | SmallIntSetWithValue( value );
}
PURE static inline SmallIntSet_t SmallIntSetByRemovingValue( SmallIntSet_t set, uint8_t value )
{
return set & ~SmallIntSetWithValue( value );
}
PURE static inline bool SmallIntSetContainsValue( SmallIntSet_t set, uint8_t value )
{
return (set & SmallIntSetWithValue( value )) != 0;
}
PURE SmallIntSet_t SmallIntSetWithValues( size_t count, ... );