CPARSEC3 Base library provides basic functionality of general purpose Generic Data Types, Traits, and Generic Macros.
Generic Data Type is a parametric typed struct/union.
Generic Data Type is a similar concept as known as parameterized type, polymorphic type, template class (C++), and so on.
The below sections show Generic Data Types provided by CPARSEC3 Base library.
- Array(T)
-
Array of objects of
Ttype.
See alsoArrayT(T)trait.
- List(T)
-
Linked-list of objects of
Ttype orNULL.
See alsoListT(T)trait.
- Maybe(T)
-
Wraps an object of
Ttype or nothing.
See alsoMaybeT(T)trait.
- Itr(C)
-
An iterator that points to an item (or nothing) of the container of
Ctype. AnItr(C)object is used to operate iteration / traversal for each item of a container ofCtype.
See alsoItrT(C)trait.
- Slice(C)
-
A partial (or whole) view of the underlying containter of
Ctype. In other words, aSlice(C)object is a reference to a portion of underlying container. ASlice(C)object is also iterable (i.e.Itr(Slice(C))is available). TheCmust be iterable (i.e.Itr(C)must be available). ThereforeCshall beArray(T)orList(T)for now.
See alsoSliceT(C)trait.
- Result(T, E)
-
Wraps an ok value of
Ttype or an error value ofEtype.
- Tuple(T1)
-
1-tuple of a value of
T1type. - Tuple(T1, T2)
-
2-tuple of values of
T1andT2type. - Tuple(T1, T2, T3)
-
3-tuple of values of
T1,T2, andT3type. - Tuple(T1, T2, T3, T4)
-
4-tuple of values of
T1,T2,T3, andT4type. - Tuple(T1, T2, T3, T4, T5)
-
5-tuple of values of
T1,T2,T3,T4, andT5type. - Tuple(T1, T2, T3, T4, T5, T6)
-
6-tuple of values of
T1,T2,T3,T4,T5, andT6type.
Trait provides a set of functions against a particular concrete type.
Trait is a similar concept as known as type class (Haskell), trait (Rust), interface (Java), and so on.
To use Trait, calling to trait(M) creates a concrete trait object.
The below sections show Traits provided by CPARSEC3 Base library.
/* Eq(String) is a trait type to test equality of two String values */
Eq(String) E = trait(Eq(String));
if (E.eq("foo", "foo")) { printf("foo == foo\n"); }
if (E.neq("foo", "foo")) { printf("foo != foo\n");}
// -> foo == foo- Eq(T)
-
Type of a trait that provides a set of functions to test equality of two
values of
Ttype. - trait(Eq(T))
-
Constructs a new trait of
Eq(T)type.
For example,trait(Eq(int))creates an object ofEq(int)type.
An Eq(T) trait provides the following member functions :
- bool eq(T a, T b)
- Returns
trueif a = b - bool neq(T a, T b)
- Returns
trueif a ≠ b
String s = "foo";
Ord(String) O = trait(Ord(String));
if (O.le("foo", "bar")) {
printf("foo <= bar\n")
}
if (O.lt("foo", "bar")) {
printf("foo < bar\n")
}
if (O.ge("foo", "bar")) {
printf("foo >= bar\n")
}
if (O.gt("foo", "bar")) {
printf("foo > bar\n")
}
printf("%d\n", O.compare("foo", "bar"));- Ord(T)
-
Type of a trait that provides a set of functions to compare two values of
Ttype. - trait(Ord(T))
-
Constructs a new trait of
Ord(T)type.
For example,trait(Ord(int))creates an object ofOrd(int)type.
An Ord(T) trait provides the following member functions :
- bool le(T a, T b)
- Returns
trueif a ≤ b - bool lt(T a, T b)
- Returns
trueif a < b - bool ge(T a, T b)
- Returns
trueif a ≥ b - bool gt(T a, T b)
- Returns
trueif a > b - T min(T a, T b)
- Returns
aif a ≤ b,botherwise. - T max(T a, T b)
- Returns
aif a ≥ b,botherwise. - int compare(T a, T b)
- Returns
- -1 if a < b,
- 0 if a = b, or
- 1 if a > b
MemT(int) m = trait(Mem(int));
int* p = m.create(5); /* allocate int[5] */
m.free(p); /* free memoty */- MemT(T)
-
Type of a trait that provides a set of functions to malloc/free memory of
Ttype. - trait(Mem(T))
-
Constructs a new trait of
MemT(T)type.
For example,trait(Mem(int))creates an object ofMemT(int)type.
An MemT(T) trait provides the following member functions :
- T* create(size_t n)
-
Allocates
T[n]and returns the address of head of the allocated memory.
i.e. returnsmalloc(sizeof(T) * n). - T* recreate(T* ptr, size_t n)
-
Re-allocates
T[n]and returns the new address of head of the allocated memory.
i.e. returnsrealloc(ptr, sizeof(T) * n). - void free(T* ptr)
-
Deallocates (free) the allocated memory.
i.e.free(ptr).
/* ArrayT(int) is a trait type to construct, destruct, and manipulate Array(T) */
ArrayT(int) m = trait(Array(int));
Array(int) a = m.create(5);
size_t n = m.length(a); /* -> n = 5 */
for (int* it = m.begin(a); it != m.end(a); *it++ = n--)
;
for (int* it = m.begin(a); it != m.end(a); it++) {
printf("%d ", *it); /* -> 5 4 3 2 1 */
}
m.free(&a);- ArrayT(T)
-
Type of a trait that provides a set of functions to construct, destruct,
and manipulate
Array(T). - trait(Array(T))
-
Constructs a new trait of
ArrayT(T)type.
For example,trait(Array(int))creates an object ofArrayT(int)type.
An ArrayT(T) trait provides the following member variables/functions :
- Array(T) empty
-
An empty array.
i.e. 0-length array. - bool null(Array(T) a)
-
Returns
trueif length of the arrayawas 0,falseotherwise.
i.e. returns!a.length - size_t length(Array(T) a)
-
Returns the length of the array
a.
i.e. returnsa.length. - Array(T) create(size_t n)
-
Constructs an array of length
n. - void free(Array(T)* pa)
-
Destructs the array that the
papoints to.
i.e.free(pa->data). - T* begin(Array(T) a)
-
Returns the address of the 1st item of the array
a.
i.e. returnsa.data. - T* end(Array(T) a)
-
Returns the out of bounds address over the last item of the array
a.
i.e. returnsa.data + a.length. - void reverse(Array(T)* ptr)
-
Reverses the order of items in the array that the pointer
ptrpoints to. Note that theptrmust not beNULL.
ListT(int) m = trait(List(int));
List(int) xs = m.cons(1, m.cons(2, m.cons(3, m.empty)));
for (List(int) ys = xs; !m.null(ys); ys = m.tail(ys)) {
printf("%d ", m.head(ys)); /* -> 1 2 3 */
}
m.free(&xs);- ListT(T)
-
Type of a trait that provides a set of functions to construct, destruct,
and manipulate
List(T). - trait(List(T))
-
Constructs a new trait of
ListT(T)type.
For example,trait(List(int))creates an object ofListT(int)type.
An ListT(T) trait provides the following member variables/functions :
- List(T) empty
-
An empty list.
i.e.NULL - bool null(List(T) xs)
-
Returns
trueif the listxswasNULL,falseotherwise.
i.e. returns!xs. - size_t length(List(T) xs)
-
Returns the length of the list
xs. - List(T) cons(T x, List(T) xs)
- Constructs a linked-list (cons-cell).
- void free(List(T)* pxs)
-
Destructs the list that the
pxspoints to. - List(T) drop(size_t n, List(T) xs)
-
Destructs leading at most
ncells of the listxs, and returns remaining list. - T head(List(T) xs)
-
Returns the head value of the list
xs.
i.e. returnsxs->head. - List(T) tail(List(T) xs)
-
Returns the tail list of the list
xs.
i.e. returnsxs->tail. - void reverse(List(T)* ptr)
-
Reverses the order of items in the list that the pointer
ptrpoints to. Note that theptrmust not beNULL.
/* MaybeT(int) is a trait type to construct and manipulate Maybe(T) */
MaybeT(int) M = trait(Maybe(int));
Maybe(int) m = M.just(5);
assert(M.length(m) == 1);
assert(!M.null(m));
assert(!m.none);
assert(m.value == 5);
Maybe(int) e = M.empty;
assert(M.length(e) == 0);
assert(M.null(e));
assert(e.none);
int x = e.value; // undefined behaviour- MaybeT(T)
-
Type of a trait that provides a set of functions to construct and
manipulate
Maybe(T). - trait(Maybe(T))
-
Constructs a new trait of
MaybeT(T)type.
For example,trait(Maybe(int))creates an object ofMaybeT(int)type.
A MaybeT(T) trait provides the following member variables/functions :
- Maybe(T) empty
-
An object that represents nothing.
i.e.empty.none=true. - bool null(Maybe(T) m)
-
Returns
trueif themwas nothing,falseotherwise.
i.e. returnsm.none - size_t length(Maybe(T) m)
-
Returns 0 if the
mwas nothing, 1 otherwise.
i.e. returns(m.none ? 0 : 1). - Maybe(T) just(T value)
-
Constructs a Maybe(T) object that represents the
value.
i.e. returns(Maybe(T)){.none = false, .value = value}.
Iterable(List(int)) J = trait(Iterable(List(int)));
ItrT(List(int)) I = trait(Itr(List(int)));
ListT(int) m = trait(List(int));
List(int) xs = m.cons(1, m.cons(2, m.cons(3, m.empty)));
for (Itr(List(int)) it = J.itr(xs); !I.null(it); it = I.next(it)) {
printf("%d ", I.get(it)); /* -> 1 2 3 */
}
m.free(&xs);- ItrT(C)
-
Type of a trait that provides a set of functions to construct and
manipulate
Itr(C). - Item(C)
-
Type of an item of
Ctype.- NOTE :
Item(C)type is a type alias ofTwhenCwasArray(T)orList(T).
- NOTE :
- trait(Itr(C))
-
Constructs a new trait of
ItrT(C)type.
For example,trait(Itr(List(int)))creates an object ofItrT(List(int))type.
An ItrT(C) trait provides the following member variables/functions :
- Itr(C) itr(C c)
-
Constructs an iterator that points to the 1st item (or nothing) of the
container
c. The type ofcshall be aArray(T)or aList(T). - Item(C)* ptr(Itr(C) it)
-
Returns the pointer to the container’s item of that the iterator
itpoints to.- NOTE :
Item(C)* ptr(Itr(C) it)is introduced for easy to implement typical iterator.ptr(it)is called from typical implementation ofnull(it),get(it), andset(v, it).- Use
null(it),get(it), orset(v, it)instead ofptr(it)unless you have necessary to callptr(it).
- NOTE :
- Itr(C) next(Itr(C) it)
-
Returns the iterator that points to the next item (or nothing) of the
iterator
itpoints to.
- NOTE : If the iterator
itwas empty (i.e.null(it)=`true`), causes assertion failed.
- NOTE : If the iterator
- Itr(C) skip(size_t n, Itr(C) it)
-
Skips at most `n` items of the
itand returns the iterator followed.- NOTE : Returns an empty iterator (i.e. an iterator that points to
nothing), if
nwas greater than or equals to the length of the sequence represented by the iteratorit.
- NOTE : Returns an empty iterator (i.e. an iterator that points to
nothing), if
- bool null(Itr(C) it)
-
Returns
trueifitwas empty (i.e.itpoints to nothing), otherwisefalse. - Item(C) get(Itr(C) it)
-
Returns the value of the container’s item of that the iterator
itpoints to. - void set(Item(C) v, Itr(C) it)
-
Assign the value
vto the container’s item of that the iteratoritpoints to.
ListT(int) L = trait(List(int));
SliceT(List(int)) S = trait(Slice(List(int)));
Iterable(Slice(List(int))) J = trait(Iterable(Slice(List(int))));
ItrT(Slice(List(int))) I = trait(Itr(Slice(List(int))));
List(int) xs = L.cons(1, L.cons(2, L.cons(3, L.cons(4, L.cons(5, L.empty)))));
Slice(List(int)) s = S.slice(xs, 0, 3);
for (Itr(Slice(List(int))) it = J.itr(xs); !I.null(it); it = I.next(it)) {
printf("%d ", I.get(it)); /* -> 1 2 3 */
}
m.free(&xs);- SliceT(C)
-
Type of a trait that provides a set of functions to construct and
manipulate
Slice(C). - trait(Slice(C))
-
Constructs a new trait of
SliceT(C)type.
For example,trait(Slice(List(int)))creates an object ofSliceT(List(int))type.
An SliceT(C) trait provides the following member variables/functions :
- Slice(C) empty
- An empty slice.
- bool null(Slice(C) s)
-
Returns
trueif the sliceswas empty,falseotherwise. - size_t length(Slice(C) s)
-
Returns the length of the slice
s. - Slice(C) slice(C c, int start, int stop)
-
Constructs a new
Slice(C)object that is a reference to a portion of underlying containercofCtype. Returns an emptySlice(C)object iff(start)≥f(stop). Otherwise, returns aSlice(C)object refering to a range of thecthat starts with indexf(start)(included) and ends with indexf(stop)(excluded), where
f(x)=xif 0 ≤x≤length(c)f(x)=length(c)iflength(c)≤xf(x)=x+length(c)ifx< 0 and 0 ≤x+length(c)f(x)= 0 ifx+length(c)< 0- note that always 0 ≤
length(c)
- void reverse(Slice(C)* ptr)
-
Reverses the order of items in the range of contaner refered by the slice
that the pointer
ptrpoints to. Note that theptrmust not beNULL.
- NOTE : Not implemented yet.
- Show(T)
-
Type of a trait that provides a set of functions to represent a value of
Ttype as aString. - trait(Show(T))
-
Constructs a new trait of
Show(T)type.
For example,trait(Show(int))creates an object ofShow(int)type.
An Show(T) trait provides the following member functions :
- String show(T a)
- Returns a
Stringrepresentation ofa.
CPARSEC3 provides also Generic Macros for easy to use various traits and containers.
- Pros of Generic Macros
- Makes it easy to use various traits and containers.
- Cons of Generic Macros
- Needs much more compile time / memory.
The below sections show Generic Macros provided by CPARSEC3 Base library.
- g_eq(a, b)
-
Returns
trueifa=b,falseotherwise. - g_neq(a, b)
-
Returns
trueifa≠b,falseotherwise.
- g_le(a, b)
-
Returns
trueifa≤b,falseotherwise. - g_lt(a, b)
-
Returns
trueifa<b,falseotherwise. - g_ge(a, b)
-
Returns
trueifa≥b,falseotherwise. - g_gt(a, b)
-
Returns
trueifa>b,falseotherwise. - g_min(a, b)
-
Returns
aifa≤b,botherwise. - g_max(a, b)
-
Returns
aifa≥b,botherwise. - g_cmp(a, b)
-
- Returns -1 if
a<b - Returns 0 if
a=b - Returns 1 if
a>b
- Returns -1 if
- g_compare(a, b)
-
Same as
g_cmp(a, b).
- g_array(T, …)
-
Constructs a new
Array(T)object.
For example,g_array(int, 1, 2, 3)creates a 3 length array. - g_begin(a)
-
Returns the address of the 1st item of the array
a. - g_end(a)
-
Returns the out of bounds address over the last item of the array
a. - g_free(a)
-
Destructs the array
a.
- g_list(T, …)
-
Constructs a new
List(T)object.
For example,g_list(int, 1, 2, 3)creates a 3 length list. - g_cons(x, xs)
-
Constructs a new
List(T)object.
xshall be aTandxsshall be aList(T). - g_head(xs)
-
Returns the head value of the list
xs. - g_tail(xs)
-
Returns the tail list of the list
xs. - g_drop(n, xs)
-
Destructs leading at most
ncells of the listxs, and returns remaining list. - g_free(xs)
-
Destructs the list
xs.
- g_slice(c, start, stop)
-
Constructs a new
Slice(C)object that is a reference to a portion of underlying containercofCtype. Returns an emptySlice(C)object iff(start)≥f(stop). Otherwise, returns aSlice(C)object refering to a range of thecthat starts with indexf(start)(included) and ends with indexf(stop)(excluded), where
f(x)=xif 0 ≤x≤length(c)f(x)=length(c)iflength(c)≤xf(x)=x+length(c)ifx< 0 and 0 ≤x+length(c)f(x)= 0 ifx+length(c)< 0- note that always 0 ≤
length(c)
- g_slice(c, n)
-
Same as
g_slice(c, 0, n). This is a syntax sugar to construct a slice that refers the leading portion of the container.
- g_null(c)
-
Returns
trueifcwas empty, otherwisefalse.
cshall be anArray(T),List(T),Maybe(T),Slice(Array(T)), orSlice(List(T)). (see alsog_null(it)) - g_length(c)
-
Returns length of the container
c.
cshall be anArray(T),List(T),Maybe(T),Slice(Array(T)), orSlice(List(T)).
- g_reverse(c)
-
Reverses the order of items in the
c. Note that thecmust be a lvalue.
cshall be anArray(T),List(T),Slice(Array(T)), orSlice(List(T)).
- g_itr(c)
-
Constructs an iterator
Itr(C)object.
cshall be anArray(T),List(T),Slice(Array(T)), orSlice(List(T)). - g_null(it)
-
Returns
trueifitwas empty, otherwisefalse.
itshall be anItr(Array(T)),Itr(List(T)),Itr(Slice(Array(T)), orItr(Slice(List(T)). (see alsog_null(c)) - g_next(it)
-
Returns the next iterator of the
it.
itshall be anItr(Array(T)),Itr(List(T)),Itr(Slice(Array(T)), orItr(Slice(List(T)). - g_skip(n, it)
-
Skips at most `n` items of the
itand returns the iterator followed.
itshall be anItr(Array(T)),Itr(List(T)),Itr(Slice(Array(T)), orItr(Slice(List(T)). - g_get(it)
-
Returns the value of container’s item of that the iterator
itpoints to.
itshall be anItr(Array(T)),Itr(List(T)),Itr(Slice(Array(T)), orItr(Slice(List(T)). - g_set(v, it)
-
Assign the value
vto the container’s item of that the iteratoritpoints to.
itshall be anItr(Array(T)),Itr(List(T)),Itr(Slice(Array(T)), orItr(Slice(List(T)), thusvshall be aT.
- g_for(it, c)
-
Expanded to
for (__auto_type it = g_itr(c); !g_null(it); it = g_next(it)).
itis the name of variable to be used as an iterator,cshall be anArray(T),List(T),Slice(Array(T)), orSlice(List(T)).- NOTE : Available if and only if
__GNUC__was defined.
- NOTE : Available if and only if
- g_for(it, c, step)
-
Expanded to
for (__auto_type it = g_itr(c); !g_null(it); it = g_skip(step, it)).
itis the name of variable to be used as an iterator,cshall be anArray(T),List(T),Slice(Array(T)), orSlice(List(T)).- NOTE : Available if and only if
__GNUC__was defined.
- NOTE : Available if and only if
- g_bind((a,b,…), t)
-
Expanded to
__auto_type a = t.e1; __auto_type b = t.e2; ....
(a,b,...)is a list of the name of variables to be defined.tis a tuple. For each item in the tuplet, the item is assigned to the variable in the given list in order. If a variable in the list was not specified, the corresponding assignment is omitted.- NOTE : Available if and only if
__GNUC__was defined.
- NOTE : Available if and only if
{
g_bind((a,b,c), (Tuple(int, char, String)){1, 'X', "hello"});
// -> __auto_type a = 1; __auto_type b = 'X'; __auto_type c = "hello";
}
{
g_bind((a,b), (Tuple(int, char, String)){1, 'X', "hello"});
// -> __auto_type a = 1; __auto_type b = 'X';
}
{
g_bind(a, (Tuple(int, char, String)){1, 'X', "hello"});
// -> __auto_type a = 1;
}
{
g_bind((a,,c), (Tuple(int, char, String)){1, 'X', "hello"});
// -> __auto_type a = 1; __auto_type c = "hello";
}- g_scoped(C) c = …;
-
Declares/defines a variable
cof typeCas a RAII object that will be automatically deallocated when leave the current scope.
Cshall beArray(T)orList(T).- NOTE : Available if and only if
__GNUC__was defined.
- NOTE : Available if and only if
void foo(void) {
g_scoped(Array(int)) a = g_array(int, 1, 2, 3, 4, 5);
// do something...
} // at here, g_free(a) is automatically called before leaving the function.