0.2.5 nibbleArray

This commit is contained in:
rob tillaart 2023-02-09 11:31:57 +01:00
parent 5e09281cc7
commit b8c103c9c1
14 changed files with 121 additions and 43 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

@ -6,11 +6,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.2.5] - 2023-02-08
- update readme.md
- fix keywords.txt
- improve measurement - nibbleArray_performance.ino
- update GitHub actions
- update license 2023
## [0.2.4] - 2022-11-18
- add RP2040 in build-CI
- add changelog.md
## [0.2.3] - 2021-12-22
- update library.json, readme
- update license

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2017-2022 Rob Tillaart
Copyright (c) 2017-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,53 +20,71 @@ The nibbleArray is an array that stores 2 nibbles in a byte therefore it is
twice as small as a normal array.
The current implementation can hold 510 elements. This is due a limitation of
the UNO which can **alloc** max 255 bytes in one **malloc()** call.
the UNO which can **allocate** max 255 bytes in one **malloc()** call.
This **NIBBLEARRAY_MAXSIZE** can be defined compile time "-D NIBBLEARRAY_MAXSIZE"
or one can adjust it in the library if other platforms can allocate more memory.
#### 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).
## Interface
The interface of the nibbleArray is straightforward:
```cpp
#include "nibbleArray.h"
```
#### Constructor
- **nibbleArray(uint16_t size)** constructor
- **uint16_t size()** returns the size of the array.
- **uint16_t memory()** returns the memory used in bytes.
#### Base
- **uint8_t set(uint16_t index, uint8_t value)** set a value in the nibbleArray.
Index must be in range otherwise 0xFF = NIBBLEARRAY_ERROR_INDEX will be returned.
If value > 15 it will be truncated.
- **uint8_t get(uint16_t index)** get value from the nibbleArray.
Index must be in range otherwise 0xFF = NIBBLEARRAY_ERROR_INDEX will be returned.
- **uint16_t size()** returns the size of the array.
- **uint16_t memory()** returns the memory used in bytes.
- **void clear()** set all elements to 0.
- **void SetAll(uint8_t value)** set all elements to value (0..15).
If value > 15 it will be truncated.
## Operation
See examples
## Future
#### must
#### Must
- align interface with boolArray and bitArray.
- add a begin() function that does the work now done in constructor (0.3.0)
- similar to bitArray and BoolArray classes.
#### Should
- align interface with BoolArray and bitArray.
- is there some base class?
- align error codes.
- add a begin() function that does the work now done in constructor
- allow larger allocations for non AVR, how?
- don't test for size, user responsibility?
#### could
#### Could
- implement NIBBLEARRAY_ERROR_VALUE for set and setAll ??
- for now user responsibility.
#### won't
#### Won't
- setAll( f() ) - fill the array by calling a function n times?

View File

