0.2.7 BoolArray

This commit is contained in:
rob tillaart 2023-02-08 17:14:17 +01:00
parent e8e7ccafe1
commit 5e09281cc7
14 changed files with 171 additions and 70 deletions

View File

@ -6,7 +6,7 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: arduino/arduino-lint-action@v1
with:
library-manager: update

View File

@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6

View File

@ -10,7 +10,7 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
with:

View File

@ -1,12 +1,10 @@
//
// FILE: BoolArray.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.6
// VERSION: 0.2.7
// PURPOSE: BoolArray library for Arduino
// URL: https://github.com/RobTillaart/BoolArray.git
// URL: https://github.com/RobTillaart/BoolArray
// http://forum.arduino.cc/index.php?topic=361167
//
// HISTORY: see changelog.md
#include "BoolArray.h"
@ -28,14 +26,53 @@ BoolArray::~BoolArray()
uint8_t BoolArray::begin(const uint16_t size)
{
if (size > BOOLARRAY_MAXSIZE) return BOOLARRAY_SIZE_ERROR;
// if (_size == size) no need to reallocate...
_size = size;
_bytes = (_size + 7) / 8;
if (_array) free(_array);
_array = (byte*) malloc(_bytes);
if (_array)
{
free(_array);
}
_array = (uint8_t *) malloc(_bytes);
return BOOLARRAY_OK;
}
uint16_t BoolArray::size()
{
return _size;
}
uint8_t BoolArray::memory()
{
return _bytes;
}
uint8_t BoolArray::setAll(const uint8_t value)
{
if (_array == NULL) return BOOLARRAY_INIT_ERROR;
uint8_t *p = _array;
uint8_t t = _bytes;
if (value == 0)
{
while(t--) *p++ = 0;
}
else
{
while(t--) *p++ = 0xFF;
}
return BOOLARRAY_OK;
}
uint8_t BoolArray::clear()
{
return setAll(0);
}
uint8_t BoolArray::get(const uint16_t index)
{
if (_array == NULL) return BOOLARRAY_INIT_ERROR;
@ -69,22 +106,5 @@ uint8_t BoolArray::toggle(const uint16_t index)
}
uint8_t BoolArray::setAll(const uint8_t value)
{
if (_array == NULL) return BOOLARRAY_INIT_ERROR;
uint8_t *p = _array;
uint8_t t = _bytes;
if (value == 0)
{
while(t--) *p++ = 0;
}
else
{
while(t--) *p++ = 0xFF;
}
return BOOLARRAY_OK;
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -2,7 +2,7 @@
//
// FILE: BoolArray.h
// AUTHOR: Rob Tillaart
// VERSION: 0.2.6
// VERSION: 0.2.7
// 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.6"))
#define BOOLARRAY_LIB_VERSION (F("0.2.7"))
#define BOOLARRAY_MAXSIZE (250 * 8) // 2000
@ -32,11 +32,11 @@ public:
uint8_t begin(const uint16_t size);
uint16_t size() { return _size; };
uint8_t memory() { return _bytes; };
uint16_t size();
uint8_t memory();
uint8_t setAll(const uint8_t value);
uint8_t clear() { return setAll(0); };
uint8_t clear();
uint8_t get(const uint16_t index);
uint8_t set(const uint16_t index, const uint8_t value);
uint8_t toggle(const uint16_t index);
@ -44,10 +44,10 @@ public:
private:
uint8_t _masks[8] = {1, 2, 4, 8, 16, 32, 64, 128};
uint8_t * _array;
uint16_t _size = 0;
uint16_t _size = 0;
uint8_t _bytes = 0;
};
// -- END OF FILE --
// -- END OF FILE --

View File

@ -6,12 +6,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.2.7] - 2023-02-08
- update readme.md
- update GitHub actions
- update license 2023
- move code to .cpp
- improved measurement of performance - boolArrayDemo2.ino
## [0.2.6] - 2022-10-29
- add RP2040 to build-CI
- add changelog.md
- minor edit unit test
## [0.2.5] - 2021-12-12
- update library.json
- license

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2015-2022 Rob Tillaart
Copyright (c) 2015-2023 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

