0.2.5 BoolArray

This commit is contained in:
rob tillaart 2021-12-14 14:48:42 +01:00
parent 9648c886cb
commit 88b5876093
10 changed files with 72 additions and 30 deletions

View File

@ -1,7 +1,7 @@
//
// FILE: BoolArray.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.4
// VERSION: 0.2.5
// PURPOSE: BoolArray library for Arduino
// URL: https://github.com/RobTillaart/BoolArray.git
// http://forum.arduino.cc/index.php?topic=361167
@ -9,7 +9,7 @@
// HISTORY
// 0.1.00 initial version
// 0.1.01 fixed constructor - Thanks WPD64 + error handling
// 0.1.02 added errorhandling
// 0.1.02 added error handling
// 0.1.3 added toggle
// 0.1.4 2017-07-16 added masks for performance
// 0.2.0 2020-03-29 #pragma, readme.md,
@ -17,7 +17,7 @@
// 0.2.2 2020-12-15 add arduino-CI + unit tests
// 0.2.3 2021-01-19 update readme
// 0.2.4 2021-10-19 update build-CI + badges
//
// 0.2.5 2021-12-12 update library.json, license, minor edits
#include "BoolArray.h"
@ -25,14 +25,14 @@
BoolArray::BoolArray()
{
_ar = NULL;
_array = NULL;
_size = 0;
}
BoolArray::~BoolArray()
{
if (_ar) free(_ar);
if (_array) free(_array);
}
@ -41,49 +41,49 @@ uint8_t BoolArray::begin(const uint16_t size)
if (size > BOOLARRAY_MAXSIZE) return BOOLARRAY_SIZE_ERROR;
_size = size;
_bytes = (_size + 7) / 8;
if (_ar) free(_ar);
_ar = (byte*) malloc(_bytes);
if (_array) free(_array);
_array = (byte*) malloc(_bytes);
return BOOLARRAY_OK;
}
uint8_t BoolArray::get(const uint16_t index)
{
if (_ar == NULL) return BOOLARRAY_INIT_ERROR;
if (_array == NULL) return BOOLARRAY_INIT_ERROR;
if (index >= _size) return BOOLARRAY_SIZE_ERROR;
uint8_t by = index / 8;
uint8_t bi = index & 7;
return (_ar[by] & masks[bi]) > 0;
return (_array[by] & _masks[bi]) > 0;
}
uint8_t BoolArray::set(const uint16_t index, const uint8_t value)
{
if (_ar == NULL) return BOOLARRAY_INIT_ERROR;
if (_array == NULL) return BOOLARRAY_INIT_ERROR;
if (index >= _size) return BOOLARRAY_SIZE_ERROR;
uint8_t by = index / 8;
uint8_t bi = index & 7;
if (value == 0) _ar[by] &= ~masks[bi];
else _ar[by] |= masks[bi];
if (value == 0) _array[by] &= ~_masks[bi];
else _array[by] |= _masks[bi];
return BOOLARRAY_OK;
}
uint8_t BoolArray::toggle(const uint16_t index)
{
if (_ar == NULL) return BOOLARRAY_INIT_ERROR;
if (_array == NULL) return BOOLARRAY_INIT_ERROR;
if (index >= _size) return BOOLARRAY_SIZE_ERROR;
uint8_t by = index / 8;
uint8_t bi = index & 7;
_ar[by] ^= masks[bi];
_array[by] ^= _masks[bi];
return BOOLARRAY_OK;
}
uint8_t BoolArray::setAll(const uint8_t value)
{
if (_ar == NULL) return BOOLARRAY_INIT_ERROR;
uint8_t *p = _ar;
if (_array == NULL) return BOOLARRAY_INIT_ERROR;
uint8_t *p = _array;
uint8_t t = _bytes;
if (value == 0)
{

View File

@ -2,7 +2,7 @@
//
// FILE: BoolArray.h
// AUTHOR: Rob Tillaart
// VERSION: 0.2.4
// VERSION: 0.2.5
// PURPOSE: BoolArray library for Arduino
// URL: https://github.com/RobTillaart/BoolArray.git
@ -14,7 +14,7 @@
#include "Arduino.h"
#define BOOLARRAY_LIB_VERSION (F("0.2.4"))
#define BOOLARRAY_LIB_VERSION (F("0.2.5"))
#define BOOLARRAY_MAXSIZE (250 * 8) // 2000
@ -42,8 +42,8 @@ public:
uint8_t toggle(const uint16_t index);
private:
uint8_t masks[8] = {1, 2, 4, 8, 16, 32, 64, 128};
uint8_t * _ar;
uint8_t _masks[8] = {1, 2, 4, 8, 16, 32, 64, 128};
uint8_t * _array;
uint16_t _size = 0;
uint8_t _bytes = 0;
};

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2015-2021 Rob Tillaart
Copyright (c) 2015-2022 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -21,6 +21,7 @@ The class is optimized for storage by packing 8 elements of the array in one byt
You need to check if your application needs more performance than this library can deliver.
The BoolArray library is one from a set of three:
- **BitArray** for elements of user defined size in bits (values 0 .. 2^n-1)
- **BoolArray** for elements of 1 bit (values 0 .. 1)
- **nybbleArray** for elements of 4 bits (values 0 .. 15)

View File

@ -0,0 +1,27 @@
LIB VERSION: 0.2.3
SIZE: 2000
SET TEST0
DURATION: 8924
DURATION: 16060
7136
X: 0
SET TEST0
DURATION: 8488
DURATION: 15556
7068
X: 0
GET TEST
DURATION: 10620
DURATION: 19848
9228
X: 0
SET TEST
DURATION: 97628
DURATION: 113544
15916
X: 0
Done...

View File

@ -6,8 +6,6 @@
// DATE: 2015-12-12
// URL: https://forum.arduino.cc/index.php?topic=361167.0
//
// 0.1.1 - added performance for toggle
//
#include "BoolArray.h"
@ -148,4 +146,6 @@ void loop()
{
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -1,4 +1,3 @@
Start D:\Rob\WORK\Arduino\libraries\BoolArray\examples\boolArrayDemo2\boolArrayDemo2.ino
BOOLARRAY_LIB_VERSION: 0.2.0
Bool array size: 1000

View File

@ -15,8 +15,9 @@
"type": "git",
"url": "https://github.com/RobTillaart/BoolArray.git"
},
"version": "0.2.4",
"version": "0.2.5",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*"
"platforms": "*",
"headers": "BoolArray.h"
}

View File

@ -1,5 +1,5 @@
name=BoolArray
version=0.2.4
version=0.2.5
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for compact array of booleans of max size 2000 (UNO).

View File

@ -35,14 +35,28 @@ unittest_teardown()
}
unittest(test_constants)
{
assertEqual(2000, BOOLARRAY_MAXSIZE );
assertEqual(0x00, BOOLARRAY_OK );
assertEqual(0xFF, BOOLARRAY_ERROR );
assertEqual(0xFE, BOOLARRAY_SIZE_ERROR);
assertEqual(0xFD, BOOLARRAY_INIT_ERROR);
}
unittest(test_constructor)
{
fprintf(stderr, "\tVERSION:\t %s\n", (char *) BOOLARRAY_LIB_VERSION);
BoolArray ba;
assertEqual(0, ba.size());
ba.begin(1000);
assertEqual(1000, ba.size());
fprintf(stderr, "\tVERSION:\t %s\n", BOOLARRAY_LIB_VERSION);
assertEqual(125, ba.memory());
ba.begin(100);
assertEqual(100, ba.size());
assertEqual(13, ba.memory());
}