-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_base.h
More file actions
67 lines (52 loc) · 2.19 KB
/
Copy pathlinked_base.h
File metadata and controls
67 lines (52 loc) · 2.19 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef _LINKED_STRUCTURES_BASE_H_
#define _LINKED_STRUCTURES_BASE_H_
#define LINKED_MALLOC malloc
#define LINKED_FREE free
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <stdlib.h>
typedef struct Object_vtable Object_vtable;
typedef struct Comparable_vtable Comparable_vtable;
typedef struct Comparator_vtable Comparator_vtable;
#undef TRUE
#undef FALSE
typedef enum BOOLEAN { FALSE=0, TRUE=-1, MAYBE=-2 } BOOLEAN;
typedef struct Object {
const Object_vtable *method;//also can be used as a key for the function type
} Object;
typedef uint64_t hash_t;
struct Object_vtable {//the most basic form of an object
hash_t (*hash)(const Object *self);
char* (*hashable)(const Object *self, size_t *size); //part of object that is hashable
void (*destroy)(const Object *self); //how to destroy our data, NOT deallocate!
Object* (*copy)(const Object *self, void* buffer); //return a copy of the information
BOOLEAN (*equals)(const Object *self, const void* oth);
size_t size;
};
struct Comparable_vtable {//an interface for comparable objects
long (*compare)(const void* self, const void* oth); //how to compare (NULL if undesired)
};
typedef struct Comparator {
const Comparator_vtable *method;
} Comparator;
struct Comparator_vtable {
long (*compare)(const void* self, const void* oth1, const void* oth2);
};
struct Iterator_vtable {
Object_vtable parent;
void* (*next)(const Object *self);
};
#define CALL_POSSIBLE(OBJ, METHOD) (OBJ && OBJ->method && OBJ->method->METHOD)
#define CALL(OBJ, METHOD, ELSE, ...) (CALL_POSSIBLE(OBJ, METHOD)?(OBJ->method->METHOD((void*)OBJ, ## __VA_ARGS__)):(ELSE))
#define CALL_VOID(OBJ, METHOD, ...) do{ if(CALL_POSSIBLE(OBJ, METHOD)) OBJ->method->METHOD ((void*)OBJ, ## __VA_ARGS__); } while(0)
typedef BOOLEAN (*lMapFunc)(Object *data, const void *aux/*auxilarly data (constant between calls)*/, void* node); //a mapping function
typedef BOOLEAN (*lTransFunc)(Object **data, const void* aux, const void* node);/*in-place data transformation, should always return TRUE as FALSE means stop*/
typedef enum TRAVERSAL_STRATEGY {
BREADTH_FIRST=0,
DEPTH_FIRST_PRE=1,
DEPTH_FIRST=2,
DEPTH_FIRST_IN=2,
DEPTH_FIRST_POST=3,
} TRAVERSAL_STRATEGY;
#endif