summaryrefslogtreecommitdiff
path: root/doc/aapl/ex_avlimelkey.cpp
blob: 6545d0807fd1a25a0dc37b5dac851c809a128b3a (plain)
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
#include "avlimelkey.h"

/* Forward the element struct so we can pass it to AvliTreeEl. */
struct CustomEl;

/* Make two base structures that will be used by 
 * the tree to resolve ambiguites. */
struct CustomElBase1 : 
        public AvliTreeEl<CustomEl>
{
    CustomElBase1( int key ) : intKey(key) { }

    const int getKey() { return intKey; }
    int intKey;
};

struct CustomElBase2 : 
        public AvliTreeEl<CustomEl>
{
    CustomElBase2( char *key ) : charStarKey(key) { }

    const char *getKey() { return charStarKey; }
    char *charStarKey;
};

struct CustomEl :
        public CustomElBase1,
        public CustomElBase2
{
    /* For our purposes. */
    CustomEl(int intKey, char *charStarKey, const char *data) :
        CustomElBase1(intKey), 
        CustomElBase2(charStarKey), 
        data(data) { }

    const char *data;
};

int main()
{
    /* Specify to AvliMelKey which base element and base key to use. */
    AvliMelKey< CustomEl, int, CustomElBase1, CustomElBase1 > avltree1;
    AvliMelKey< CustomEl, char*, CustomElBase2, CustomElBase2, CmpStr > avltree2;

    /* With AvliMelKey it makes the most sense to new the element ourseves
     * rather than letting the tree do it for use. */
    CustomEl *avlElement = new CustomEl( 1, "strkey", "CustomEl" );

    /* These two calls are completely independant and safe. The first
     * uses the int key whereas the second uses the char* key.  */
    avltree1.insert( avlElement );
    avltree2.insert( avlElement );

    return 0;
}