gpt4 book ai didi

data-structures - 不同的 ETS 表类型之间有什么区别?

转载 作者:行者123 更新时间:2023-12-04 08:14:36 25 4
gpt4 key购买 nike

根据manual有 4 种类型的 ETS 表:

  • set – The table is a set table: one key, one object, no order among objects. This is the default table type.
  • ordered_set – The table is a ordered_set table: one key, one object, ordered in Erlang term order, which is the order implied by the < and > operators. Tables of this type have a somewhat different behavior in some situations than tables of other types. Most notably, the ordered_set tables regard keys as equal when they compare equal, not only when they match. This means that to an ordered_set table, integer() 1 and float() 1.0 are regarded as equal. This also means that the key used to lookup an element not necessarily matches the key in the returned elements, if float()'s and integer()'s are mixed in keys of a table.
  • bag – The table is a bag table, which can have many objects, but only one instance of each object, per key.
  • duplicate_bag – The table is a duplicate_bag table, which can have many objects, including multiple copies of the same object, per key.

虽然我从他们的描述中并没有真正理解他们之间的区别。这里的“对象”和“对象的实例”是什么?

最佳答案

设置

这是一个规则映射,其中每个键都是唯一的,并且指的是单个元组。对单个键的每次后续写入都将始终覆盖该键下的现有条目。

1> T = ets:new(t, [set]).
2> ets:insert(T, {1, a}).
3> ets:insert(T, {1, b}).
3> ets:insert(T, {1, b}).
4> ets:insert(T, {1.0, c}).
5> ets:lookup(T, 1).
[{1,b}]

有序集

就像 set 一样,但是通过标准比较运算符保持元素的顺序,并通过 == 运算符解决相等问题。因此键 11.0 被视为等价的。

1> T = ets:new(t, [set]).
2> ets:insert(T, {1, a}).
3> ets:insert(T, {1, b}).
3> ets:insert(T, {1, b}).
4> ets:insert(T, {1.0, c}).
5> ets:lookup(T, 1).
[{1.0,c}]

它是元组的唯一集合。只要条目在值的某处不同,它们就可以共享键。

1> T = ets:new(t, [bag]).
2> ets:insert(T, {1, a}).
3> ets:insert(T, {1, b}).
4> ets:insert(T, {1, b}).
4> ets:insert(T, {1.0, c}).
5> ets:lookup(T, 1).
[{1,a},{1,b}]

duplicate_bag

bag相同,但不是唯一的。元组可以重复,这意味着每个后续添加的相同元素都会在表中添加新条目

1> T = ets:new(t, [duplicate_bag]).
2> ets:insert(T, {1, a}).
3> ets:insert(T, {1, b}).
4> ets:insert(T, {1, b}).
4> ets:insert(T, {1.0, c}).
5> ets:lookup(T, 1).
[{1,a},{1,b},{1,b}]

关于data-structures - 不同的 ETS 表类型之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65771406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com