C.Interface.And.Implementations—set的实现

2024-08-24 18:18

本文主要是介绍C.Interface.And.Implementations—set的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、A set is an  unordered collection of distinct members. 

2、The basic operations on a set are testing  for membership, adding members, and removing members. 

3、Other operations include set union, intersection, difference, and symmetric difference. 


set实现类似于table(key-value系统)的实现,采用类似的“哈希表和链表”的底层数据结构。可以参看上一章的内容。


=============================set.h==============================

#ifndef SET_INCLUDED
#define SET_INCLUDED#define T Set_T
typedef struct T *T;//exported functions
extern T      Set_new   (int hint,int cmp(const void *x, const void *y),unsigned hash(const void *x));extern void   Set_free  (T *set);
extern int    Set_length(T set);
extern int    Set_member(T set, const void *member);
extern void   Set_put   (T set, const void *member);
extern void  *Set_remove(T set, const void *member);
extern void   Set_map   (T set,void apply(const void *member, void *cl), void *cl);
extern void **Set_toArray(T set, void *end);
extern T      Set_union (T s, T t);
extern T      Set_inter (T s, T t);
extern T      Set_minus (T s, T t);
extern T      Set_diff  (T s, T t);#undef
#endif

=============================set.c============================

#include <limits.h>
#include <stddef.h>
#include "mem.h"
#include "assert.h"
#include "arith.h"
#include "set.h"#define T Set_T//types
struct T{int length;unsigned timestamp;int (*cmp)(const void *x, const void *y);unsigned (*hash)(const void *x);int size;struct member{struct member *link;const void *member;} **buckets;
}//static functions
static int cmpatom(const void *x, const void *y){return x != y;
}static unsigned hashatom(const void *x){return (unsigned long)x>>2;
}static T copy(T t, int hint){T set;assert(t);set = Set_new(hint, t->cmp, t->hash);{int i;struct member *q;for(i = 0; i < t->size; ++i){for(q = t->buckets[i]; q; q = q->link){struct member *p;const void *member = q->member;int i = (*set->hash)(member)%set->size;NEW(p);p->member = member;p->link = set->buckets[i];set->buckets[i] = p;set->length++;}}}return set;
}//functions
T Set_new(int hint,int cmp(const void *x, const void *y),unsigned hash(const void *x)){T set;int i;static int primes[] = { 509, 509, 1021, 2053, 4093,8191, 16381, 32771, 65521, INT_MAX };assert(hint >= 0);for(i = 1; primes[i] < hint; ++i){;set = ALLOC(sizeof(*set) +primes[i-1]*sizeof(set->buckets[0]));set->size = primes[i-1];set->cmp = cmp ? cmp : cmpatom;set->hash = hash ? hash : hashatom;set->buckets = (struct member **)(set + 1);for(i = 0; i < set->size; ++i){set->buckets[i] = NULL;}set->length = 0;set->timestamp = 0;return set;
}int Set_member(T set, const void *member){int i;struct member *p;assert(set);assert(member);//search set for memberi = (*set->hash)(member)%set->size;for(p = set->buckets[i]; p; p = p->link){if((*set->cmp)(member, p->member) == 0)break;}return p != NULL;
}void Set_put(T set, const void *member){int i;struct member *p;assert(set);assert(member);//search set for memberi = (*set->hash)(member)%set->size;for(p = set->buckets[i]; p; p = p->link){if((*set->cmp)(member, p->member) == 0)break;}if(p == NULL){//add member to setNEW(p);p->member = member;p->link = set->buckets[i];set->buckets[i] = p;set->length++;}else{p->member = member;}set->timestamp++;
}void *Set_remove(T set, const void *member){int i;struct member **pp;assert(set);assert(member);set->timestamp++;i = (*set->hash)(member)%set->size;for(pp = &set->buckets[i]; *pp; pp = &(*pp)->link){if((*set->cmp)(member, (*pp)->member) == 0){struct member *p = *pp;*pp = p->link;member = p->member;FREE(p);set->length--;retunr (void *)member;}}return NULL;
}int Set_length(T set){assert(set);return set->length;
}void Set_free(T *set){assert(set && *set);if((*set)->length > 0){int i;struct member *p; *q;for(i = 0; i < (*set)->size; ++i){for(p = (*set)->buckets[i]; p; p = q){q = p->link;FREE(p);}}}FREE(*set);
}void Set_map(T set,void apply(const void *member,  void *cl), void *cl){int i;unsigned stamp;struct member *p;assert(set);assert(apply);stamp = set->timestamp;for(i = 0; i < set->size; ++i){for(p = set->buckets[i]; p; p = p->link){apply(p->member, cl);assert(set->timestamp == stamp);}}
}void **Set_toArray(T set, void *end){int i, j = 0;void **array;struct member *p;assert(set);array = ALLOC((set->length + 1)*sizeof(*array));for(i = 0; i < set->size; ++i){for(p = set->buckets[i]; p; p = p->link){array[j++] = (void *)p->member;}}array[j] = end;return array; 
}T Set_union(T s, T t){if(s == NULL){assert(t);return copy(t, t->size);}else if( t == NULL){return copy(s, s->size);}else{T set = copy(s, Arith_max(s->size, t->size));assert(s->cmp == t->cmp && s->hash == t->hash);{//for each member q in tint i;struct member *q;for(i = 0; i < t->size; ++i){for(q = t->buckets[i]; q; q = q->link){Set_put(set, q->member);}}}return set;}
}T Set_inter(T s, T t){if(s == NULL){assert(t);return Set_new(t->size, t->cmp, t->hash);}else if( t == NULL){return Set_new(s->size, s->cmp, s->hash);}else if(s->length < t->length){return Set_inter(t, s);}else{T set = Set_new(Arith_min(s->size, t->size), s->cmp, s->hash);assert(s->cmp == t->cmp && s->hash == t->hash);{int i;struct member *q;for(i = 0; i < t->size; ++i){for(q = t->buckets[i]; q; q = q->link){if(Set_member(s, q->member)){struct member *pconst void *member = q->member;int i = (*set->hash)(member)%set->size;NEW(p);p->member = member;p->link = set->buckets[i];set->buckets[i] = p;set->length++;}}}}return set;}
}T Set_minus(T t, T s){if(t == NULL){assert(s);return Set_new(s->size, s->cmp, s->hash);}else if(s == NULL){return copy(t, t->size);}else{T set = Set_new(Arith_min(s->size, t->size),s->cmp, s->hash);assert(s->cmp == t->cmp && s->hash == t->hash);{int i;struct member *q;for(i = 0; i < t->size; ++i){for(q = t->buckets[i]; q; q = q->link){if(!Set_member(s, q->member)){struct member *p;const void *member = q->member;int i = (*set->hash)(member)%set->size;NEW(p);p->member = member;p->link = set->buckets[i];set->buckets[i] = p;set->length++;}}}}return set;}
}T Set_diff(T s, T t){if(s == NULL){assert(t);return copy(t, t->size);}else if(t == NULL){return copy(s, s->size);}else{T set = Set_new(Arith_min(s->size, t->size),s->cmp, s->hash);assert(s->cmp == t->cmp && s->hash == t->hash);{int i;struct member *q;for(i = 0; i < t->size; ++i){for(q = t->buckets[i]; q; q = q->link){if(!Set_member(s, q->member)){struct member *p;const void *member = q->member;int i = (*set->hash)(member)%set->size;NEW(p);p->member = member;p->link = set->buckets[i];set->buckets[i] = p;set->length++;}}}}{T u = t; t = s; s = u;}{int i;struct member *q;for(i = 0; i < t->size; ++i){for(q = t->buckets[i]; q; q = q->link){if(!Set_member(s, q->member)){struct member *p;const void *member = q->member;int i = (*set->hash)(member)%set->size;NEW(p);p->member = member;p->link = set->buckets[i];set->buckets[i] = p;set->length++;}}}}return set;}
}


这篇关于C.Interface.And.Implementations—set的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1103235

相关文章

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义

opencv图像处理之指纹验证的实现

《opencv图像处理之指纹验证的实现》本文主要介绍了opencv图像处理之指纹验证的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录一、简介二、具体案例实现1. 图像显示函数2. 指纹验证函数3. 主函数4、运行结果三、总结一、

Springboot处理跨域的实现方式(附Demo)

《Springboot处理跨域的实现方式(附Demo)》:本文主要介绍Springboot处理跨域的实现方式(附Demo),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录Springboot处理跨域的方式1. 基本知识2. @CrossOrigin3. 全局跨域设置4.

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

基于SpringBoot实现文件秒传功能

《基于SpringBoot实现文件秒传功能》在开发Web应用时,文件上传是一个常见需求,然而,当用户需要上传大文件或相同文件多次时,会造成带宽浪费和服务器存储冗余,此时可以使用文件秒传技术通过识别重复... 目录前言文件秒传原理代码实现1. 创建项目基础结构2. 创建上传存储代码3. 创建Result类4.

SpringBoot日志配置SLF4J和Logback的方法实现

《SpringBoot日志配置SLF4J和Logback的方法实现》日志记录是不可或缺的一部分,本文主要介绍了SpringBoot日志配置SLF4J和Logback的方法实现,文中通过示例代码介绍的非... 目录一、前言二、案例一:初识日志三、案例二:使用Lombok输出日志四、案例三:配置Logback一

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.