Created
June 15, 2014 12:40
-
-
Save sealfin/f6eeaac620c8a74b4b0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "bigBooleanArray.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
static long g_maximum_index; | |
static long g_number_of_bytes; | |
static char *g_byte = NULL; | |
bool f_BigBooleanArray_Initialised( void ) | |
{ | |
return g_byte != NULL; | |
} | |
void p_BigBooleanArray_New( const long p ) | |
{ | |
if( g_byte != NULL ) | |
{ | |
fprintf( stderr, "p_BigBooleanArray_New - f_BigBooleanArray_Initialised() == true.\n" ); | |
abort(); | |
} | |
if( p <= 0 ) | |
{ | |
fprintf( stderr, "p_BigBooleanArray_New - p ≤ 0.\n" ); | |
abort(); | |
} | |
g_maximum_index = p; /* Indices begin at 1. */ | |
g_number_of_bytes = p / 8; | |
if( p % 8 > 0 ) | |
g_number_of_bytes ++; | |
g_byte = ( char* )malloc( sizeof( char ) * g_number_of_bytes ); | |
} | |
long f_BigBooleanArray_MaximumIndex( void ) | |
{ | |
if( g_byte == NULL ) | |
{ | |
fprintf( stderr, "f_BigBooleanArray_MaximumIndex - f_BigBooleanArray_Initialised() == false.\n" ); | |
abort(); | |
} | |
return g_maximum_index; | |
} | |
void p_BigBooleanArray_Delete( void ) | |
{ | |
if( g_byte != NULL ) | |
{ | |
free( g_byte ); | |
g_byte = NULL; | |
} | |
} | |
void p_BigBooleanArray_SetAll( const bool p ) | |
{ | |
if( g_byte == NULL ) | |
{ | |
fprintf( stderr, "p_BigBooleanArray_SetAll - f_BigBooleanArray_Initialised() == false.\n" ); | |
abort(); | |
} | |
{ | |
long i = 0; | |
const char value = ( p )?255:0; | |
for( ; i < g_number_of_bytes; i ++ ) | |
g_byte[ i ] = value; | |
} | |
} | |
#define M_BYTE_AND_BIT( p ) \ | |
long byte; \ | |
short bit; \ | |
const short bit_mask[ 8 ] = { 1, 2, 4, 8, 16, 32, 64, 128 }; \ | |
\ | |
byte = p / 8; \ | |
if( p % 8 == 0 ) \ | |
byte --; \ | |
\ | |
bit = p % 8; \ | |
if( bit == 0 ) \ | |
bit = 8; \ | |
bit --; /* bit_mask index. */ | |
void p_BigBooleanArray_Set( const long p_index, const bool p_value ) | |
{ | |
if( g_byte == NULL ) | |
{ | |
fprintf( stderr, "p_BigBooleanArray_Set - f_BigBooleanArray_Initialised() == false.\n" ); | |
abort(); | |
} | |
if(( p_index <= 0 ) || ( p_index > g_maximum_index )) | |
{ | |
fprintf( stderr, "f_BigBooleanArray_Set - p_index ≤ 0 || p_index > f_BigBooleanArray_MaximumIndex().\n" ); | |
abort(); | |
} | |
{ | |
char c; | |
M_BYTE_AND_BIT( p_index ) | |
c = g_byte[ byte ]; | |
if( p_value ) | |
c = c | bit_mask[ bit ]; | |
else | |
{ | |
char m = 255 & bit_mask[ bit ]; | |
m = m ^ 255; | |
c = c & m; | |
} | |
g_byte[ byte ] = c; | |
} | |
} | |
bool f_BigBooleanArray_Get( const long p ) | |
{ | |
if( g_byte == NULL ) | |
{ | |
fprintf( stderr, "p_BigBooleanArray_Get - f_BigBooleanArray_Initialised() == false.\n" ); | |
abort(); | |
} | |
if(( p <= 0 ) || ( p > g_maximum_index )) | |
{ | |
fprintf( stderr, "f_BigBooleanArray_Get - p ≤ 0 || p > f_BigBooleanArray_MaximumIndex().\n" ); | |
abort(); | |
} | |
{ | |
M_BYTE_AND_BIT( p ) | |
return g_byte[ byte ] & bit_mask[ bit ]; | |
} | |
} | |
#undef M_BYTE_AND_BIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* After a *lot* of swearing, I've determined that Leonardo IDE doesn't properly support hexadecimal literals or bitwise operators. Which is *awesome* in an IDE intended to help teach programming/C. */ | |
#ifndef bigBooleanArray_h | |
#define bigBooleanArray_h | |
#ifdef __cplusplus | |
extern "C" | |
{ | |
#endif | |
#include "seal_bool.h" | |
bool f_BigBooleanArray_Initialised( void ); | |
void p_BigBooleanArray_New( const long p ); | |
long f_BigBooleanArray_MaximumIndex( void ); | |
void p_BigBooleanArray_Delete( void ); | |
void p_BigBooleanArray_SetAll( const bool p ); | |
void p_BigBooleanArray_Set( const long p_index, const bool p_value ); | |
bool f_BigBooleanArray_Get( const long p ); | |
#ifdef __cplusplus | |
} | |
#endif | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment