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 ...
随机推荐
- 快速改变文件hash值的方法
查看哈希值命令 Linux : md5sum + 文件名 $ md5sum .png fe5c3f5ef1d207bc1b646911b463c907 .png Windows : certutil ...
- Java并发之等待/通知机制
目录 1 前言 1.1 先来段代码放松一下 2 Object wait()/notify() 2.1 一段入门代码 2.2 问题三连击 a.为什么官方说wait() 要放在while里面? b.为什么 ...
- readiness与liveness
一.liveness(存活探针)方式 HTTP GET:对指定的端口和路径执行http get请求,返回非错误代码即代表正常 TCP socket:对指定端口建立TCP链接,链接通过则代表正常 Exe ...
- OpenVAS开源风险评估系统部署方案
OpenVAS,即开放式漏洞评估系统,是一个用于评估目标漏洞的杰出框架.功能十分强大,最重要的是,它是“开源”的——就是免费的意思啦- 它与著名的Nessus“本是同根生”,在Nessus商业化之后仍 ...
- 【记录】【java】JDK8新特性Stream方式遍历集合
由于是以流方式,所以怎么操作也不改变原来的集合 1.普通遍历forEach List<String> list = new ArrayList(); list.add("a&qu ...
- 腾讯云直播+点播全线产品支持AV1,带来极致视频体验
日前,腾讯视频云直播.点播.媒体处理全线产品均已支持AV1标准,据悉,腾讯云也是国内首家直播+点播同时支持AV1视频处理业务的公有云厂商. 据悉,AV1(Alliance for Open Media ...
- IDEA配置类签名
- Listener学习
监听器Listener用于监听web应用中某些对象.信息的创建.销毁.增加,修改,删除等动作的发生,然后作出相应的响应处理.当范围对象的状态发生变化的时候,服务器自动调用监听器对象中的方法.常用于统计 ...
- PAT甲级1006水题飘过
题目分析:由于不存在相同的两个时间(24:00:00和00:00:00不会同时存在),则我们假设两个全局变量存放到达的最早的时间和达到的最晚的时间,设置最早的初值为“23:59:59”,设置最晚的初值 ...
- F1 相似查找字段存在的table
REPORT ZME12_BDC1. *REPORT demo_select_dynamic_conditions . TABLES: makt,dd02t,marc. TYPE-POOLS : AB ...