IP trie树接口
自己实现了一个IP trie树接口.
在这里保存一下,方便备份以后使用,同时欢迎纠错和交流,希望有大神能指教更高效的算法.
1.头文件如下(iptrie.h)
#ifndef _IP_TRIE_H_
#define _IP_TIRE_H_ #define SPLIT_SIGN "."
#define IP_BINARY_LEN 32 typedef struct ip_trie_node
{
struct ip_trie_node *child[]; //two child node
}ip_trie_node; ip_trie_node *create_iptrie_node(); void insert_iptrie_node(ip_trie_node *root,char ip[]); int select_iptrie_node(ip_trie_node *root,char ip[]); #endif
2.c文件如下(iptrie.c)
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #include "iptrie.h" /*
*name: itobinary
*
*param:
* num: orignal number; binary_str: dest string; index: the binary str copy index
*
*return:
* void
*/
void itobinary(int num,char binary_str[],int index)
{
if(binary_str == NULL)
{
return;
} int i,bit = 0x01;
for(i = ; i < ; i++)
{//conver integer to 8 bit binary str
if((num & bit) != )
{//oprater & is lower than !=
binary_str[index + - i] = '';
}
else
{
binary_str[index + - i] = '';
} bit <<= ; //bit * 2
}
} /*
*name: convert_ip_binary
*
*param:
* ip:orign ip string; binary_str:dest binary string
*
*return:
* void
*/
void convert_ip_binary(char ip[],char binary_str[])
{
if(ip == NULL || binary_str == NULL)
{
return;
} /*为确保正确性在进行转换之前可以进一步进行IP格式校验*/ char *ip_sub = NULL;
int i,index =; ip_sub = strtok(ip,SPLIT_SIGN); //slit ip by . itobinary(atoi(ip_sub),binary_str,index); for(i = ; i < ; i++)
{//need to ip legal detect to pretend error
ip_sub = strtok(NULL,SPLIT_SIGN); index += ;
itobinary(atoi(ip_sub),binary_str,index);
} } /*
*name: create_iptrie_node
*
*return:
* new ip trie node
*/
ip_trie_node *create_iptrie_node()
{
ip_trie_node *node = (ip_trie_node *)calloc(,sizeof(ip_trie_node)); if(node == NULL)
{
perror("create ip trie node error -- calloc");
}
else
{
node->child[] = NULL;
node->child[] = NULL;
} return node;
} /*
*name: insert_iptrie_node
*
*param:
* root: trie root; ip: orignal ip string
*
*return:
* void
*
*notice:
* this function call strtok it will change input ip
* so if input ip need to use at other position
* you shold input a copy of ip
*/
void insert_iptrie_node(ip_trie_node *root,char ip[])
{
if(root == NULL)
{
printf("trie have not init\n"); return;
} if(ip == NULL)
{
return;
} char binary_str[IP_BINARY_LEN + ];
int i,child_index; memset(binary_str,,IP_BINARY_LEN + ); convert_ip_binary(ip,binary_str); //to binary string for(i = ; i < IP_BINARY_LEN; i++)
{
child_index = binary_str[i] - ''; //child is 0 or 1
if(root->child[child_index] == NULL)
{
root->child[child_index] = create_iptrie_node();
} root = root->child[child_index];
}
} /*
*name: select_iptrie_node
*
*param:
* root: trie root; ip: orignal ip string
*
*return:
* 0 :not find; 1:find
*
*notice:
* this function call strtok it will change input ip
* so if input ip need to use at other position
* you shold input a copy of ip
*/
int select_iptrie_node(ip_trie_node *root,char ip[])
{
if(root == NULL)
{
printf("trie have not init\n");
return ;
} if(ip == NULL)
{
return ;
} int i;
char binary_str[IP_BINARY_LEN + ]; memset(binary_str,,IP_BINARY_LEN + ); convert_ip_binary(ip,binary_str); //to binary string int child_index;
for(i = ; i < IP_BINARY_LEN; i++)
{
child_index = binary_str[i] - ''; if(root->child[child_index] == NULL)
{
return ;
} root = root->child[child_index];
} return ;
}
3.main.c如下(测试程序)
#include <stdio.h>
#include <stdlib.h> #include "iptrie.h" int main()
{
char sip[];
char dip[];
int i = ;
int isfind = ;
ip_trie_node *root = create_iptrie_node(); while()
{
printf("insert a ip:\n");
scanf("%s",sip);
insert_iptrie_node(root,sip); printf("query a ip:\n");
scanf("%s",dip);
isfind = select_iptrie_node(root,dip);
if(isfind == )
{
printf("find\n");
}
else
{
printf("not find\n");
}
}
}
4.Makefile (linux下编译)
CC = gcc
CFLAG = -g INC = -I./ target:Iptrie Iptrie:iptrie.o main.c
$(CC) $(CFLAG) $(INC) -o $@ $^ iptrie.o:iptrie.c
$(CC) -c $< clean:
rm *.o Iptrie
IP trie树接口的更多相关文章
- Trie树的应用:查询IP地址的ISP
1. 问题描述 给定一个IP地址,如何查询其所属的ISP,如:中国移动(ChinaMobile),中国电信(ChinaTelecom),中国铁通(ChinaTietong)?现有ISP的IP地址区段可 ...
- 【BZOJ-4523】路由表 Trie树 + 乱搞
4523: [Cqoi2016]路由表 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 155 Solved: 98[Submit][Status][ ...
- Trie树详解(转)
特别声明 本文只是一篇笔记类的文章,所以不存在什么抄袭之类的. 以下为我研究时参考过的链接(有很多,这里我只列出我记得的): Trie(字典树)的应用——查找联系人 trie树 Trie树:应用于统计 ...
- 【Luogu3732】[HAOI2017]供给侧改革(Trie树)
[Luogu3732][HAOI2017]供给侧改革(Trie树) 题面 洛谷 给定一个纯随机的\(01\)串,每次询问\([L,R]\)之间所有后缀两两之间的\(LCP\)的最大值. 题解 一个暴力 ...
- [BinaryTree] AVL树、红黑树、B/B+树和Trie树的比较
转自:AVL树.红黑树.B/B+树和Trie树的比较 AVL树 最早的平衡二叉树之一.AVL是一种高度平衡的二叉树,所以通常的结果是,维护这种高度平衡所付出的代价比从中获得的效率收益还大,故而实际的应 ...
- 【BZOJ4523】[Cqoi2016]路由表 Trie树模拟
[BZOJ4523][Cqoi2016]路由表 Description 路由表查找是路由器在转发IP报文时的重要环节.通常路由表中的表项由目的地址.掩码.下一跳(Next Hop)地址和其他辅助信息组 ...
- HDU 4760 Good Firewall ( Trie树 )
一开始看的时候就想歪了,比赛的时候一直在YY线段树区间覆盖,然后纠结节点数太多开不下怎么办啊啊啊啊…… 然后昨天吃饭的时候也在纠结这到底是个啥题,后来发现公共前缀->前缀??!!!!->这 ...
- AVL树,红黑树,B-B+树,Trie树原理和应用
前言:本文章来源于我在知乎上回答的一个问题 AVL树,红黑树,B树,B+树,Trie树都分别应用在哪些现实场景中? 看完后您可能会了解到这些数据结构大致的原理及为什么用在这些场景,文章并不涉及具体操作 ...
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
随机推荐
- BeetlConfiguration扩展配置
beetl拓展配置类,绑定一些工具类,方便在模板中直接调用 BeetlConfiguration.java public class BeetlConfiguration extends BeetlG ...
- react 常用组件整理
0.es6语法糖 deptList = deptnameList.filter(item => item.rel ===this.state.thUser.orgId);//返回一个新的数组对象 ...
- DIY:从零开始写一个 SQL 构建器
最近在项目中遇到了一个棘手的问题,因为 EF Core 不支持直接生成 Update 语句,所以这个项目就用到了 EFCore.Plus 来实现这个功能,但是 EFCore.Plus 对 SQLite ...
- [数据结构 - 第3章] 线性表之双向链表(C语言实现)
一.什么是双向链表? 双向链表(double linked list)是在单链表的每个结点中,再设置一个指向其前驱结点的指针域.所以在双向链表中的结点都有两个指针域,一个指向直接后继,另一个指向直接前 ...
- 【springcloud】【idea】启动服务报错Command line is too long. Shorten command line for XXXApplication or also for Spring Boot default configuration.
在workspace.xml 在标签<component name="PropertiesComponent">里 添加<property name=" ...
- pvs显示unknown device
一 .不要unknown的那块pv盘的解决办法 [root@gezi ~]# pvs WARNING: Device for PV D1LLfT-3Hle-NbrP-5165-Q6WR-2UWF-2x ...
- Golang修改json文件的两种方法
第三方包 go get -u github.com/tidwall/sjson bytes, _ := ioutil.ReadFile(jsonFile) value1, _ := sjson.Set ...
- springboot+mybatis实现数据库的读写分离
介绍 随着业务的发展,除了拆分业务模块外,数据库的读写分离也是常见的优化手段.方案使用了AbstractRoutingDataSource和mybatis plugin来动态的选择数据源选择这个方案的 ...
- vue2.0版本中v-html中过滤器的使用
Vue 2.0 不再支持在 v-html 中使用过滤器 解决方法: 1:全局方法(推荐) 2:computed 属性 3:$options.filters(推荐) 1:使用全局方法: 可以在 Vue ...
- 对于Node中Express框架的中间件概念的感知
中间件是什么呢? 中间件就是客户端http请求发起传送到服务器和服务器返回响应之间的一些处理函数. 为什么要使用中间件? 通过中间件,可以对数据进行操作使得我们能方便地操作请求数据编写服务器响应.如b ...