learning-cc4e

Solution to https://cc4e.com/. If you copy these you're not right in the head.
git clone https://kaka.farm/~git/learning-cc4e
Log | Files | Refs

main.c (4332B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 struct TreeMapEntry {
      6     char *key;  /* public */
      7     int value;  /* public */
      8     struct TreeMapEntry *__next;
      9     struct TreeMapEntry *__left;
     10     struct TreeMapEntry *__right;
     11 };
     12 
     13 struct TreeMapIter {
     14    struct TreeMapEntry *__current;
     15 
     16    struct TreeMapEntry* (*next)(struct TreeMapIter* self);
     17    void (*del)(struct TreeMapIter* self);
     18 };
     19 
     20 struct TreeMap {
     21    /* Attributes */
     22    struct TreeMapEntry *__head;
     23    struct TreeMapEntry *__root;
     24    int __count;
     25    int debug;
     26 
     27    /* Methods */
     28    void (*put)(struct TreeMap* self, char *key, int value);
     29    int (*get)(struct TreeMap* self, char *key, int def);
     30    int (*size)(struct TreeMap* self);
     31    void (*dump)(struct TreeMap* self);
     32    struct TreeMapIter* (*iter)(struct TreeMap* self);
     33    void (*del)(struct TreeMap* self);
     34 };
     35 
     36 void __TreeMap_del(struct TreeMap* self) {
     37     struct TreeMapEntry *cur, *next;
     38     cur = self->__head;
     39     while(cur) {
     40         free(cur->key);
     41         /* value is just part of the struct */
     42         next = cur->__next;
     43         free(cur);
     44         cur = next;
     45     }
     46     free((void *)self);
     47 }
     48 
     49 void __TreeMapIter_del(struct TreeMapIter* self) {
     50     free((void *)self);
     51 }
     52 
     53 void __TreeMap_dump_tree(struct TreeMapEntry *cur, int depth)
     54 {
     55     int i;
     56     if ( cur == NULL ) return;
     57     for(i=0;i<depth;i++) printf("| ");
     58     printf("%s=%d\n", cur->key, cur->value);
     59     if ( cur->__left != NULL ) {
     60         __TreeMap_dump_tree(cur->__left, depth+1);
     61     }
     62     if ( cur->__right != NULL ) {
     63         __TreeMap_dump_tree(cur->__right, depth+1);
     64     }
     65 }
     66 
     67 void __TreeMap_dump(struct TreeMap* self)
     68 {
     69     struct TreeMapEntry *cur;
     70     printf("Object TreeMap count=%d\n", self->__count);
     71     for(cur = self->__head; cur != NULL ; cur = cur->__next ) {
     72          printf("  %s=%d\n", cur->key, cur->value);
     73     }
     74     printf("\n");
     75     __TreeMap_dump_tree(self->__root, 0);
     76     printf("\n");
     77 }
     78 
     79 /* Run a check to see if left and right are broken */
     80 void __Map_check(struct TreeMap* self, struct TreeMapEntry *left, char *key, struct TreeMapEntry *right)
     81 {
     82     if ( self->debug ) 
     83         printf("Check position: %s < %s > %s\n", (left ? left->key : "0"),
     84             key, (right ? right->key : "0") );
     85 
     86     /* Check our location in the linked list */
     87     if ( left != NULL ) {
     88         if ( left->__next != right ) {
     89             printf("FAIL left->__next != right\n");
     90         }
     91     } else {
     92         if ( self->__head != right ) {
     93             printf("FAIL self->__head != right\n");
     94         }
     95     }
     96 
     97     /* Check our location in the tree */
     98     if ( right != NULL && right->__left == NULL ) {
     99         /* OK */
    100     } else if ( left != NULL && left->__right == NULL ) {
    101         /* OK */
    102     } else {
    103         printf("FAIL Neither right->__left nor left->__right are available\n");
    104     }
    105 }
    106 
    107 #include "student.c"
    108 
    109 int __TreeMap_size(struct TreeMap* self)
    110 {
    111     return self->__count;
    112 }
    113 
    114 struct TreeMapIter* __TreeMap_iter(struct TreeMap* self)
    115 {
    116     struct TreeMapIter *iter = malloc(sizeof(*iter));
    117     iter->__current = self->__head;
    118     iter->next = &__TreeMapIter_next;
    119     iter->del = &__TreeMapIter_del;
    120     return iter;
    121 }
    122 
    123 struct TreeMap * TreeMap_new() {
    124     struct TreeMap *p = malloc(sizeof(*p));
    125 
    126     p->__head = NULL;
    127     p->__root = NULL;
    128     p->__count = 0;
    129     p->debug = 0;
    130 
    131     p->put = &__TreeMap_put;
    132     p->get = &__TreeMap_get;
    133     p->size = &__TreeMap_size;
    134     p->dump = &__TreeMap_dump;
    135     p->iter = &__TreeMap_iter;
    136     p->del = &__TreeMap_del;
    137     return p;
    138 }
    139 
    140 int main(void)
    141 {
    142     struct TreeMap * map = TreeMap_new();
    143     struct TreeMapEntry *cur;
    144     struct TreeMapIter *iter;
    145 
    146     setvbuf(stdout, NULL, _IONBF, 0);  /* Internal */
    147 
    148     map->debug = 1 == 1;
    149 
    150     printf("Testing TreeMap\n");
    151     map->put(map, "h", 42);
    152     map->put(map, "d", 8);
    153     map->put(map, "f", 5);
    154     map->put(map, "b", 123);
    155     map->dump(map);
    156     map->put(map, "k", 9);
    157     map->put(map, "m", 67);
    158     map->put(map, "j", 12);
    159     map->put(map, "f", 6);
    160     map->dump(map);
    161 
    162     printf("r=%d\n", map->get(map, "r", 42));
    163     printf("x=%d\n", map->get(map, "x", 42));
    164 
    165     printf("\nIterate\n");
    166     iter = map->iter(map);
    167     while(1) {
    168         cur = iter->next(iter);
    169         if ( cur == NULL ) break;
    170         printf(" %s=%d\n", cur->key, cur->value);
    171     }
    172     iter->del(iter);
    173 
    174     map->del(map);
    175 }