@ -8,6 +8,7 @@
#include "nibbleArray.h"
// AVR UNO can handle only 510
// ESP32 can do more but depends on RTOS limits
@ -43,6 +44,8 @@ void test_size()
{
Serial.print("Nibble array size:\t");
Serial.println(na.size());
Serial.print("Nibble array mem:\t");
Serial.println(na.memory());
delay(100);
}
@ -50,8 +53,9 @@ void test_size()
void test_get()
{
Serial.println("\nget");
delay(10);
start = micros();
for (int i = 0; i < na.size(); i++)
for (uint16_t i = 0; i < na.size(); i++)
{
x += na.get(i);
}
@ -62,7 +66,7 @@ void test_get()
delay(100);
start = micros();
for (int i = 0; i < na.size(); i++)
for (uint16_t i = 0; i < na.size(); i++)
{
x += na.get(i);
x += na.get(i);
@ -82,8 +86,9 @@ void test_get()
void test_set()
{
Serial.println("\nset");
delay(10);
start = micros();
for (int i = 0; i < na.size(); i++)
for (uint16_t i = 0; i < na.size(); i++)
{
na.set(i, 5);
}
@ -94,7 +99,7 @@ void test_set()
delay(100);
start = micros();
for (int i = 0; i < na.size(); i++)
for (uint16_t i = 0; i < na.size(); i++)
{
na.set(i, 5);
na.set(i, 10);
@ -112,6 +117,7 @@ void test_set()
void test_clear()
{
Serial.println("\nclear");
delay(10);
start = micros();
na.clear();
stop = micros();
@ -130,7 +136,7 @@ void test_clear()
Serial.print("DELTA:\t\t");
Serial.println(d2 - d1);
delay(100);
for (int i = 0; i < na.size(); i++)
for (uint16_t i = 0; i < na.size(); i++)
{
if (na.get(i) != 0)
{
@ -144,6 +150,7 @@ void test_clear()
void test_setAll()
{
Serial.println("\nsetAll");
delay(10);
start = micros();
na.setAll(1);
stop = micros();
@ -151,7 +158,7 @@ void test_setAll()
d1 = stop - start;
Serial.println(d1);
delay(100);
for (int i = 0; i < na.size(); i++)
for (uint16_t i = 0; i < na.size(); i++)
{
if (na.get(i) != 1)
{
@ -178,5 +185,5 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -0,0 +1,25 @@
NIBBLEARRAY_LIB_VERSION: 0.2.5
Nibble array size: 500
Nibble array mem: 250
get
DURATION: 1892
DURATION: 3552
DELTA: 1660
X: 4500
set
DURATION: 1580
DURATION: 2804
DELTA: 1224
clear
DURATION: 100
DURATION: 196
DELTA: 96
setAll
DURATION: 96
DURATION: 192
DELTA: 96
Done...

View File

@ -1,7 +1,7 @@
# Syntax Colouring Map For BoolArray
# Syntax Colouring Map For nibbleArray
# Data types (KEYWORD1)
BoolArray KEYWORD1
nibbleArray KEYWORD1
# Methods and Functions (KEYWORD2)
@ -18,6 +18,7 @@ setAll KEYWORD2
# Constants (LITERAL1)
NIBBLEARRAY_LIB_VERSION LITERAL1
NIBBLEARRAY_MAXSIZE LITERAL1
NIBBLEARRAY_OK LITERAL1
NIBBLEARRAY_ERROR_INDEX LITERAL1

View File

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

View File

@ -1,9 +1,9 @@
name=NibbleArray
version=0.2.4
version=0.2.5
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library to implement a compact array of nibbles (4 bit).
paragraph=
paragraph=
category=Data Processing
url=https://github.com/RobTillaart/Arduino/tree/master/libraries/
architectures=*

View File

@ -1,7 +1,7 @@
//
// FILE: nibbleArray.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.4
// VERSION: 0.2.5
// PURPOSE: Arduino library for a compact array of nibbles (4 bits)
// URL: https://github.com/RobTillaart/nibbleArray
@ -32,9 +32,10 @@ nibbleArray::~nibbleArray()
uint8_t nibbleArray::get(const uint16_t index)
{
// disable this check for more speed
if (index > _size)
{
return NIBBLEARRAY_ERROR_INDEX; // disable this check for more speed
return NIBBLEARRAY_ERROR_INDEX;
}
if (index & 1) return _arr[index/2] & 0x0F;
return _arr[index/2] >> 4;
@ -43,9 +44,10 @@ uint8_t nibbleArray::get(const uint16_t index)
uint8_t nibbleArray::set(const uint16_t index, uint8_t value)
{
// disable this check for more speed
if (index > _size)
{
return NIBBLEARRAY_ERROR_INDEX; // disable this check for more speed
return NIBBLEARRAY_ERROR_INDEX;
}
uint8_t v = value & 0x0F;
if (index & 1) _arr[index/2] = (_arr[index/2] & 0xF0) | v;
@ -54,6 +56,18 @@ uint8_t nibbleArray::set(const uint16_t index, uint8_t value)
}
uint16_t nibbleArray::size()
{
return _size;
};
uint16_t nibbleArray::memory()
{
return _bytes;
};
void nibbleArray::clear()
{
memset(_arr, 0, (_size + 1)/2);
@ -68,5 +82,5 @@ void nibbleArray::setAll(uint8_t value)
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -2,7 +2,7 @@
//
// FILE: nibbleArray.h
// AUTHOR: Rob Tillaart
// VERSION: 0.2.4
// VERSION: 0.2.5
// PURPOSE: Arduino library for a compact array of nibbles (4 bits)
// URL: https://github.com/RobTillaart/nibbleArray
//
@ -10,7 +10,9 @@
#include "Arduino.h"
#define NIBBLEARRAY_LIB_VERSION (F("0.2.4"))
#define NIBBLEARRAY_LIB_VERSION (F("0.2.5"))
#ifndef NIBBLEARRAY_MAXSIZE
// UNO BASED MAXSIZE?
@ -33,8 +35,8 @@ public:
// returns 0xFF for index error.
uint8_t set(const uint16_t index, uint8_t value);
uint16_t size() { return _size; };
uint16_t memory() { return _bytes; };
uint16_t size();
uint16_t memory();
void clear();
void setAll(uint8_t value);
@ -46,4 +48,5 @@ private:
};
// -- END OF FILE --
// -- END OF FILE --

View File

@ -38,9 +38,10 @@
unittest_setup()
{
fprintf(stderr, "VERSION: %s\n", (char *) NIBBLEARRAY_LIB_VERSION);
fprintf(stderr, "NIBBLEARRAY_LIB_VERSION: %s\n", (char *) NIBBLEARRAY_LIB_VERSION);
}
unittest_teardown()
{
fprintf(stderr, "\n");
@ -87,11 +88,13 @@ unittest(test_all)
assertEqual(15, na.get(i));
sum += na.get(i);
}
assertEqual(330, sum); // not all 500 all summed!
assertEqual(330, sum); // not all 500 all summed!
}
unittest_main()
// --------
// -- END OF FILE --