PSet

这段代码实现了一个PSet,即Positive Set,是GSet的扩展。PSet是一个集合,支持添加和删除元素,但是不支持重复元素。PSet的实现是通过两个GSet来实现的,一个GSet存储添加的元素,另一个GSet存储删除的元素。value函数返回PSet中的元素,add函数向PSet中添加元素,rem函数从PSet中删除元素,merge函数将两个PSet合并。其中,GSet是一个Grow-only Set,即只支持添加元素,不支持删除和修改元素。

PSet是一个集合,支持添加和删除元素,但是不支持重复元素。下面是各个函数的功能:

  • PSet():创建一个空的PSet。
  • zero():创建一个空的PSet。
  • value(a: PSet):返回PSet中的元素。
  • add(a: PSet, value):向PSet中添加元素。
  • rem(a: PSet, value):从PSet中删除元素。
  • merge(a: PSet, b: PSet):将两个PSet合并。
import GSet

def PSet():
return (GSet.zero(), GSet.zero()) def zero():
return PSet() def value(a: PSet):
add, rem = a
return add - rem def add(a: PSet, value):
add, rem = a
return add.add(value) def rem(a: PSet, value):
add, rem = a
return rem.add(value) def merge(a: PSet, b: PSet):
add_a, rem_a = a
add_b, rem_b = b
return (GSet.merge(add_a, add_b), GSet.merge(rem_a, rem_b))
import java.util.HashSet;
import java.util.Set; public class PSet {
public static Set<Integer> value(PSetTuple a) {
Set<Integer> add = GSet.value(a.add);
Set<Integer> rem = GSet.value(a.rem);
Set<Integer> res = new HashSet<>(add);
res.removeAll(rem);
return res;
} public static PSetTuple add(PSetTuple a, int value) {
Set<Integer> add = GSet.add(a.add, value);
Set<Integer> rem = a.rem;
return new PSetTuple(add, rem);
} public static PSetTuple rem(PSetTuple a, int value) {
Set<Integer> add = a.add;
Set<Integer> rem = GSet.add(a.rem, value);
return new PSetTuple(add, rem);
} public static PSetTuple merge(PSetTuple a, PSetTuple b) {
Set<Integer> add_a = a.add;
Set<Integer> rem_a = a.rem;
Set<Integer> add_b = b.add;
Set<Integer> rem_b = b.rem;
Set<Integer> add = GSet.merge(add_a, add_b);
Set<Integer> rem = GSet.merge(rem_a, rem_b);
return new PSetTuple(add, rem);
} public static PSetTuple zero() {
return new PSetTuple(GSet.zero(), GSet.zero());
} public static class PSetTuple {
public Set<Integer> add;
public Set<Integer> rem; public PSetTuple(Set<Integer> add, Set<Integer> rem) {
this.add = add;
this.rem = rem;
}
}
} class GSet {
public static Set<Integer> value(Set<Integer> a) {
return a;
} public static Set<Integer> add(Set<Integer> a, int value) {
Set<Integer> res = new HashSet<>(a);
res.add(value);
return res;
} public static Set<Integer> merge(Set<Integer> a, Set<Integer> b) {
Set<Integer> res = new HashSet<>(a);
res.addAll(b);
return res;
} public static Set<Integer> zero() {
return new HashSet<>();
}
}

