-
Notifications
You must be signed in to change notification settings - Fork 0
/
itplus.h
1725 lines (1662 loc) · 84.5 KB
/
itplus.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
* @brief All your iterplus needs in a single header file.
*
* Repo: https://github.com/TotallyNotChase/c-iterplus
*
* @author TotallyNotChase
* @copyright MIT License
*
* Copyright (c) 2021 TotallyNotChase
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef LIB_ITPLUS_H
#define LIB_ITPLUS_H
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#define ITPL_CONCAT_(A, B) A##B
#define ITPL_CONCAT(A, B) ITPL_CONCAT_(A, B)
/**
* @def typeclass(funcs)
* @brief Define a typeclass with the given functions.
*
* # Example
*
* @code
* typedef typeclass(char* (*show)(void* self)) Show; // Defines a typeclass and names it `Show`
* @endcode
*
* @param funcs A semicolon separated list of typeclass functions.
*
* @note The functions usually take the `self` from the typeclass instance (and possibly more arguments).
*/
#define typeclass(funcs) \
struct \
{ \
funcs; \
}
/**
* @def typeclass_instance(Typeclass)
* @brief Define a typeclass instance for the given typeclass.
*
* This just contains a `void* self` member and the typeclass itself.
* # Example
*
* @code
* typedef typeclass(char* (*show)(void* self)) Show;
* typedef typeclass_instance(Show) Showable; // Defines the typeclass instance for `Show` typeclass
* @endcode
*
* @param Typeclass The semantic type (C type) of the typeclass defined with #typeclass(funcs).
*/
#define typeclass_instance(Typeclass) \
struct \
{ \
void* self; \
Typeclass const* tc; \
}
/**
* @struct MaybeTag
* @brief The enum used for tagging the `Maybe` type.
*/
typedef enum
{
MaybeTag_Nothing = 0, /**< `Nothing` tag - indicates absence of a value. */
MaybeTag_Just /**< `Just` tag - indicates presence of a value. */
} MaybeTag;
/**
* @def Maybe(T)
* @brief Convenience macro to get the type of the Maybe defined with a certain type.
*
* # Example
*
* @code
* DefineMaybe(int)
* Maybe(int) const x = {0}; // Uses the maybe type defined in the previous line
* @endcode
*
* @param T The type of value the `Maybe` struct will contain. Must be the same type name passed to #DefineMaybe(T).
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only alphanumerics.
*/
#define Maybe(T) ITPL_CONCAT(Maybe_, T)
/**
* @def DefineMaybe(T)
* @brief Define a Maybe<T> type.
*
* # Example
*
* @code
* DefineMaybe(int) // Defines a Maybe(int) type as well as its corresponding functions
* @endcode
*
* @param T The type of value this `Maybe` will hold. Must be alphanumeric.
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only alphanumerics.
* @note This should not be delimited by a semicolon.
*/
#define DefineMaybe(T) \
typedef struct \
{ \
MaybeTag tag; \
/* Don't access this member manually */ \
T val; \
} Maybe(T); \
static inline T ITPL_CONCAT(T, _from_just)(Maybe(T) maybex) \
{ \
if (is_just(maybex)) { \
return maybex.val; \
} else { \
fputs("Attempted to extract Just value from Nothing", stderr); \
abort(); \
} \
}
/**
* @def Just(v, T)
* @brief Wrap a `Just` value into a #Maybe(T).
*
* # Example
*
* @code
* DefineMaybe(int)
* Maybe(int) const x = Just(42, int); // Initializes a Maybe(int) with the value 42
* @endcode
*
* @param v The concrete value to wrap in `Just` (must be of the correct type).
* @param T The type of value the `Maybe` will hold. Must be alphanumeric.
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only alphanumerics.
* @note The value is simply assigned to the #Maybe(T) struct. No implicit cloning is done.
*/
#define Just(v, T) ((Maybe(T)){.tag = MaybeTag_Just, .val = (v)})
/**
* @def Nothing(T)
* @brief Wrap a `Nothing` value into a #Maybe(T).
*
* # Example
*
* @code
* DefineMaybe(int)
* Maybe(int) const x = Nothing(int); // Initializes a Maybe(int) with no value
* @endcode
*
* @param T The type of value the `Maybe` will hold. Must be alphanumeric.
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only alphanumerics.
*/
#define Nothing(T) ((Maybe(T)){0})
/**
* @def is_nothing(x)
* @brief Check if the given Maybe type is tagged with `Nothing`.
*
* @param x The #Maybe(T) struct to check against.
*/
#define is_nothing(x) ((x).tag == MaybeTag_Nothing)
/**
* @def is_just(x)
* @brief Check if the given Maybe type is tagged with `Just`.
*
* @param x The #Maybe(T) struct to check against.
*/
#define is_just(x) ((x).tag == MaybeTag_Just)
/**
* @def from_just(x, T)
* @brief Extract the `Just` value from given #Maybe(T).
*
* @param x The `Maybe` type to extract the value from.
* @param T The type of value the `Maybe` will hold. Must be alphanumeric.
*
* @return `Just` value of type corresponding to the given #Maybe(T) if it's not `Nothing`.
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only alphanumerics.
* @note Aborts the program if given #Maybe(T) struct was tagged with `Nothing`.
*/
#define from_just(x, T) ITPL_CONCAT(T, _from_just(x))
/**
* @def from_just_(x)
* @brief "Unsafe" version of #from_just(x, T).
*
* @param x The `Maybe` type to extract the value from.
*
* @return `Just` value of type corresponding to the given `Maybe` struct.
*
* @note This does not check whether the `Maybe` struct actually has a value and hence
* should only be used when the caller is sure that the Maybe contains a `Just` value. Otherwise
* the behavior is undefined.
*/
#define from_just_(x) (x).val
/**
* @def fmap_maybe(x, fn, R)
* @brief Map the function `fn` over `x` to construct a `Maybe(R)`.
*
* @param x The `Maybe` type to map over.
* @param fn The function to map over the value inside the `Maybe` (if any). Should be of type `R (*)(T x)`. Where `T`
* is the type of value contained within `x`.
* @param R The return type of the mapping function.
*
* @return If `x` had a value, a `Maybe(B)` with a `Just` value after mapping `fn`. Otherwise, a `Maybe(B)` as
* `Nothing`.
*
* @note This uses `x` twice. Do not use it with an `x` that may have side effects.
*/
#define fmap_maybe(x, fn, R) is_nothing(x) ? Nothing(R) : Just(fn(from_just_(x)), R)
/**
* @def Iterator(T)
* @brief Convenience macro to get the type of the Iterator (typeclass) with given element type.
*
* # Example
*
* @code
* DefineIteratorOf(int);
* Iterator(int) i; // Declares a variable of type Iterator(int) (a typeclass)
* @endcode
*
* @param T The type of value the `Iterator` instance will yield. Must be the same type name passed to
* #DefineIteratorOf(T).
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only alphanumerics.
*/
#define Iterator(T) ITPL_CONCAT(Iterator_, T)
/**
* @def Iterable(T)
* @brief Convenience macro to get the type of the Iterable (typeclass instance) with given element type.
*
* # Example
*
* @code
* DefineIteratorOf(int);
* Iterable(int) i; // Declares a variable of type Iterable(Int) (the typeclass instance)
* @endcode
*
* @param T The type of value the `Iterable` will yield. Must be the same type name passed to #DefineIteratorOf(T).
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only alphanumerics.
*/
#define Iterable(T) ITPL_CONCAT(Iterable_, T)
/**
* @def DefineIteratorOf(T)
* @brief Define an Iterator typeclass and its Iterable instance for given element type.
*
* # Example
*
* @code
* DefineIteratorOf(int); // Defines an Iterator(int) typeclass as well as its instance
* @endcode
*
* @param T The type of value the `Iterator` instance will yield. Must be alphanumeric.
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only alphanumerics.
* @note A #Maybe(T) for the given `T` **must** also exist.
*/
#define DefineIteratorOf(T) \
typedef typeclass(Maybe(T) (*const next)(void* self)) Iterator(T); \
typedef typeclass_instance(Iterator(T)) Iterable(T)
/**
* @def impl_iterator(IterType, ElmntType, Name, next_f)
* @brief Define a function to turn given `IterType` into an #Iterable(ElmntType).
*
* Implement the Iterator typeclass for a type. Essentially defining a wrapper function that returns the Iterable.
*
* The defined function takes in a value of `IterType` and wraps it in an `Iterable` - which can be passed around to
* generic functions working on an iterable.
*
* The term "generic" is used here in the context of the **input**.
* As in, the function taking a generic iterable, does not care about what type is backing up the iterable; but,
* does care about what element type the iterator yields.
*
* # Example
*
* @code
* // Example of implementing an infinite iterator representing the fibonacci sequence
*
* #include <stdint.h>
*
* typedef struct fibonacci
* {
* uint32_t curr;
* uint32_t next;
* } Fibonacci;
*
* DefineMaybe(uint32_t)
* DefineIteratorOf(uint32_t);
*
* static Maybe(uint32_t) fibnxt(Fibonacci* self)
* {
* uint32_t new_nxt = self->curr + self->next;
* self->curr = self->next;
* self->next = new_nxt;
* return Just(new_nxt, uint32_t);
* }
*
* // Define a function named `prep_fib_itr`, which takes in a `Fibonacci*` and returns an `Iterable(int)`
* // The returned iterable is an infinite fibonacci sequence
* impl_iterator(Fibonacci*, uint32_t, prep_fib_itr, fibnxt)
* @endcode
*
* @param IterType The semantic type (C type) this impl is for, must be a pointer type.
* @param ElmntType The type of value the `Iterator` instance will yield.
* @param Name Name to define the function as.
* @param next_f Function pointer that serves as the `next` implementation for `IterType`. This function must have
* the signature of `Maybe(ElmntType) (*)(IterType self)` - i.e, should take IterType and return a value of the
* corresponding element type wrapped in a `Maybe` - `Nothing` value indicates end of iteration.
*
* @note If `ElmntType` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only
* alphanumerics.
* @note A #Maybe(T) for the given `ElmntType` **must** exist.
* @note This should not be delimited by a semicolon.
*/
#define impl_iterator(IterType, ElmntType, Name, next_f) \
Iterable(ElmntType) Name(IterType x) \
{ \
Maybe(ElmntType) (*const next_)(IterType self) = (next_f); \
(void)next_; \
static Iterator(ElmntType) const tc = {.next = (Maybe(ElmntType)(*const)(void*))(next_f)}; \
return (Iterable(ElmntType)){.tc = &tc, .self = x}; \
}
/**
* @def Pair(T, U)
* @brief Convenience macro to get the type of the Pair defined with certain types.
*
* # Example
*
* @code
* DefinePair(int, char);
* Pair(int, char) const x = {0}; // Uses the pair type defined in the previous line
* @endcode
*
* @param T The type the first value in this `Pair` struct. Must be the same type name passed to #DefinePair(T, U)
* @param T The type the second value in this `Pair` struct. Must be the same type name passed to #DefinePair(T, U).
*
* @note If `T`, or `U`, is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only
* alphanumerics.
*/
#define Pair(T, U) ITPL_CONCAT(ITPL_CONCAT(Pair_, T), ITPL_CONCAT(_, U))
/**
* @def DefinePair(T, U)
* @brief Define a Pair<T, U> type.
*
* # Example
*
* @code
* DefinePair(int, char); // Defines a Pair(int, char) type
* @endcode
*
* @param T The type the first value in this `Pair` struct.
* @param T The type the second value in this `Pair` struct.
*
* @note If `T`, or `U`, is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only
* alphanumerics.
*/
#define DefinePair(T, U) \
typedef struct \
{ \
T a; \
U b; \
} Pair(T, U)
/**
* @def PairOf(x, y, T, U)
* @brief Wrap 2 values into a #Pair(T, U).
*
* # Example
*
* @code
* DefinePair(int, char);
* Pair(int, char) const x = PairOf(42, 'c', int, char); // Initializes a Pair(int, char) with the value 42 and 'c'
* @endcode
*
* @param x The value to put as the first member of the pair struct.
* @param y The value to put as the second member of the pair struct.
* @param T The type of the first value.
* @param U The type of the second value.
*
* @note If `T`, or `U`, is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only
* alphanumerics.
* @note A #Pair(T, U) for given `T` and `U` must exist.
* @note The values are simply assigned to the #Pair(T, U) struct. No implicit cloning is done.
*/
#define PairOf(x, y, T, U) ((Pair(T, U)){.a = (x), .b = (y)})
/**
* @def fst(pair)
* @brief Get the first value from given pair.
*/
#define fst(pair) (pair).a
/**
* @def snd(pair)
* @brief Get the second value from given pair.
*/
#define snd(pair) (pair).b
#define UNIQVAR(x) ITPL_CONCAT(ITPL_CONCAT(x, _4x2_), __LINE__) /* "Unique" variable name */
/**
* @def foreach(T, x, it)
* @brief Iterate through given iterable and store each element in `x`
*
* @param T Type of the elements the iterable yields.
* @param x The variable name to store each element in. Available only inside the loop.
* @param it The iterable to iterate over. This will be consumed.
*
* @note `it` must not be an unevaluated expression. Otherwise, it will be evaluated multiple times in this macro.
* @note This macro cannot be used multiple times *in the same line*. Due to naming conflicts from an implicitly defined
* variable.
*/
#define foreach(T, x, it) \
Maybe(T) UNIQVAR(res) = (it).tc->next((it).self); \
for (T x = from_just_(UNIQVAR(res)); is_just(UNIQVAR(res)); \
UNIQVAR(res) = (it).tc->next((it).self), x = from_just_(UNIQVAR(res)))
/**
* @def IterChain(T)
* @brief Convenience macro to get the type of the IterChain struct with given element type.
*
* # Example
*
* @code
* DefineIterChain(int);
* IterChain(int) i; // Declares a variable of type IterChain(int)
* @endcode
*
* @param T The type of value the `Iterable` wrapped in this `IterChain` will yield. Must be the same type name passed
* to #DefineIterChain(T).
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only alphanumerics.
*/
#define IterChain(T) ITPL_CONCAT(IterChain_, T)
/**
* @def DefineIterChain(T)
* @brief Define an IterChain struct that works on `Iterable(T)`s.
*
* # Example
*
* @code
* DefineIterChain(int); // Defines an IterChain(int) struct
* @endcode
*
* @param T The type of value the `Iterable` wrapped in this `IterChain` will yield.
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only alphanumerics.
* @note An #Iterator(T) for the given `T` **must** also exist.
*/
#define DefineIterChain(T) \
typedef struct \
{ \
Iterable(T) curr; \
Iterable(T) nxt; \
} IterChain(T)
/**
* @def define_iterchain_func(T, Name)
* @brief Define a function to turn an #IterChain(T) into an #Iterable(T).
*
* Define the `next` function implementation for the #IterChain(T) struct, and use it to implement the Iterator
* typeclass, for given `T`.
*
* The defined function takes in a value of type `IterChain(T)*` and wraps it in an `Iterable(T)`.
*
* # Example
*
* @code
* DefineIterChain(int);
*
* // Implement `Iterator` for `IterChain(int)`
* // The defined function has the signature- `Iterable(int) wrap_intitrchn(IterChain(int)* x)`
* define_iterchain_func(int, wrap_intitrchn)
* @endcode
*
* Usage of the defined function-
*
* @code
* // Chain together fst_it and snd_it (both of type `Iterable(int)`)
* Iterable(int) it = wrap_intitrchn(&(IterChain(int)){ .curr = fst_it, .nxt = snd_it });
* @endcode
*
* @param T The type of value the `Iterable` wrapped in this `IterChain` will yield.
* @param Name Name to define the function as.
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only
* alphanumerics.
* @note An #IterChain(T) for the given `T` **must** exist.
* @note This should not be delimited by a semicolon.
*/
#define define_iterchain_func(T, Name) \
static Maybe(T) ITPL_CONCAT(IterChain(T), _nxt)(IterChain(T) * self) \
{ \
Iterable(T) const srcit = self->curr; \
Maybe(T) const res = srcit.tc->next(srcit.self); \
if (is_just(res)) { \
return res; \
} \
self->curr = self->nxt; \
Iterable(T) re_srcit = self->curr; \
return re_srcit.tc->next(re_srcit.self); \
} \
impl_iterator(IterChain(T)*, T, Name, ITPL_CONCAT(IterChain(T), _nxt))
#ifndef ITPLUS_COLLECT_BUFSZ
#define ITPLUS_COLLECT_BUFSZ 64
#endif /* !ITPLUS_COLLECT_BUFSZ */
/**
* @def define_itercollect_func(T, Name)
* @brief Define the `collect` function for an iterable.
*
* The defined function takes in an iterable of type `T`, and turns it into an array. Each element of said array
* is of type `T`.
*
* This defined function will consume the given iterable.
*
* # Example
*
* @code
* // Defines a function with the signature- `int* collect_int(Iterable(int) x, size_t* len)`
* define_itercollect_func(int, collect_int)
* @endcode
*
* Usage of the defined function-
*
* @code
* size_t arrlen = 0;
* // Collect `it` (of type `Iterable(int)`) into an array
* int* intarr = collect_int(it, &arrlen);
* @endcode
*
* @param T The type of value the `Iterable`, for which this is being implemented, yields.
* @param Name Name to define the function as.
*
* @note The returned array must be freed.
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only alphanumerics.
* @note An #Iterator(T) for the given `T` **must** also exist.
*/
#define define_itercollect_func(T, Name) \
T* Name(Iterable(T) it, size_t* len) \
{ \
size_t size = ITPLUS_COLLECT_BUFSZ; \
*len = 0; \
T* arr = malloc(size * sizeof(*arr)); \
if (arr == NULL) { \
return NULL; \
} \
foreach (T, x, it) { \
if (*len == size) { \
T* temp = realloc(arr, (size *= 2) * sizeof(*arr)); \
if (temp == NULL) { \
free(arr); \
return NULL; \
} \
arr = temp; \
} \
arr[(*len)++] = x; \
} \
return arr; \
}
/**
* @def IterDrop(T)
* @brief Convenience macro to get the type of the IterDrop struct with given element type.
*
* # Example
*
* @code
* DefineIterDrop(int);
* IterDrop(int) i; // Declares a variable of type IterDrop(int)
* @endcode
*
* @param T The type of value the `Iterable` wrapped in this `IterDrop` will yield. Must be the same type name passed to
* #DefineIterDrop(T).
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only alphanumerics.
*/
#define IterDrop(T) ITPL_CONCAT(IterDrop_, T)
/**
* @def DefineIterDrop(T)
* @brief Define an IterDrop struct that works on `Iterable(T)`s.
*
* # Example
*
* @code
* DefineIterDrop(int); // Defines an IterDrop(int) struct
* @endcode
*
* @param T The type of value the `Iterable` wrapped in this `IterDrop` will yield.
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only alphanumerics.
* @note An #Iterator(T) for the given `T` **must** also exist.
*/
#define DefineIterDrop(T) \
typedef struct \
{ \
size_t i; \
size_t limit; \
Iterable(T) src; \
} IterDrop(T)
/**
* @def define_iterdrop_func(T, Name)
* @brief Define a function to turn an #IterDrop(T) into an #Iterable(T).
*
* Define the `next` function implementation for the #IterDrop(T) struct, and use it to implement the Iterator
* typeclass, for given `T`.
*
* The defined function takes in a value of type `IterDrop(T)*` and wraps it in an `Iterable(T)`.
*
* # Example
*
* @code
* DefineIterDrop(int);
*
* // Implement `Iterator` for `IterDrop(int)`
* // The defined function has the signature- `Iterable(int) wrap_intitrdrp(IterDrop(int)* x)`
* define_iterdrop_func(int, wrap_intitrdrp)
* @endcode
*
* Usage of the defined function-
*
* @code
* // Drop (skip) the first 10 elements from `it` (of type `Iterable(int)`) and create a new iterable
* Iterable(int) after10 = wrap_intitrdrp(&(IterDrop(int)){ .limit = 10, .src = it });
* @endcode
*
* @param T The type of value the `Iterable` wrapped in this `IterDrop` will yield.
* @param Name Name to define the function as.
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only
* alphanumerics.
* @note An #IterDrop(T) for the given `T` **must** exist.
* @note This should not be delimited by a semicolon.
*/
#define define_iterdrop_func(T, Name) \
static Maybe(T) ITPL_CONCAT(IterDrop(T), _nxt)(IterDrop(T) * self) \
{ \
Iterable(T) const srcit = self->src; \
foreach (T, x, srcit) { \
if (self->i >= self->limit) { \
return Just(x, T); \
} \
++(self->i); \
} \
return Nothing(T); \
} \
impl_iterator(IterDrop(T)*, T, Name, ITPL_CONCAT(IterDrop(T), _nxt))
/**
* @def IterDropWhile(T)
* @brief Convenience macro to get the type of the IterDropWhile struct with given element type.
*
* # Example
*
* @code
* DefineIterDropWhile(int);
* IterDropWhile(int) i; // Declares a variable of type IterDropWhile(int)
* @endcode
*
* @param T The type of value the `Iterable` wrapped in this `IterDropWhile` will yield. Must be the same type name
* passed to #DefineIterDropWhile(T).
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only alphanumerics.
*/
#define IterDropWhile(T) ITPL_CONCAT(IterDropWhile_, T)
/**
* @def DefineIterDropWhile(T)
* @brief Define an IterDropWhile struct that works on `Iterable(T)`s.
*
* # Example
*
* @code
* DefineIterDropWhile(int); // Defines an IterDropWhile(int) struct
* @endcode
*
* @param T The type of value the `Iterable` wrapped in this `IterDropWhile` will yield.
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only alphanumerics.
* @note An #Iterator(T) for the given `T` **must** also exist.
*/
#define DefineIterDropWhile(T) \
typedef struct \
{ \
bool (*pred)(T x); \
bool done; \
Iterable(T) src; \
} IterDropWhile(T)
/**
* @def define_iterdropwhile_func(T, Name)
* @brief Define a function to turn an #IterDropWhile(T) into an #Iterable(T).
*
* Define the `next` function implementation for the #IterDropWhile(T) struct, and use it to implement the Iterator
* typeclass, for given `T`.
*
* The defined function takes in a value of type `IterDropWhile(T)*` and wraps it in an `Iterable(T)`.
*
* # Example
*
* @code
* DefineIterDropWhile(int);
*
* // Implement `Iterator` for `IterDropWhile(int)`
* // The defined function has the signature- `Iterable(int) wrap_intitrdrpwhl(IterDropWhile(int)* x)`
* define_iterdropwhile_func(int, wrap_intitrdrpwhl)
* @endcode
*
* Usage of the defined function-
*
* @code
* static bool is_even(int x) { return x % 2 == 0; }
* @endcode
*
* @code
* // Drop (skip) elements from `it` (of type `Iterable(int)`), while they satisfy `is_even`, and create a new iterable
* Iterable(int) after_evens = wrap_intitrdrpwhl(&(IterDropWhile(int)){ .pred = is_even, .src = it });
* @endcode
*
* @param T The type of value the `Iterable` wrapped in this `IterDropWhile` will yield.
* @param Name Name to define the function as.
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only
* alphanumerics.
* @note An #IterDropWhile(T) for the given `T` **must** exist.
* @note This should not be delimited by a semicolon.
*/
#define define_iterdropwhile_func(T, Name) \
static Maybe(T) ITPL_CONCAT(IterDropWhile(T), _nxt)(IterDropWhile(T) * self) \
{ \
if (self->done) { \
return Nothing(T); \
} \
Iterable(T) const srcit = self->src; \
foreach (T, x, srcit) { \
if (!self->pred(x)) { \
self->done = true; \
return Just(x, T); \
} \
} \
return Nothing(T); \
} \
impl_iterator(IterDropWhile(T)*, T, Name, ITPL_CONCAT(IterDropWhile(T), _nxt))
/**
* @def IterEnumr(T, U)
* @brief Convenience macro to get the type of the IterEnumr struct with given element types.
*
* # Example
*
* @code
* DefineIterEnumr(int);
* IterEnumr(int) i; // Declares a variable of type IterEnumr(int)
* @endcode
*
* @param T The type of value the `Iterable` wrapped in this `IterEnumr` will yield. Must be the same type name
* passed to #DefineIterEnumr(T).
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only
* alphanumerics.
*/
#define IterEnumr(T) ITPL_CONCAT(IterEnumr_, T)
/**
* @def DefineIterEnumr(T)
* @brief Define an IterEnumr struct that works with on `Iterable(T)`s.
*
* # Example
*
* @code
* DefineIterEnumr(int); // Defines an IterEnumr(int) struct
* @endcode
*
* @param T The type of value the `Iterable` wrapped in this `IterEnumr` will yield.
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only
* alphanumerics.
* @note An #Iterator(T) for the given `T` **must** also exist.
*/
#define DefineIterEnumr(T) \
typedef struct \
{ \
size_t i; \
Iterable(T) src; \
} IterEnumr(T)
/**
* @def define_iterenumr_func(T, Name)
* @brief Define a function to turn an #IterEnumr(T) into an #Iterable(T) where `T = Pair(size_t, T)`.
*
* Define the `next` function implementation for the #IterEnumr(T) struct, and use it to implement the Iterator
* typeclass, for given `T`.
*
* The defined function takes in a value of type `IterEnumr(T)*` and wraps it in an `Iterable(Pair(size_t, T))`.
*
* # Example
*
* @code
* DefineIterEnumr(int);
*
* // Implement `Iterator` for `IterEnumr(int)`
* // The defined function has the signature- `Iterable(Pair(size_t, int)) wrap_intenumr(IterEnumr(int)* x)`
* define_iterenumr_func(int, wrap_intenumr)
* @endcode
*
* Usage of the defined function-
*
* @code
* // Enumerate `it` (of type `Iterable(int)`) to create a new iterable
* Iterable(Pair(size_t, int)) enumerated = wrap_intenumr(&(IterEnumr(int)){ .src = it });
* @endcode
*
* @param T The type of value the first `Iterable` wrapped in this `IterEnumr` will yield.
* @param Name Name to define the function as.
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only
* alphanumerics.
* @note An #IterEnumr(T) for the given `T` **must** exist.
* @note An #Iterator(T), with `T = Pair(size_t, T)`, for the given `T`.
* @note This should not be delimited by a semicolon.
*/
#define define_iterenumr_func(T, Name) \
static Maybe(Pair(size_t, T)) ITPL_CONCAT(IterEnumr(T), _nxt)(IterEnumr(T) * self) \
{ \
Iterable(T) const srcit = self->src; \
Maybe(T) const res = srcit.tc->next(srcit.self); \
return is_just(res) ? Just(PairOf(self->i++, from_just_(res), size_t, T), Pair(size_t, T)) \
: Nothing(Pair(size_t, T)); \
} \
impl_iterator(IterEnumr(T)*, Pair(size_t, T), Name, ITPL_CONCAT(IterEnumr(T), _nxt))
/**
* @def IterFilt(T)
* @brief Convenience macro to get the type of the IterFilt struct with given element type.
*
* # Example
*
* @code
* DefineIterFilt(int);
* IterFilt(int) i; // Declares a variable of type IterFilt(int)
* @endcode
*
* @param T The type of value the `Iterable` wrapped in this `IterFilt` will yield. Must be the same type name passed to
* #DefineIterFilt(T).
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only alphanumerics.
*/
#define IterFilt(T) ITPL_CONCAT(IterFilt_, T)
/**
* @def DefineIterFilt(T)
* @brief Define an IterFilt struct that works on `Iterable(T)`s.
*
* # Example
*
* @code
* DefineIterFilt(int); // Defines an IterFilt(int) struct
* @endcode
*
* @param T The type of value the `Iterable` wrapped in this `IterFilt` will yield.
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only alphanumerics.
* @note An #Iterator(T) for the given `T` **must** also exist.
*/
#define DefineIterFilt(T) \
typedef struct \
{ \
bool (*pred)(T x); \
Iterable(T) src; \
} IterFilt(T)
/**
* @def define_iterfilt_func(T, Name)
* @brief Define a function to turn an #IterFilt(T) into an #Iterable(T).
*
* Define the `next` function implementation for the #IterFilt(T) struct, and use it to implement the Iterator
* typeclass, for given `T`.
*
* The defined function takes in a value of type `IterFilt(T)*` and wraps it in an `Iterable(T)`.
*
* # Example
*
* @code
* DefineIterFilt(int);
*
* // Implement `Iterator` for `IterFilt(int)`
* // The defined function has the signature- `Iterable(int) wrap_intitrfilt(IterFilt(int)* x)`
* define_iterfilt_func(int, wrap_intitrfilt)
* @endcode
*
* Usage of the defined function-
*
* @code
* static bool is_even(int x) { return x % 2 == 0; }
* @endcode
*
* @code
* // Filter `it` (of type `Iterable(int)`) by `is_even`
* Iterable(int) evens = wrap_intitrfilt(&(IterFilt(int)){ .pred = is_even, .src = it });
* @endcode
*
* @param T The type of value the `Iterable` wrapped in this `IterFilt` will yield.
* @param Name Name to define the function as.
*
* @note If `T` is a pointer, it needs to be typedef-ed into a type that does not contain the `*`. Only
* alphanumerics.
* @note An #IterFilt(T) for the given `T` **must** exist.
* @note This should not be delimited by a semicolon.
*/
#define define_iterfilt_func(T, Name) \
static Maybe(T) ITPL_CONCAT(IterFilt(T), _nxt)(IterFilt(T) * self) \
{ \
Iterable(T) const srcit = self->src; \
foreach (T, el, srcit) { \
if (self->pred(el)) { \
return Just(el, T); \
} \
} \
return Nothing(T); \
} \
impl_iterator(IterFilt(T)*, T, Name, ITPL_CONCAT(IterFilt(T), _nxt))
/**
* @def IterFiltMap(ElmntType, FnRetType)
* @brief Convenience macro to get the type of the IterFiltMap struct with given element type and function raw return
* type.
*
* # Example
*
* @code
* DefineIterFiltMap(int, int);
* IterFiltMap(int, int) i; // Declares a variable of type IterFiltMap(int, int)
* @endcode
*
* @param ElmntType The type of value the `Iterable` wrapped in this `IterTake` will yield. Must be the same type name
* passed to #DefineIterFiltMap(ElmntType, FnRetType).
* @param FnRetType The type of value the function contained within an `IterFiltMap` struct will return. Must be the
* same type name passed to #DefineIterFiltMap(ElmntType, FnRetType).
*
* @note If `ElmntType` (or `FnRetType`) is a pointer, it needs to be typedef-ed into a type that does not contain the
* `*`. Only alphanumerics.
*/
#define IterFiltMap(ElmntType, FnRetType) ITPL_CONCAT(ITPL_CONCAT(IterFiltMap_, ElmntType), ITPL_CONCAT(_, FnRetType))
/**
* @def DefineIterFiltMap(ElmntType, FnRetType)
* @brief Define an IterFiltMap struct that maps a function of type `FnRetType (*)(ElmntType)` over an
* `Iterable(ElmntType)`.
*
* # Example
*
* @code
* DefineIterFiltMap(int, int); // Defines an IterFiltMap(int, int) struct
* @endcode
*
* @param ElmntType The type of value the `Iterable` wrapped in this `IterFiltMap` will yield.
* @param FnRetType The **raw** type of value the function contained within an `IterFiltMap` struct will return. A
* #Maybe(T) for given `FnRetType` must exist.
*
* @note If `ElmntType` (or `FnRetType`) is a pointer, it needs to be typedef-ed into a type that does not contain the
* `*`. Only alphanumerics.
* @note An #Iterator(T) for given `ElmntType`, and `FnRetType` **must** also exist.
* @note A filter-map function returns a #Maybe(T), where `T` is `FnRetType`, also referred to as the "raw" return type.
*/