OpenSDN source code
patricia_api.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include "patricia_map.h"
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 #include "patricia_api.h"
11 #ifdef __cplusplus
12 }
13 #endif
14 
15 patroot *patricia_root_init(patroot *root, boolean key_is_ptr, uint16_t klen,
16  uint8_t koffset)
17 {
18  patroot *pat_root = NULL;
19 
20  pat_root = (patroot *)malloc(sizeof(patroot));
21  if (!pat_root) {
22  return NULL;
23  }
24 
25  PatriciaMapTable *agent_patroot = new PatriciaMapTable(klen, koffset);
26  if (!agent_patroot) {
27  free(pat_root);
28  return NULL;
29  }
30 
31  pat_root->agent_patroot = agent_patroot;
32 
33  return pat_root;
34 }
35 
37 {
38  if (!root) {
39  return;
40  }
41 
42  PatriciaMapTable *agent_patroot = (PatriciaMapTable *)root->agent_patroot;
43  delete agent_patroot;
44 
45  free(root);
46 
47  return;
48 }
49 
50 boolean patricia_add(patroot *root, patnode *node)
51 {
52  PatriciaMapTable *agent_patroot = (PatriciaMapTable *)root->agent_patroot;
53 
54  PatriciaNode *agent_patnode = new PatriciaNode(agent_patroot->klen_,
55  agent_patroot->koffset_, node, node);
56 
57  bool ret = agent_patroot->Insert(agent_patnode);
58  if (!ret) {
59  delete agent_patnode;
60  return FALSE;
61  }
62 
63  node->agent_patnode = agent_patnode;
64  return TRUE;
65 }
66 
67 boolean patricia_delete(patroot *root, patnode *node)
68 {
69  PatriciaMapTable *agent_patroot = (PatriciaMapTable *)root->agent_patroot;
70 
71  PatriciaNode *agent_patnode = (PatriciaNode *)node->agent_patnode;
72 
73  bool ret = agent_patroot->Remove(agent_patnode);
74  if (!ret) {
75  return FALSE;
76  }
77 
78  delete (PatriciaNode *)node->agent_patnode;
79 
80  return TRUE;
81 }
82 
83 patnode *patricia_lookup(patroot *root, const void *key)
84 {
85  PatriciaMapTable *agent_patroot = (PatriciaMapTable *)root->agent_patroot;
86 
87  PatriciaNode *agent_patnode = new PatriciaNode(agent_patroot->klen_,
88  0, NULL, key);
89  if (!agent_patnode) {
90  return NULL;
91  }
92 
93  PatriciaNode *found = agent_patroot->Find(agent_patnode);
94 
95  delete agent_patnode;
96  if (!found) {
97  return NULL;
98  }
99 
100  patnode *node = (patnode *)found->knode_;
101  return node;
102 }
103 
105 {
106  PatriciaMapTable *agent_patroot = (PatriciaMapTable *)root->agent_patroot;
107 
108  PatriciaNode *agent_patnode = NULL;
109  agent_patnode = agent_patroot->GetNext(agent_patnode);
110  if (!agent_patnode) {
111  return NULL;
112  }
113 
114  patnode *node = (patnode *)agent_patnode->knode_;
115 
116  return node;
117 }
118 
120 {
121  PatriciaMapTable *agent_patroot = (PatriciaMapTable *)root->agent_patroot;
122 
123  PatriciaNode *agent_patnode = agent_patroot->GetLast();
124  if (!agent_patnode) {
125  return NULL;
126  }
127 
128  patnode *node = (patnode *)agent_patnode->knode_;
129 
130  return node;
131 }
132 
134 {
135  PatriciaMapTable *agent_patroot = (PatriciaMapTable *)root->agent_patroot;
136 
137  PatriciaNode *agent_patnode = node ? (PatriciaNode *)node->agent_patnode : NULL;
138  agent_patnode = agent_patroot->GetNext(agent_patnode);
139  if (!agent_patnode) {
140  return NULL;
141  }
142 
143  return (patnode *)agent_patnode->knode_;
144 }
145 
147 {
148  PatriciaMapTable *agent_patroot = (PatriciaMapTable *)root->agent_patroot;
149 
150  PatriciaNode *agent_patnode = (PatriciaNode *)node->agent_patnode;
151  agent_patnode = agent_patroot->GetPrev(agent_patnode);
152  if (!agent_patnode) {
153  return NULL;
154  }
155 
156  return (patnode *)agent_patnode->knode_;
157 }
158 
160 {
161  PatriciaMapTable *agent_patroot = (PatriciaMapTable *)root->agent_patroot;
162 
163  PatriciaNode *agent_patnode = new PatriciaNode(agent_patroot->klen_,
164  agent_patroot->koffset_, node, node);
165  if (!agent_patnode) {
166  return NULL;
167  }
168 
169  PatriciaNode *found = agent_patroot->Find(agent_patnode);
170 
171  if (found) {
172  delete agent_patnode;
173  return (patnode *)found->knode_;
174  }
175 
176  found = agent_patroot->GetNext(agent_patnode);
177 
178  delete agent_patnode;
179  if (!found) {
180  return NULL;
181  }
182 
183  return (patnode *)found->knode_;
184 }
185 
uint8_t koffset_
Definition: patricia_map.h:46
uint16_t klen_
Definition: patricia_map.h:45
void * knode_
Definition: patricia_map.h:33
bool Remove(D *data)
Definition: patricia.h:96
bool Insert(D *data)
Definition: patricia.h:92
D * Find(const D *data)
Definition: patricia.h:100
D * GetNext(D *data)
Definition: patricia.h:112
D * GetPrev(const D *data)
Definition: patricia.h:116
D * GetLast()
Definition: patricia.h:120
#define TRUE
Definition: mcast_common.h:16
#define FALSE
Definition: mcast_common.h:15
boolean patricia_delete(patroot *root, patnode *node)
Definition: patricia_api.cc:67
boolean patricia_add(patroot *root, patnode *node)
Definition: patricia_api.cc:50
patnode * patricia_lookup_geq(patroot *root, patnode *node)
patnode * patricia_lookup(patroot *root, const void *key)
Definition: patricia_api.cc:83
patnode * patricia_get_next(patroot *root, patnode *node)
patnode * patricia_lookup_least(patroot *root)
void patricia_root_delete(patroot *root)
Definition: patricia_api.cc:36
patroot * patricia_root_init(patroot *root, boolean key_is_ptr, uint16_t klen, uint8_t koffset)
Definition: patricia_api.cc:15
patnode * patricia_get_previous(patroot *root, patnode *node)
patnode * patricia_lookup_greatest(patroot *root)
PatriciaMap< PatriciaNode, &PatriciaNode::node_, PatriciaNode::Key > PatriciaMapTable
Definition: patricia_map.h:49
void * agent_patnode
Definition: patricia_api.h:16
void * agent_patroot
Definition: patricia_api.h:12