Multibit trie implementation for ip lookup? - ip

I have to implement multibit trie for IP lookup, but I am not able to implement multibit trie approach in C.
I have searched on many sites and found the method:-
typedef struct node {
struct node* table[256];
Port_type port;
} Node;
Node* root[256];
Using the above code create a table for multibit and store the corresponding prefix in the array.
Does anybody know how to implement it in C or C++?

Related

Can someone explain how the recursion in this structure works?

What is "next" here? Please explain how recursion works here? Thanks
typedef struct node {
int val;
struct node * next;
} node_t;
next is a pointer called next to a struct node.
The definition is recursive because you use a reference of what you define inside the definition.
From Wikipedia
recursive data type (also known as a recursively-defined, inductively-defined or inductive data type) is a data type for values that may contain other values of the same type.
It's like saying a node_t is a data type that consists in a struct that has two fields, one is an int called val, the other called next by the programmer is a pointer to a node_t (here is the recursive definition).

Good Practice to pass arguments in queued connection

I want an simple and clear example of how to do the signal and slot mechanism in queued connection.
Take the below line as example. Think that obj1 is backend functionality object emitting signal asynchronously from another thread and is connected to a slot in GUI ( main thread):
connect(obj1, SIGNAL(Mysignal(vector<mystruct> )), this, slot(myslot(vector <mystruct>)))
I have read that you have to register the types(meta types).
Please give a clear, simple and ready to use code lines for the above example that I would need, so that errors during run time like vector, my struct or string not defined, etc.. I don't face.
Also, is there a better way to handle this like sending pointers like:
connect(obj1, SIGNAL(Mysignal(obj2 *)), this, slot(myslot(obj2 *)))
Obj2 contains the vector of mystruct. Will i still need to register the obj2 with those metatypes?
If somebody has experience in this, please share all your good practices and simple code snippets, I am new to the queued connections with arguments. Please help.
If you want a queued connection, you need to call connect with a 5. parameter Qt::QueuedConnection. Otherwise, you get a direct connection inside the thread where you sent the signal from. Edit: See Tobys comment below.
You must wrap a QVector<> into a typedef, otherwise registering will not work (bug? in Qt from the stoneage). Also do not use references to your typedef, will not work either.
Header
typedef struct {
int a;
int b;
} mystruct;
typedef QVector<mystruct> myvector;
Q_DECLARE_METATYPE(myvector);
Source
void MainWindow::test()
{
qRegisterMetaType<myvector>();
connect(this, SIGNAL(sigRec(myvector)), SLOT(slotRec(myvector)), Qt::QueuedConnection);
mystruct x = {1,2};
myvector v;
v.append(x);
emit sigRec(v);
}
void MainWindow::slotRec(myvector s)
{
}

A pointerless node in c++

How could you make a pointerless node. A node that does not contain pointers.
This would be user for a linkedlist or something
Also how can you insert into a pointerless node.
The other parts can have pointers. Just the node cannot.
The question is a bit vague. A linked list uses pointers by definition. So pointerless nodes cannot exist and would be pointless. You could split out the pointer stuff from the rest of the payload so that the pointer structure contains pointers to previous, next and payload; is that what you mean? Like this:
struct payload
{
// data defs but no pointers
};
struct node
{
struct node *next, *prev;
struct payload *payload;
};

interfacing Rust with Berkeley DB

I have an existing C++ program that uses Berkeley DB as a storage backend. I would like to rewrite it in Rust. Is there a way to write a Foreign Function Interface in Rust to use Berkeley DB? I have found the tutorial Rust Foreign Function Interface, but it seems too simple an example for the complicated C structs used in BDB; for example, to open a database
I need to declare a DB struct and call DB->open(). But I don't know how to do this using the example shown in the tutorial.
Can anyone help with this?
Well, looking into the C API of BDB I found out that it consists of C structures with elements-pointers to functions. It is not explained in the tutorial (which is very strange), but Rust currently supports pointers to foreign functions. It is also mentioned in Rust reference manual.
You can create all required structures roughly based on the ones defined in db.h, and since Rust and C structures memory layout is the same you can pass these structures to/from the library and expect correct pointers to be present in them.
For example, your DB->open() call could look like this:
struct DB {
open: extern "C" fn()
}
let db = ... // Get DB from somewhere
(db.open)() // Parentheses around db.open are needed to disambiguate field access
This, however, really should be wrapped in some kind of impl-based interface because calling extern functions is unsafe operation, and you do not want your users to put unsafe around all database interactions.
Given the sheer size and complexity of the DB struct, there doesn't appear to be a "clean" way to expose the whole thing to Rust. A tool similar to C2HS to generate the FFI from C headers would be nice, but alas we don't have one.
Note also that the Rust FFI can't currently call into C++ libraries, so you'll have to use the C API instead.
I'm not familiar with the DB APIs at all, but it appears plausible to create a small support library in C to actually create an instance of the DB struct, then expose the public members of the struct __db via getter and setter functions.
Your implementation might look something like this:
[#link_args = "-lrust_dbhelper"]
extern {
fn create_DB() -> *c_void;
fn free_DB(db: *c_void);
}
struct DB {
priv db: *c_void
}
impl Drop for DB {
fn drop(&self) {
free_DB(self.db);
}
}
priv struct DBAppMembers {
pgsize: u32,
priority: DBCachePriority
// Additional members omitted for brevity
}
impl DB {
pub fn new() -> DB {
DB {
db: create_DB()
}
}
pub fn set_pgsize(&mut self, u32 pgsize) {
unsafe {
let x: *mut DBAppMembers = ::std::ptr::transmute(self.db);
x.pgsize = pgsize;
}
}
// Additional methods omitted for brevity
}
You can save yourself from some additional work by specifically calling C functions with the DB.db member as a parameter, but that requires working in an unsafe context, which should probably be avoided where possible. Otherwise, each function exported by libdb will need to have its own wrapper in your native struct DB.

Passing a pointer based struct to cuda

I have C code which uses a pointer to a struct. I'm trying to figure out how to pass it to cuda without much luck.
I have
typedef struct node { /* describes a tip species or an ancestor */
struct node *next, *back; /* pointers to nodes */
etc...
} node;
Then
typedef node **pointptr;
static pointptr treenode;
In my code I iterate through all of these, and I'm trying to figure out how to pass them to the kernel so I can perform the following operation:
for (i = 1; i <= nonodes; i++) {
treenode[i - 1]->back = NULL;
etc....
}
But I can't figure out how to pass it.
Any ideas?
The problem is that in order to use your tree inside the kernel, your next and back should probably point somewhere in device memory. Assuming you construct your tree on the host and then pass it, you could do something like:
node* traverse(node*n){
if (n==NULL)
return NULL;
node x, *d;
x.back = traverse(n->back);
x.next = traverse(n->next);
cudaMalloc(&d, sizeof(node));
cudaMemcpy(d, &x, sizeof(node), cudaMemcpyHostToDevice);
return d;
}
and by calling it on the root you'd end up with a pointer to the root of the tree in device memory, which you could pass to your kernel directly. I haven't tested this code, and you'd have to write something similar to delete the tree afterwards.
Alternatively, you could store your tree nodes contiguously inside an array, with indices in the back and next instead of pointers (possibly changing them back to pointers in device code if necessary).
Check this question:
Copying a multi-branch tree to GPU memory
Although it does not answer your question exactly, I think it may clear some things out and ultimately help you tackle your problem.

Resources