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