data-structureserlangets# What are the differences between different ETS table types?

According to the manual there are 4 types of ETS tables:

`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.

Though I don't really understand the differences between them from their descriptions. What are "objects" and "instances of objects" here?

Solution

`set`

It is a regular mapping where each key is unique and refers to a single tuple. Each subsequent write to a single key will always overwrite the existing entry under that 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}]
```

`ordered_set`

Just like `set`

, but keeps the elements ordered by standard comparison operators and solves equality by the `==`

operator. Therefore keys `1`

and `1.0`

are treated as equivalent.

```
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}]
```

`bag`

It is a unique collection of tuples. Entries can share keys as long as they differ somewhere in the values.

```
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`

Same as `bag`

, but not unique. Tuples can duplicate meaning that each subsequent addition of a same element will add new entry into the table

```
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}]
```

- How to turn Dymola package.order warnings into error
- The simplest algorithm for poker hand evaluation
- Debugging Union Find Algorithm Implementation
- Maximum sum of distinct subarrays with k stuck at flag logic
- Is calling stack a LIFO queue correct?
- Javascript - Check if arrays within an array are the same
- What is going wrong in this longest subarray of 1s after deleting one element
- Implementation of a terminal history functionality in a tiny toy project
- What's the best way to recursively traverse a BinaryTree in Java without void methods?
- can anybody explains the code and also recursion tree
- Why Linked List item insertion is slower than Array item insertion in Javasript?
- What are the benefits of using Linkedlists over Arrays in Javascript
- Finding the minimum and maximum height in a AVL tree, given a number of nodes?
- Congested Mountain Trail
- Can an enum be used as a data structure here? Error: "Redefinition of enumerator"
- Is there any graph data structure implemented for C#
- BFS Maximize Minimum Distance from a Monster along a path
- Two limited scope variables vs one global
- Parsing several structures with boost spirit
- Parsing struct into struct using boost spirit
- Return list of Nodes a Tree in Java-Parent can have multiple child Nodes
- valid Parenthese using stack in c
- Why is O(n) better than O( nlog(n) )?
- Find max product using divide and conqure in O(n) time
- Fast Convertion From Adjacency List to Edge List
- Simplest code for array intersection in javascript
- How to conveniently parse a very specific chunk of a plaintext file in Ada?
- How to use collections.deque most effectively (popleft vs. appendleft)
- Computing an md5 hash of a data structure
- Has anyone actually implemented a Fibonacci-Heap efficiently?