-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
119 lines (101 loc) · 2.78 KB
/
utils.h
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <structmember.h>
#ifndef UTILS_H
#define UTILS_H
#include <stdio.h>
#include <stdlib.h>
// macro definition
#define BLACK (0)
#define RED (1)
#define RBTNIL (&sentinel)
#define COMPARE_ERR INT_MIN
#define INC_REF 1
#define KEEP_REF 0
#define DEC_REF -1
// whether tree holds duplicated key or not
// if so, node count will increase.
#define NO_DUP 0
#define DUP 1
typedef enum
{
KEY_LONG,
KEY_DOUBLE,
KEY_OBJECT
} KeyType;
typedef union key_tag
{
long lval;
double dval;
PyObject *obj;
} Key;
typedef struct
{
KeyType type;
Key value;
} TypedKey;
typedef struct objnode
{
PyObject *obj;
struct objnode *next;
} ObjNode;
typedef struct rbnode
{
// the key of the object to compare
TypedKey *key;
// linked list of python objects with the same key
ObjNode *obj_list;
// the number of elements of obj_list
unsigned long count;
char color;
// the number of nodes in the subtree rooted at this node
unsigned long size;
struct rbnode *parent;
struct rbnode *left;
struct rbnode *right;
} RBNode;
typedef int (*CompareOperator)(TypedKey *, TypedKey *);
typedef struct
{
PyObject_HEAD RBNode *root;
unsigned long size;
char is_dup;
CompareOperator ope;
PyObject *keyfunc;
PyObject *captured;
} BSTreeObject;
// functions of create and delete rbnodes/objnodes
RBNode *_create_rbnode(TypedKey *);
void _delete_rbnode(RBNode *);
void _delete_all_rbnodes(RBNode *);
ObjNode *_create_objnode(PyObject *);
int _add_objnode_to_rbnode(ObjNode *, RBNode *);
int _delete_obj_from_rbnode(RBNode *);
// functions of reading tree
RBNode *_search(TypedKey *, RBNode *, CompareOperator);
RBNode *_search_leaf(TypedKey *, RBNode *, CompareOperator);
PyObject *_list_in_order(RBNode *, PyObject *, int *, char);
int _add_counter(RBNode *, PyObject *);
RBNode *_get_min(RBNode *);
RBNode *_get_max(RBNode *);
RBNode *_get_next(RBNode *);
RBNode *_get_prev(RBNode *);
long _get_rank(TypedKey *, RBNode *, CompareOperator);
int _helper_smallest(RBNode *, unsigned long, PyObject **);
int _helper_largest(RBNode *, unsigned long, PyObject **);
// functions of writing tree
void _left_rotate(BSTreeObject *, RBNode *);
void _right_rotate(BSTreeObject *, RBNode *);
void _insert_fixup(BSTreeObject *, RBNode *);
void _delete_fixup(BSTreeObject *, RBNode *);
void _update_size(BSTreeObject *, RBNode *);
void _transplant(BSTreeObject *, RBNode *, RBNode *);
// functions of comparing pyobjects
TypedKey *get_key_from(PyObject *, PyObject *);
int _compare(TypedKey *, TypedKey *, CompareOperator);
int _lt(TypedKey *, TypedKey *);
int _lt_obj(PyObject *, PyObject *);
int get_long_from(PyObject *, long *);
int get_double_from(PyObject *, double *);
extern RBNode sentinel;
#endif // UTILS_H