]> mj.ucw.cz Git - libucw.git/blob - ucw/doc/generic.txt
Merge branch 'v3.12.4'
[libucw.git] / ucw / doc / generic.txt
1 Generic data structures and algorithms
2 ======================================
3
4 The C preprocessor is a very powerful tool. One handy way to use it
5 can be generating generic data structures and algorithms. Here you can
6 find some conventions that are used in all such generic structures in
7 libUCW, and also hints for use of these structures.
8
9 - <<idea,General idea>>
10 - <<use,How to use them>>
11 - <<implement,How it is implemented>>
12 - Modules with generics
13   * <<growbuf:gbuf,`gbuf.h`>>
14   * <<binheap:,`binheap.h`>>
15
16 // TODO The module list
17
18 [[idea]]
19 General idea
20 ------------
21
22 The idea is simple. If you have some code, you can customize it a
23 little by preprocessor macros. You can change constants, data types it
24 operates on, whole expressions, or you can compile parts of the code
25 conditionally. You can generate new function names using macros.
26
27 So if you provide few macros for data types, function names and
28 parameters and include some code using them, it gets modified by it
29 and a code for a specific data type is created. Then you can provide
30 new macros and include it again, to get another version of the code,
31 with different function names and types.
32
33 [[use]]
34 How to use them
35 ---------------
36
37 The use is best explained with an example, so we will suppose there
38 is a header file `array.h`, which contains a generic array data type
39 and an indexing function, which returns a pointer to n'th element.
40
41 To get an array of integers, we need to provide macro for used data
42 type and macro that will provide prefixes for identifier names. Then
43 we include the file. Then we could get another array with unsigned
44 integers, so we will do the same:
45
46   #define ARRAY_TYPE int
47   #define ARRAY_PREFIX(name) intarray_##name
48   #include <array.h>
49
50   #define ARRAY_TYPE uns
51   #define ARRAY_PREFIX(name) unsarray_##name
52   #include <array.h>
53
54 This will generate the data types (presumably `intarray_t` and
55 `unsarray_t`) and the index functions (`intarray_index` and
56 `unsarray_index`). We can use them like anything else.
57
58 Maybe the `ARRAY_PREFIX` deserves some attention. When the header file
59 wants to generate an identifier, it uses this macro with
60 some name. Then the macro takes the name, adds a prefix to it and
61 returns the new identifier, so `ARRAY_PREFIX(t)` will generate
62 `intarray_t` in the first case and `unsarray_t` in the second. This
63 allows having more than one instance of the same data structure or
64 algorithm, because it generates different identifiers for them.
65
66 A similar macro is needed for every generic header in libUCW.
67
68 [[implement]]
69 How it is implemented
70 ---------------------
71
72 For those who want to write their own or are just interested, how it
73 works, here is the `array.h` header and some description to it.
74
75   #define ARRAY_A_TYPE ARRAY_PREFIX(t)
76   typedef ARRAY_TYPE *ARRAY_A_TYPE
77
78   static ARRAY_TYPE *ARRAY_PREFIX(index)(ARRAY_A_TYPE array, uns index)
79   {
80     return array + index;
81   }
82
83   #undef ARRAY_A_TYPE
84   #undef ARRAY_TYPE
85   #undef ARRAY_PREFIX
86
87 There are few things that are worth noticing. The first two lines
88 define the data type. The macro (`ARRAY_A_TYPE`) is only for
89 convenience inside the header, since such type names can be used quite
90 often inside the header (if it is large).
91
92 Then there is the function with its name generated (do not get scared
93 by the double parenthesis, ones will be eaten by the macro, the second
94 ones are real function parameters). The function is static, since more
95 than one `.c` file might want to use the same header with the same
96 prefix -- each one generates it's own instance.
97
98 And the end just undefines all the macros, so user may define them
99 again and get another instance of the data structure.
100
101 Also note it is not protected against multiple inclusion in the usual
102 way (eg. `#ifndef ARRAY_H` ...), since multiple inclusion is desired
103 -- it generates multiple versions of the data structure.