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