Skip to content

Commit d0f1031

Browse files
committed
add Set 0.2.4
1 parent 1e2905b commit d0f1031

File tree

7 files changed

+67
-8
lines changed

7 files changed

+67
-8
lines changed

libraries/Set/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ all iterator-functions returns the current element or -1 if not exist.
6868
- **next()** find the next element. Will not wrap zround when 'end' of the set is reached.
6969
- **prev()** find the previous element. Will not wrap zround when 'begin' of the set is reached.
7070
- **last()** find the last element.
71-
71+
- **getNth(n)** find the Nth element in a set if it exist.
7272

7373
## Operational
7474

libraries/Set/Set.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//
22
// FILE: set.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.2.3
4+
// VERSION: 0.2.4
55
// DATE: 2014-09-11
66
// PURPOSE: SET library for Arduino
77
// URL: https://github.com/RobTillaart/SET
88
//
99
// HISTORY:
10+
// 0.2.4 2021-05-06 getNth(n)
1011
// 0.2.3 2021-05-05 Add addAll (256 elements) + setCurrent
1112
// 0.2.2 2021-01-07 Arduino-CI, unit test
1213
// 0.2.1 2020-06-19 fix library.json
@@ -264,6 +265,21 @@ int Set::last()
264265
}
265266

266267

268+
int Set::getNth(const uint8_t n)
269+
{
270+
if (n == 0) return -1;
271+
if (n == 1) return first();
272+
_current = first();
273+
int i = 1;
274+
while ((_current > -1) && (i < n))
275+
{
276+
_current = next();
277+
i++;
278+
}
279+
return _current;
280+
}
281+
282+
267283
int Set::findPrev(const uint8_t p, uint8_t q)
268284
{
269285
uint8_t m = 1 << q;

libraries/Set/Set.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: set.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.2.3
5+
// VERSION: 0.2.4
66
// DATE: 2014-09-11
77
// PURPOSE: SET library for Arduino
88
// URL: https://github.com/RobTillaart/SET
@@ -11,7 +11,7 @@
1111
#include "Arduino.h"
1212

1313

14-
#define SET_LIB_VERSION (F("0.2.3"))
14+
#define SET_LIB_VERSION (F("0.2.4"))
1515

1616

1717
class Set
@@ -59,7 +59,7 @@ class Set
5959
int next(); // find next element
6060
int prev(); // find previous element
6161
int last(); // find last element
62-
62+
int getNth(const uint8_t n); // find Nth element in a set (from start)
6363

6464
private:
6565
uint8_t _mem[32]; // can hold 0..255

libraries/Set/keywords.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ first KEYWORD2
2020
next KEYWORD2
2121
prev KEYWORD2
2222
last KEYWORD2
23-
23+
getNth KEYWORD2
2424

2525
# Constants (LITERAL1)
2626
SET_LIB_VERSION LITERAL1

libraries/Set/library.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/SET.git"
1717
},
18-
"version":"0.2.3",
18+
"version": "0.2.4",
19+
"license": "MIT",
1920
"frameworks": "arduino",
2021
"platforms": "*"
2122
}

libraries/Set/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SET
2-
version=0.2.3
2+
version=0.2.4
33
author=Rob Tillaart <rob.tillaart@gmail.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=Arduino library to implement simple SET datastructure.

libraries/Set/test/unit_test_001.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,48 @@ unittest(test_operator)
147147
assertTrue( C <= B);
148148
}
149149

150+
151+
unittest(test_iterator)
152+
{
153+
fprintf(stderr, "VERSION: %s\n", SET_LIB_VERSION);
154+
155+
Set A;
156+
A.clear();
157+
158+
for (int i = 0; i < 20; i++)
159+
{
160+
A.add(i*3);
161+
}
162+
assertEqual(20, A.count());
163+
164+
fprintf(stderr, "\nFirst() -> next()\n");
165+
int cur = A.first();
166+
for (int i = 0; i < 20; i++)
167+
{
168+
assertEqual(i * 3, cur);
169+
cur = A.next();
170+
}
171+
172+
fprintf(stderr, "\nlast() -> prev()\n");
173+
cur = A.last();
174+
for (int i = 19; i > 0; i--)
175+
{
176+
assertEqual(i * 3, cur);
177+
cur = A.prev();
178+
}
179+
180+
fprintf(stderr, "\ngetNth()\n");
181+
assertEqual(0, A.getNth(1));
182+
assertEqual(3, A.getNth(2));
183+
assertEqual(6, A.getNth(3));
184+
assertEqual(9, A.getNth(4));
185+
assertEqual(12, A.getNth(5));
186+
assertEqual(15, A.getNth(6));
187+
assertEqual(18, A.getNth(7));
188+
assertEqual(21, A.getNth(8));
189+
190+
}
191+
150192
unittest_main()
151193

152194
// --------

0 commit comments

Comments
 (0)