自己实现了一个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

最新文章

  1. Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)【转】【修改】
  2. 【转】CDH5.x升级
  3. pickle
  4. 【转】BitKeeper与Linux,git史前琐事
  5. 【CITE】C# 如何 实现一个窗体和另一个窗体始终保持相对的位置
  6. VS2013 help viewer搜索结果显示源码以及桌面独立运行help viewer
  7. PDF数据提取------3.解析Demo
  8. DBCONN
  9. Mongo服务器集群配置【转】
  10. android Activity切换动画效果
  11. [Paper Reading]--Exploiting Relevance Feedback in Knowledge Graph
  12. PAT乙级--1003
  13. 树莓派centos安装的基本配置
  14. Dom4j修改xml文档引入
  15. 最新.net和Java调用SAP RFC中间件下载
  16. Apache SkyWalking的架构设计【译文】
  17. 安装 Xshell 5/6 时出现.dll以及0xc000007错误的解决
  18. c# winform 自动升级
  19. 微软职位内部推荐-Senior Software Engineer-Eco
  20. selenium Java-1 配置

热门文章

  1. 【NPDP笔记】第五章 工具与度量
  2. 1-2docker-基本的使用
  3. docker添加mongo4.0.3并配置复制集
  4. c#之break和continue的区别
  5. react中的ref在input中的详解
  6. RDA的使用和说明
  7. 解决非controller使用@Autowired注解注入为null问题
  8. golang函数式编程
  9. 用LabVIEW做声源定位系统
  10. 插件部署【BE、BP、UI】