0.2.8 BitArray

This commit is contained in:
Rob Tillaart 2024-04-09 16:45:31 +02:00
parent d072dafec9
commit c160daa034
16 changed files with 141 additions and 141 deletions

View File

@ -1,4 +1,5 @@
# These are supported funding model platforms
github: RobTillaart
github: RobTillaart
custom: "https://www.paypal.me/robtillaart"

View File

@ -1,12 +1,12 @@
name: Arduino-lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: arduino/arduino-lint-action@v1
with:
library-manager: update

View File

@ -1,4 +1,3 @@
---
name: Arduino CI
on: [push, pull_request]
@ -6,9 +5,10 @@ on: [push, pull_request]
jobs:
runTest:
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6

View File

@ -9,10 +9,11 @@ on:
jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
uses: limitusus/json-syntax-check@v2
with:
pattern: "\\.json$"

View File

@ -1,7 +1,7 @@
//
// FILE: BitArray.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.7
// VERSION: 0.2.8
// PURPOSE: BitArray library for Arduino
// URL: https://github.com/RobTillaart/BitArray
// http://forum.arduino.cc/index.php?topic=361167
@ -12,124 +12,124 @@
BitArray::BitArray()
{
for (uint8_t i = 0; i < BA_MAX_SEGMENTS; i++)
{
_ar[i] = NULL;
}
for (uint8_t i = 0; i < BA_MAX_SEGMENTS; i++)
{
_ar[i] = NULL;
}
}
BitArray::~BitArray()
{
for (uint8_t i = 0; i < BA_MAX_SEGMENTS; i++)
{
if (_ar[i]) free(_ar[i]);
}
for (uint8_t i = 0; i < BA_MAX_SEGMENTS; i++)
{
if (_ar[i]) free(_ar[i]);
}
}
uint8_t BitArray::begin(const uint8_t bits, const uint16_t size)
{
if ((bits == 0) || (bits > 32))
{
_error = BA_ELEMENT_SIZE_ERR;
return _error;
}
if (((1UL * bits * size)/8) > (1UL * BA_MAX_SEGMENTS * BA_SEGMENT_SIZE))
{
_error = BA_SIZE_ERR;
return _error;
}
for (uint8_t i = 0; i < BA_MAX_SEGMENTS; i++)
{
if (_ar[i]) free(_ar[i]);
}
_segments = 0;
_bits = bits;
_bytes = (_bits * size + 7) / 8;
uint16_t b = _bytes;
while (b > 0)
{
_ar[_segments] = (uint8_t*) malloc(min(b, (uint16_t) BA_SEGMENT_SIZE));
if (_ar[_segments] == NULL)
{
_error = BA_NO_MEMORY_ERR;
return _error;
}
b = b - min(b, (uint16_t) BA_SEGMENT_SIZE);
_segments++;
}
_error = BA_OK;
if ((bits == 0) || (bits > 32))
{
_error = BA_ELEMENT_SIZE_ERR;
return _error;
}
if (((1UL * bits * size)/8) > (1UL * BA_MAX_SEGMENTS * BA_SEGMENT_SIZE))
{
_error = BA_SIZE_ERR;
return _error;
}
for (uint8_t i = 0; i < BA_MAX_SEGMENTS; i++)
{
if (_ar[i]) free(_ar[i]);
}
_segments = 0;
_bits = bits;
_bytes = (_bits * size + 7) / 8;
uint16_t b = _bytes;
while (b > 0)
{
_ar[_segments] = (uint8_t*) malloc(min(b, (uint16_t) BA_SEGMENT_SIZE));
if (_ar[_segments] == NULL)
{
_error = BA_NO_MEMORY_ERR;
return _error;
}
b = b - min(b, (uint16_t) BA_SEGMENT_SIZE);
_segments++;
}
_error = BA_OK;
return _error;
}
uint32_t BitArray::get(const uint16_t index)
{
// if (_error != BA_OK) return BA_ERR;
// if (index >= _size) return BA_IDX_RANGE;
uint32_t v = 0;
uint16_t pos = index * _bits;
// 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;
v += _bitget(pos + i);
}
return v;
for (uint8_t i = _bits; i-- > 0;)
{
v <<= 1;
v += _bitget(pos + i);
}
return v;
}
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;
uint16_t pos = index * _bits;
uint32_t mask = 1UL;
for (uint8_t i = 0; i < _bits; i++)
{
uint8_t v = (value & mask) > 0 ? 1 : 0;
_bitset(pos + i, v);
mask <<= 1;
}
return value;
// 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++)
{
uint8_t v = (value & mask) > 0 ? 1 : 0;
_bitset(pos + i, v);
mask <<= 1;
}
return value;
}
uint32_t BitArray::toggle(const uint16_t index)
{
// 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;
v += _bittoggle(pos + i);
}
return v;
// 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;
v += _bittoggle(pos + i);
}
return v;
}
void BitArray::clear()
{
uint16_t b = _bytes;
for (uint8_t s = 0; s < _segments; s++)
uint16_t b = _bytes;
for (uint8_t s = 0; s < _segments; s++)
{
uint8_t *p = _ar[s];
if (p)
{
uint8_t *p = _ar[s];
if (p)
{
uint8_t t = min(b, (uint16_t) BA_SEGMENT_SIZE);
b -= t;
while(t--)
{
*p++ = 0;
}
}
if (b == 0) break;
uint8_t t = min(b, (uint16_t) BA_SEGMENT_SIZE);
b -= t;
while(t--)
{
*p++ = 0;
}
}
if (b == 0) break;
}
}
@ -171,56 +171,56 @@ void BitArray::setAll(uint32_t value)
//
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
{
se++;
re -= (BA_SEGMENT_SIZE * 8);
}
uint8_t by = re / 8;
uint8_t bi = re & 7;
uint8_t * p = _ar[se];
uint8_t se = 0;
uint16_t re = pos;
while (re >= (BA_SEGMENT_SIZE * 8)) // 8 == #bits in uint8_t
{
se++;
re -= (BA_SEGMENT_SIZE * 8);
}
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);
}
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
{
se++;
re -= (BA_SEGMENT_SIZE * 8);
}
uint8_t by = re / 8;
uint8_t bi = re & 7;
uint8_t * p = _ar[se];
uint8_t se = 0;
uint16_t re = pos;
while (re >= (BA_SEGMENT_SIZE * 8)) // 8 == #bits in uint8_t
{
se++;
re -= (BA_SEGMENT_SIZE * 8);
}
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);
}
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
{
se++;
re -= (BA_SEGMENT_SIZE * 8);
}
uint8_t by = re / 8;
uint8_t bi = re & 7;
uint8_t * p = _ar[se];
uint8_t se = 0;
uint16_t re = pos;
while (re >= (BA_SEGMENT_SIZE * 8)) // 8 == #bits in uint8_t
{
se++;
re -= (BA_SEGMENT_SIZE * 8);
}
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);
// return ((p[by] & mask) > 0); 0.3.0
uint8_t mask = 1 << bi;
p[by] ^= mask;
return (mask > 0);
// return ((p[by] & mask) > 0); 0.3.0
}