@ -20,11 +20,22 @@ but BoolArray can store one throw in 1 bit, so 1000 throws in approx 125 bytes.
The class is optimized for storage by packing 8 elements of the array in one byte.
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:
#### Notes
The BoolArray class allocates dynamic memory.
The **BOOLARRAY_MAXSIZE** is set to 2000, this was chosen as **malloc()** can only allocate 255 bytes
in one call on an UNO. This is not checked with the recent versions of the IDE any more.
The library is tested on AVR architecture only.
#### Related
The BitArray library is one from a set of three:
- https://github.com/RobTillaart/BitArray for elements of user defined size in bits (values 0 .. 2^n-1).
- https://github.com/RobTillaart/BoolArray for elements of 1 bit (values 0 .. 1).
- https://github.com/RobTillaart/nibbleArray for elements of 4 bits or smaller (values 0 .. 15).
- **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)
BoolArray is faster than BitArray as it only supports single bits and does not need to merge parts
of different bytes to read/write a value. However BoolArray currently only supports 2000 bits while
@ -33,36 +44,58 @@ BitArray can support more.
## Interface
```cpp
#include "BoolArray.h"
```
#### Constructor
- **BoolArray()** Constructor
- **~BoolArray()** Destructor
- **uint8_t begin(uint16_t size)** dynamically allocates size elements (8 bools in one byte). Returns **BOOLARRAY_OK** on success.
- **uint8_t begin(uint16_t size)** dynamically allocates size elements (8 bools in one byte).
Returns **BOOLARRAY_OK** on success.
#### Meta
- **uint16_t size()** returns number of bool elements.
- **uint16_t memory()** returns number of bytes used.
#### Base
- **uint8_t setAll(uint8_t value)** Sets all elements to false (0) or true (all other values).
- **uint8_t set(uint16_t index, uint8_t value)** Set the element to false (0) or true (all other values).
- **uint8_t clear()** Sets all elements to false.
- **uint8_t get(uint16_t index)** Return 0 or 1 OR an error value which can be interpreted as true.
So one need to check these carefully.
- **uint8_t set(uint16_t index, uint8_t value)** Set the element to false (0) or true (all other values).
- **uint8_t toggle(uint16_t index)** Toggles element at index. Returns **BOOLARRAY_OK** on success.
- **uint8_t clear()** Sets all elements to false.
## Operation
Check out the examples.
## Notes
The BoolArray class allocates dynamic memory.
The **BOOLARRAY_MAXSIZE** is set to 2000, this was chosen as **malloc()** can only allocate 255 bytes
in one call on an UNO. This is not checked with the recent versions of the IDE any more.
The library is tested on AVR architecture only.
## Future
#### Must
- improve documentation
- add performance figures (UNO + ESP32)
#### Should
- performance test on ESP32
- performance for **clear()** dedicated loop vs **setAll(0)** call
- update examples.
- performance intern 16 bit iso 8 bit. (0.3.0)
- faster on UNO
- does allocation work as it should?
#### Could
- **begin()**
- if (_size == size) no need to reallocate...
- update examples.
- boolArray32() class
- begin(uint32_t size);
#### Wont

View File

