0.2.5 BitArray

This commit is contained in:
rob tillaart 2022-10-29 15:54:56 +02:00
parent f4128bd493
commit 6115e2c382
10 changed files with 139 additions and 244 deletions

View File

@ -1,3 +1,18 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:
packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
@ -7,5 +22,8 @@ compile:
# - leonardo
- m4
- esp32
# - esp8266
# - mega2560
- esp8266
# - mega2560
- rpipico
libraries:
- "printHelpers"

View File

@ -1,27 +1,12 @@
//
// FILE: BitArray.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.4
// VERSION: 0.2.5
// PURPOSE: BitArray library for Arduino
// URL: https://github.com/RobTillaart/BitArray
// http://forum.arduino.cc/index.php?topic=361167
//
// HISTORY
// 0.1.00 initial version
// 0.1.01 added clear() + fixed set bug
// 0.1.02 first stable version (at last)
// 0.1.03 refactoring
// 0.1.04 improve performance
// 0.1.05 added upper limits
// 0.1.06 refactored
// 0.1.07 private calls inline -> performance & footprint
// 0.1.8 added toggle
// 0.1.9 fix constructor bug
// 0.2.0 2020-03-28 #pragma once, readme, fix Fibonacci demo
// 0.2.1 2020-06-05 fix library.json
// 0.2.2 2020-12-14 add Arduino-CI + unit test
// 0.2.3 2021-10-19 update Arduino-CI + setAll(value)
// 0.2.4 2021-12-14 update library.json, license, minor edits
// HISTORY: see changelog.md
#include "BitArray.h"
@ -85,11 +70,11 @@ uint8_t BitArray::begin(const uint8_t bits, const uint16_t size)
uint32_t BitArray::get(const uint16_t index)
{
// if (_error != BA_OK) return BA_ERR;
// if (index >= _size) return BA_IDX_RANGE;
// if (_error != BA_OK) return BA_ERR;
// if (index >= _size) return BA_IDX_RANGE;
uint32_t v = 0;
uint16_t pos = index * _bits;
for (uint8_t i = _bits; i-- > 0;)
{
v <<= 1;
@ -101,8 +86,8 @@ uint32_t BitArray::get(const uint16_t index)
uint32_t BitArray::set(const uint16_t index, uint32_t value)
{
// if (_error != BA_OK) return BA_ERR;
// if (index >= _size) return BA_IDX_RANGE;
// if (_error != BA_OK) return BA_ERR;
// if (index >= _size) return BA_IDX_RANGE;
uint16_t pos = index * _bits;
uint32_t mask = 1UL;
for (uint8_t i = 0; i < _bits; i++)
@ -117,8 +102,8 @@ uint32_t BitArray::set(const uint16_t index, uint32_t value)
uint32_t BitArray::toggle(const uint16_t index)
{
// if (_error != BA_OK) return BA_ERR;
// if (index >= _size) return BA_IDX_RANGE;
// if (_error != BA_OK) return BA_ERR;
// if (index >= _size) return BA_IDX_RANGE;
uint32_t v = 0;
uint16_t pos = index * _bits;
for (uint8_t i = _bits; i-- > 0;)
@ -161,7 +146,7 @@ void BitArray::setAll(uint32_t value)
// 16 bit address usage is faster
// TODO verify correctness
// TODO verify correctness
//
// void BitArray::clear()
// {
@ -172,7 +157,7 @@ void BitArray::setAll(uint32_t value)
// uint16_t *p = (uint16_t*)q;
// if (p)
// {
// for (uint8_t t = 0; t < BA_SEGMENT_SIZE/2; t++)
// for (uint8_t t = 0; t < BA_SEGMENT_SIZE/2; t++)
// {
// *p++ = 0; // might be bug @ edge..
// }
@ -181,13 +166,15 @@ void BitArray::setAll(uint32_t value)
// }
// }
// PRIVATE
/////////////////////////////////////////////////
//
// PRIVATE
//
inline uint8_t BitArray::_bitget(uint16_t pos)
{
uint8_t se = 0;
uint16_t re = pos;
while (re >= (BA_SEGMENT_SIZE * 8)) // 8 == #bits in uint8_t
while (re >= (BA_SEGMENT_SIZE * 8)) // 8 == #bits in uint8_t
{
se++;
re -= (BA_SEGMENT_SIZE * 8);
@ -195,8 +182,8 @@ inline uint8_t BitArray::_bitget(uint16_t pos)
uint8_t by = re / 8;
uint8_t bi = re & 7;
uint8_t * p = _ar[se];
return (p[by] >> bi) & 0x01; // bitRead(p[by], bi);
return (p[by] >> bi) & 0x01; // bitRead(p[by], bi);
}
@ -204,7 +191,7 @@ inline void BitArray::_bitset(uint16_t pos, uint8_t value)
{
uint8_t se = 0;
uint16_t re = pos;
while (re >= (BA_SEGMENT_SIZE * 8)) // 8 == #bits in uint8_t
while (re >= (BA_SEGMENT_SIZE * 8)) // 8 == #bits in uint8_t
{
se++;
re -= (BA_SEGMENT_SIZE * 8);
@ -212,9 +199,9 @@ inline void BitArray::_bitset(uint16_t pos, uint8_t value)
uint8_t by = re / 8;
uint8_t bi = re & 7;
uint8_t * p = _ar[se];
if (value == 0) p[by] &= ~(1 << bi); // bitClear(p[by], bi);
else p[by] |= (1 << bi); // bitSet(p[by], bi);
if (value == 0) p[by] &= ~(1 << bi); // bitClear(p[by], bi);
else p[by] |= (1 << bi); // bitSet(p[by], bi);
}
@ -222,7 +209,7 @@ inline uint8_t BitArray::_bittoggle(const uint16_t pos)
{
uint8_t se = 0;
uint16_t re = pos;
while (re >= (BA_SEGMENT_SIZE * 8)) // 8 == #bits in uint8_t
while (re >= (BA_SEGMENT_SIZE * 8)) // 8 == #bits in uint8_t
{
se++;
re -= (BA_SEGMENT_SIZE * 8);
@ -230,7 +217,7 @@ inline uint8_t BitArray::_bittoggle(const uint16_t pos)
uint8_t by = re / 8;
uint8_t bi = re & 7;
uint8_t * p = _ar[se];
uint8_t mask = 1 << bi;
p[by] ^= mask;
return (mask > 0);
@ -238,3 +225,4 @@ inline uint8_t BitArray::_bittoggle(const uint16_t pos)
// -- END OF FILE --

View File

@ -1,56 +1,54 @@
#pragma once
//
// FILE: bitArray.h
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.2.4
// AUTHOR: Rob Tillaart
// VERSION: 0.2.5
// PURPOSE: BitArray library for Arduino
// URL: https://github.com/RobTillaart/BitArray
// BitArray allows you to make a compact array of objects with a size
// expressed in bits. typically 1..10.
// The interface uses uint32_t as that will be enough for most purposes.
// The main requirement is to optimize storage space.
//
// The BitArray uses an array of segments and the space per segment
// may not exceed 256 bytes as this is a limit on some processors.
//
// Originally created to store lot of numbers between 1..6 dice rolls
// the storage is also usable to store e.g. raw 10 bit analogRead()'s.
// BitArray allows you to make a compact array of objects with a size
// expressed in bits. typically 1..10.
// The interface uses uint32_t as that will be enough for most purposes.
// The main requirement is to optimize storage space.
//
// The BitArray uses an array of segments and the space per segment
// may not exceed 256 bytes as this is a limit on some processors.
//
// Originally created to store lot of numbers between 1..6 dice rolls
// the storage is also usable to store e.g. raw 10 bit analogRead()'s.
//
#include "Arduino.h"
#define BITARRAY_LIB_VERSION (F("0.2.4"))
#define BITARRAY_LIB_VERSION (F("0.2.5"))
#define BA_SEGMENT_SIZE 200
// max memory is board type dependent
// note the bitArray does not use all of the RAM
// 1K - max 600
// max memory is board type dependent
// note the bitArray does not use all of the RAM
// 1K - max 600
#if defined(__AVR_ATmega168__)
#define BA_MAX_SEGMENTS 3
// 2K - max 1600
// 2K - max 1600
#elif defined(__AVR_ATmega328P__)
#define BA_MAX_SEGMENTS 8
// 8K - max 7000
// 8K - max 7000
#elif defined(__AVR_ATmega1280__)
#define BA_MAX_SEGMENTS 35
// 8K - max 7000
// 8K - max 7000
#elif defined(__AVR_ATmega2560__)
#define BA_MAX_SEGMENTS 35
// 1.25K - max 800
// 1.25K - max 800
#elif defined(__AVR_ATmega16U4__)
#define BA_MAX_SEGMENTS 4
// 2.5K - max 2000
// 2.5K - max 2000
#elif defined(__AVR_ATmega32U4__)
#define BA_MAX_SEGMENTS 10
// 96K (64 + 32) DUE...
// 96K (64 + 32) DUE...
#elif defined(__SAM3X8E__)
#define BA_MAX_SEGMENTS 100
// default max 1000
// default max 1000
#else
#define BA_MAX_SEGMENTS 5
#endif
@ -95,4 +93,6 @@ private:
uint8_t _error = BA_NO_MEMORY_ERR;
};
// -- END OF FILE --

View File

@ -0,0 +1,63 @@
# Change Log BitArray
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.3.5] - 2022-10-29
- add changelog.md
- add RP2040 in build-CI
## [0.2.4] - 2021-12-14
- update library.json
- update license
- minor edits
## [0.2.3] - 2021-10-19
- update Arduino-CI
- add setAll(value)
## [0.2.2] - 2020-12-14
- add Arduino-CI + unit test
## [0.2.1] - 2020-06-05
- fix library.json
## [0.2.0] - 2020-03-28
- add #pragma once
- update readme
- fix Fibonacci demo
----
## [0.1.9]
- fix constructor bug
## [0.1.8]
- added toggle
## [0.1.07]
- private calls inline -> performance & footprint
## [0.1.06]
- refactored
## [0.1.05]
- added upper limits
## [0.1.04]
- improve performance
## [0.1.03]
- refactoring
## [0.1.02]
- first stable version (at last)
## [0.1.01]
- added clear() + fixed set bug
## [0.1.00]
- initial version

View File

@ -63,4 +63,6 @@ The library is tested on AVR architecture only.
- testing.
- functional examples.
- investigate element size of 64 (for doubles) and beyond.
- move code to .cpp

View File

@ -1,86 +0,0 @@
Start C:\Users\Rob\Desktop\WORK\Arduino\libraries\BitArray\examples\bitArrayDemo3\bitArrayDemo3.ino
LIB VERSION: 0.1.8
CAPACITY: 10000
MEMORY: 1250
BITS: 1
SEGMENTS: 7
GET:
DURATION: 113396
DURATION: 222296
X: 6792
SET:
DURATION: 84468
DURATION: 164444
CLEAR:
DURATION: 548
TOGGLE:
DURATION: 105244
DURATION: 205948
Done...
CAPACITY: 5000
MEMORY: 1250
BITS: 2
SEGMENTS: 7
GET:
DURATION: 89184
DURATION: 176084
X: 51288
SET:
DURATION: 73152
DURATION: 144008
CLEAR:
DURATION: 548
TOGGLE:
DURATION: 86064
DURATION: 169796
Done...
CAPACITY: 3333
MEMORY: 1250
BITS: 3
SEGMENTS: 7
GET:
DURATION: 81104
DURATION: 160656
X: 120501
SET:
DURATION: 69368
DURATION: 137184
CLEAR:
DURATION: 548
TOGGLE:
DURATION: 79660
DURATION: 157720
Done...
CAPACITY: 2500
MEMORY: 1250
BITS: 4
SEGMENTS: 7
GET:
DURATION: 77084
DURATION: 152972
X: 231717
SET:
DURATION: 67492
DURATION: 133792
CLEAR:
DURATION: 552
TOGGLE:
DURATION: 76472
DURATION: 151712
Done...

View File

@ -1,86 +0,0 @@
Start C:\Users\Rob\Desktop\WORK\Arduino\libraries\BitArray\examples\bitArrayDemo3\bitArrayDemo3.ino
LIB VERSION: 0.1.8
CAPACITY: 10000
MEMORY: 1250
BITS: 1
SEGMENTS: 7
GET:
DURATION: 113396
DURATION: 222296
X: 6792
SET:
DURATION: 84468
DURATION: 164444
CLEAR:
DURATION: 548
TOGGLE:
DURATION: 105244
DURATION: 205948
Done...
CAPACITY: 5000
MEMORY: 1250
BITS: 2
SEGMENTS: 7
GET:
DURATION: 89184
DURATION: 176084
X: 51288
SET:
DURATION: 73152
DURATION: 144008
CLEAR:
DURATION: 548
TOGGLE:
DURATION: 86064
DURATION: 169796
Done...
CAPACITY: 3333
MEMORY: 1250
BITS: 3
SEGMENTS: 7
GET:
DURATION: 81104
DURATION: 160656
X: 120501
SET:
DURATION: 69368
DURATION: 137184
CLEAR:
DURATION: 548
TOGGLE:
DURATION: 79660
DURATION: 157720
Done...
CAPACITY: 2500
MEMORY: 1250
BITS: 4
SEGMENTS: 7
GET:
DURATION: 77084
DURATION: 152972
X: 231717
SET:
DURATION: 67492
DURATION: 133792
CLEAR:
DURATION: 552
TOGGLE:
DURATION: 76472
DURATION: 151712
Done...

View File

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

View File

@ -1,5 +1,5 @@
name=BitArray
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 objects with a size expressed in bits.

View File

@ -27,6 +27,7 @@
unittest_setup()
{
fprintf(stderr, "BITARRAY_LIB_VERSION: %s\n", (char *) BITARRAY_LIB_VERSION);
}
@ -40,8 +41,6 @@ unittest(test_constructor)
{
BitArray ba;
fprintf(stderr, "BITARRAY_LIB_VERSION: %s\n", (char *) BITARRAY_LIB_VERSION);
assertEqual(BA_NO_MEMORY_ERR, ba.getError());
ba.begin(0, 1000);
@ -62,7 +61,6 @@ unittest(test_set_get_toggle)
{
BitArray ba;
fprintf(stderr, "BITARRAY_LIB_VERSION: %s\n", (char *) BITARRAY_LIB_VERSION);
ba.begin(1, 1000);
assertEqual(BA_OK, ba.getError());
@ -89,7 +87,7 @@ unittest(test_set_get_toggle)
sum += ba.get(i);
}
assertEqual(1000, sum);
fprintf(stderr, "\t1000x toggle(i)\n");
sum = 0;
for (int i = 0; i < ba.capacity(); i++)
@ -108,7 +106,6 @@ unittest(test_clear)
{
BitArray ba;
fprintf(stderr, "BITARRAY_LIB_VERSION: %s\n", (char *) BITARRAY_LIB_VERSION);
ba.begin(1, 1000);
assertEqual(BA_OK, ba.getError());
@ -139,7 +136,6 @@ unittest(test_setAll)
{
BitArray ba;
fprintf(stderr, "BITARRAY_LIB_VERSION: %s\n", (char *) BITARRAY_LIB_VERSION);
ba.begin(5, 200);
assertEqual(BA_OK, ba.getError());