View File

@ -2,7 +2,7 @@
//
// FILE: bitArray.h
// AUTHOR: Rob Tillaart
// VERSION: 0.2.7
// VERSION: 0.2.8
// PURPOSE: BitArray library for Arduino
// URL: https://github.com/RobTillaart/BitArray
@ -20,7 +20,7 @@
#include "Arduino.h"
#define BITARRAY_LIB_VERSION (F("0.2.7"))
#define BITARRAY_LIB_VERSION (F("0.2.8"))
#define BA_SEGMENT_SIZE 200

View File

@ -6,10 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.2.8] - 2024-04-09
- update GitHub actions
- minor edits
## [0.2.7] - 2023-10-18
- update readme.md
## [0.2.6] - 2023-02-08
- update readme.md
- fix changelog

View File

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

@ -2,7 +2,6 @@
// FILE: bitArrayDemo0.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo - store 2000 dice rolls
// DATE: 2015-11-14
// URL: https://github.com/RobTillaart/BitArray

View File

@ -2,7 +2,6 @@
// FILE: bitArrayDemo1.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// DATE: 14-11-2015
// URL: https://github.com/RobTillaart/BitArray

View File

@ -2,7 +2,6 @@
// FILE: bitArrayDemo2.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo performance bit array
// DATE: 2015-12-06
// URL: https://github.com/RobTillaart/BitArray
// URL: https://forum.arduino.cc/index.php?topic=361167.0

View File

@ -2,7 +2,6 @@
// FILE: bitArrayDemo3.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo performance bit array
// DATE: 2017-07-15
// URL: https://github.com/RobTillaart/BitArray
// URL: https://forum.arduino.cc/index.php?topic=361167.0

View File

@ -2,7 +2,6 @@
// FILE: bitArray_toggle.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo performance toggle bitarray
// DATE: 2017-07-15
// URL: https://github.com/RobTillaart/BitArray
// URL: https://forum.arduino.cc/index.php?topic=361167.0

View File

@ -1,6 +1,5 @@
// FILE: Fibonacci.ino
// AUTHOR: Rob Tillaart
// DATE: 2018-03-05
// PURPOSE: Generate Fibonacci numbers with bit array
// URL: https://github.com/RobTillaart/BitArray
// URL: https://forum.arduino.cc/index.php?topic=532760.0

View File

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

View File

@ -1,5 +1,5 @@
name=BitArray
version=0.2.7
version=0.2.8
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.