diff --git a/SmallIntSet.c b/SmallIntSet.c new file mode 100644 index 0000000..a5c22bd --- /dev/null +++ b/SmallIntSet.c @@ -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 + +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; +} \ No newline at end of file diff --git a/SmallIntSet.h b/SmallIntSet.h new file mode 100644 index 0000000..899c64e --- /dev/null +++ b/SmallIntSet.h @@ -0,0 +1,42 @@ +// +// SmallIntSet.h +// JSParser +// +// Created by Sven Weidauer on 06.04.11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#include +#include +#include +#include +#include + +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, ... ); \ No newline at end of file