实现一个CRDT工具库——PSet的更多相关文章

  1. 如何手写一个js工具库?同时发布到npm上

    自从工作以来,写项目的时候经常需要手写一些方法和引入一些js库 JS基础又十分重要,于是就萌生出自己创建一个JS工具库并发布到npm上的想法 于是就创建了一个名为learnjts的项目,在空余时间也写 ...

  2. [js高手之路] 跟GhostWu一起封装一个字符串工具库-架构篇(1)

    所谓字符串工具库就是利用javascript面向对象的知识封装一个常用的字符串处理方法库,首先给这个库起个名字,好吧就叫ghostwu.js. 看下ghostwu.js的整体架构: ; (functi ...

  3. [js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展trim,trimLeft,trimRight方法(2)

    我们接着上一篇的继续,在上一篇我们完成了工具库的架构,本文扩展字符串去空格的方法, 一共有3个 1,trimLeft: 去除字符串左边的空格 2,trimRight: 去除字符串右边的空格 3,tri ...

  4. [js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展字符串位置方法(4)

    本文,我们接着之前的框架继续扩展,这次扩展了一共有5个与字符串位置相关的方法 between( left, right ) 返回两个字符串之间的内容, 如果第二个参数没有传递,返回的是找到的第一个参数 ...

  5. [js高手之路] 跟GhostWu一起封装一个字符串工具库-扩展camelize与dasherize方法(3)

    在此之前,我们已经完成了4个方法: trimLeft, trimRight, trim, capitalize 本文,我们扩展驼峰式与下划线转化这两个对称的方法 camelize: 把空格,下划线,中 ...

  6. 一个PHP高性能、多并发、restful的工具库(基于multi_curl)

    This is high performance curl wrapper written in pure PHP. It's compatible with PHP 5.4+ and HHVM. N ...

  7. Underscore.js工具库

    Underscore 是一个 JavaScript 工具库,它提供了一整套函数式编程的实用功能,但是没有扩展任何 JavaScript 内置对象. 他解决了这个问题:“如果我面对一个空白的 HTML ...

  8. JavaScript工具库

    jPublic 交流QQ群:1017567122 前言 在我们开发项目的时候,无论项目规模大小,在所难免会写一些工具型函数来解决一些问题,随着项目开发和维护的时间越来越长,这些工具型函数会越来越多,同 ...

  9. 手把手教你使用Rollup打包📦并发布自己的工具库🔧

    DevUI是一支兼具设计视角和工程视角的团队,服务于华为云DevCloud平台和华为内部数个中后台系统,服务于设计师和前端工程师. 官方网站:devui.design Ng组件库:ng-devui(欢 ...

  10. 自己的一个LESS工具函数库

    自己大概在一年前开始使用LESS编写样式,现在感觉不用LESS都不会写样式了.现在写静态页面完全离不开LESS与Zen Coding,我可以不用什么IDE,但这两个工具却必须要,当然也强烈推荐看到这篇 ...

随机推荐

  1. Git配置新学

    Git中的AutoCRLF与SafeCRLF换行符问题 https://zhuanlan.zhihu.com/p/380574688 https://xiaozhuanlan.com/topic/40 ...

  2. rabbitMq安装 - docker

    安装rabbitmq 参考网站:https://www.rabbitmq.com/download.html 方式一: 获取rabbit镜像: docker pull rabbitmq:managem ...

  3. 对synchronized的理解和Spring为什么是单例的

    只有真正理解了Java中对象是什么,才能理解这个关键字是什么意思 字面解释 Java Guide中如此解释: synchronized 关键字解决的是多个线程之间访问资源的同步性,synchroniz ...

  4. 通过flask完成web实时播放视频

    def gen(): r = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB, password=PASS_WORD) ...

  5. go语言的特性

    一.golang语言特性 1. 垃圾回收 a.内存自动回收,再也不需要开发人员管理内存  //开发代码中不能存在无引用的变量,不然代码出错 b.开发人员专注业务实现,降低了心智负担 c.只需要new分 ...

  6. python:包含’e’和‘-’的 str 转 float

    from functools import reduce def str2float(s): s = s.split('.') a = s[0] b = s[1] if a[0] == '-': a ...

  7. C#和C++差异化对比

    这里只记录和C++面向对象的区别,也并无比较成分,只做差异化学习使用. 1. 访问修饰符区别:多了一个Internal:成员仅能被同一个项目中的代码访问. 2. 字段的访问:增加了Get,Set访问器 ...

  8. openstack:OpenStack架构详解,

    OpenStack既是一个社区,也是一个项目和一个开源软件,提供开放源码软件,建立公共和私有云,它提供了一个部署云的操作平台或工具集,其宗旨在于:帮助组织运行为虚拟计算或存储服务的云,为公有云.私有云 ...

  9. MySQL 日期相关

    NOW() 返回当前的日期和时间 CURDATE() 返回当前的日期 CURTIME() 返回当前的时间 DATE() 提取日期或日期/时间表达式的日期部分 EXTRACT() 返回日期/时间按的单独 ...

  10. Otto Group Product Classification

    遇到的坑: 做多分类,用CrossEntropyLoss时,训练时候的正确标签的范围应该是[0,n-1],而不是[1,n],不然会报 IndexError: Target is out of boun ...