@ -1,11 +1,9 @@
//
// FILE: boolArrayDemo0.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.2.4
// PURPOSE: demo performance reading boolean array
// DATE: 2015-12-06
// URL: https://forum.arduino.cc/index.php?topic=361167.0
//
// URL: https://github.com/RobTillaart/BoolArray
#include "BoolArray.h"
@ -56,6 +54,7 @@ void test0()
{
Serial.println();
Serial.println("TEST SET(1)");
delay(10);
start = micros();
for (int i = 0; i < BOOLARRAY_MAXSIZE; i++)
@ -65,6 +64,7 @@ void test0()
duration1 = micros() - start;
Serial.print("DURATION:\t");
Serial.println(duration1);
delay(10);
start = micros();
for (int i = 0; i < BOOLARRAY_MAXSIZE; i++)
@ -86,6 +86,7 @@ void test1()
{
Serial.println();
Serial.println("TEST SET(0)");
delay(10);
start = micros();
for (int i = 0; i < BOOLARRAY_MAXSIZE; i++)
@ -95,6 +96,7 @@ void test1()
duration1 = micros() - start;
Serial.print("DURATION:\t");
Serial.println(duration1);
delay(10);
start = micros();
for (int i = 0; i < BOOLARRAY_MAXSIZE; i++)
@ -116,6 +118,7 @@ void test2()
{
Serial.println();
Serial.println("TEST GET(i)");
delay(10);
start = micros();
for (int i = 0; i < BOOLARRAY_MAXSIZE; i++)
@ -125,6 +128,7 @@ void test2()
duration1 = micros() - start;
Serial.print("DURATION:\t");
Serial.println(duration1);
delay(10);
start = micros();
for (int i = 0; i < BOOLARRAY_MAXSIZE; i++)
@ -145,6 +149,7 @@ void test2()
void test3()
{
Serial.println();
delay(10);
start = micros();
for (int i = 0; i < BOOLARRAY_MAXSIZE; i++)
@ -185,4 +190,6 @@ void test3()
Serial.println(1.0 * duration1 / duration2);
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -1,11 +1,9 @@
//
// FILE: boolArrayDemo2.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// PURPOSE: demo performance boolean array
// DATE: 2015-12-12
// URL: https://forum.arduino.cc/index.php?topic=361167.0
//
// URL: https://github.com/RobTillaart/BoolArray
#include "BoolArray.h"
@ -32,6 +30,7 @@ void setup()
Serial.println(b.size());
Serial.println("\nget");
delay(10);
start = micros();
for (int i = 0; i < 1000; i++)
{
@ -40,6 +39,7 @@ void setup()
stop = micros();
Serial.print("DURATION:\t");
Serial.println(stop - start);
delay(10);
start = micros();
for (int i = 0; i < 1000; i++)
@ -54,6 +54,7 @@ void setup()
Serial.println(x);
Serial.println("\nset");
delay(10);
start = micros();
for (int i = 0; i < 1000; i++)
{
@ -62,6 +63,7 @@ void setup()
stop = micros();
Serial.print("DURATION:\t");
Serial.println(stop - start);
delay(10);
start = micros();
for (int i = 0; i < 1000; i++)
@ -74,6 +76,7 @@ void setup()
Serial.println(stop - start);
Serial.println("\nclear");
delay(10);
start = micros();
for (int i = 0; i < 1000; i++)
{
@ -82,6 +85,7 @@ void setup()
stop = micros();
Serial.print("DURATION:\t");
Serial.println(stop - start);
delay(10);
start = micros();
for (int i = 0; i < 1000; i++)
@ -94,6 +98,7 @@ void setup()
Serial.println(stop - start);
Serial.println("\nsetAll");
delay(10);
start = micros();
for (int i = 0; i < 1000; i++)
{
@ -105,8 +110,9 @@ void setup()
for (int i = 0; i < 1000; i++)
{
if (b.get(i) == 0) Serial.println("Error in CLr()");
if (b.get(i) == 0) Serial.println("Error in clear()");
}
delay(10);
start = micros();
for (int i = 0; i < 1000; i++)
@ -119,6 +125,7 @@ void setup()
Serial.println(stop - start);
Serial.println("\ntoggle");
delay(10);
start = micros();
for (int i = 0; i < 1000; i++)
{
@ -127,6 +134,7 @@ void setup()
stop = micros();
Serial.print("DURATION:\t");
Serial.println(stop - start);
delay(10);
start = micros();
for (int i = 0; i < 1000; i++)
@ -147,5 +155,4 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -0,0 +1,24 @@
BOOLARRAY_LIB_VERSION: 0.2.7
Bool array size: 1000
get
DURATION: 5160
DURATION: 9876
X: 0
set
DURATION: 3968
DURATION: 7488
clear
DURATION: 48164
DURATION: 96076
setAll
DURATION: 48164
DURATION: 96076
toggle
DURATION: 3840
DURATION: 7232
Done...

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/BoolArray.git"
},
"version": "0.2.6",
"version": "0.2.7",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=BoolArray
version=0.2.6
version=0.2.7
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

@ -31,6 +31,7 @@ unittest_setup()
fprintf(stderr, "\tVERSION:\t %s\n", (char *) BOOLARRAY_LIB_VERSION);
}
unittest_teardown()
{
}
@ -132,4 +133,6 @@ unittest(test_clear)
unittest_main()
// --------
// -- END OF FILE --