-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement double-ended iterators (#1)
Implements iterators which can be traversed both forwards and backwards. Tasks: - [x] Add support for double-ended iterators - [x] Make source iterators double-ended (e.g. repeat, once, over_array) - [x] Make transformative iterators double-ended (where possible) - [x] Document double-ended features in README - [x] Add tests for double-ended features
- Loading branch information
Showing
21 changed files
with
500 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* CIter - C library for lazily-evaluated iterators. | ||
* Copyright (C) 2024 Kian Kasad <kian@kasad.com> | ||
* | ||
* This file is part of CIter. | ||
* | ||
* CIter is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Lesser General Public License as published by the Free Software | ||
* Foundation, version 3 of the License. | ||
* | ||
* CIter is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along | ||
* with CIter. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include <citer.h> | ||
|
||
#define UNUSED(x) ((void) (x)) | ||
|
||
static void *map_deref(void *item) { | ||
return *((void **) item); | ||
} | ||
|
||
static bool is_even(void *item, void *fn_data) { | ||
UNUSED(fn_data); | ||
return ((unsigned long) item) % 2 == 0; | ||
} | ||
|
||
static void print_item(void *item, void *fn_data) { | ||
printf("%s: %lu\n", (const char *) fn_data, (unsigned long) item); | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
if (argc != 1) { | ||
fprintf(stderr, "Usage: %s\n", argv[0]); | ||
return 1; | ||
} | ||
|
||
unsigned long arr[] = { 1, 2, 3, 4, 5 }; | ||
iterator_t *it; | ||
|
||
/* | ||
* Double-ended iterators which must be tested: | ||
* - [x] over_array | ||
* - [ ] repeat (but we can't test this without take) | ||
* - [x] chain | ||
* - [x] filter | ||
* - [x] map | ||
* - [x] once | ||
* - [x] flatten | ||
* - [x] inspect | ||
* - [x] reverse | ||
*/ | ||
|
||
it = citer_over_array(arr, sizeof(*arr), sizeof(arr) / sizeof(*arr)); | ||
it = citer_map(it, map_deref); | ||
it = citer_chain(it, citer_flatten(citer_once(citer_once((void *) 6ul)))); | ||
it = citer_inspect(it, print_item, "Before map"); | ||
it = citer_filter(it, is_even, NULL); | ||
it = citer_reverse(it); | ||
|
||
void *item; | ||
while ((item = citer_next(it))) | ||
printf("Got: %lu\n", (unsigned long) item); | ||
|
||
citer_free(it); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* CIter - C library for lazily-evaluated iterators. | ||
* Copyright (C) 2024 Kian Kasad <kian@kasad.com> | ||
* | ||
* This file is part of CIter. | ||
* | ||
* CIter is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Lesser General Public License as published by the Free Software | ||
* Foundation, version 3 of the License. | ||
* | ||
* CIter is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along | ||
* with CIter. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
#include <citer.h> | ||
|
||
int main(int argc, char *argv[]) { | ||
if (argc <= 1) { | ||
fprintf(stderr, "Usage: %s <args...>\n", argv[0]); | ||
return 1; | ||
} | ||
|
||
iterator_t *it = citer_reverse(citer_over_array((void **) argv + 1, sizeof(*argv), argc - 1)); | ||
|
||
char **itemptr; | ||
unsigned long count = 0; | ||
while ((itemptr = (char **) citer_next(it))) { | ||
printf("Got: %s\n", *itemptr); | ||
count++; | ||
} | ||
printf("Count: %lu\n", count); | ||
|
||
citer_free(it); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.