Skip to content

Integer set motivating example #1

@kozross

Description

@kozross

In order to have our logical operations CIP make sense, we need to provide a few use cases as motivating examples. One easy one is the idea of an 'integer set', also known as a bitmap, a bitset or a bitvector. This is a data structure designed which, given a limit n, represents a set of numbers in the range [0, n). We would like to be able to do at least the following operations:

  • Construct the empty set (no elements) or the universe (all elements)
  • Set complement
  • Set union
  • Set intersection
  • Set difference
  • Membership testing
  • Inserting and deleting elements

This can currently only really be done using BuiltinList BuiltinInteger, which, while workable, is extremely inefficient from the point of view of both time and space. With logical operations however, we could achieve this using BuiltinByteString instead: in this representation, the kth bit in the BuiltinByteString represents the membership status of the number k. Thus, we get:

  • Empty set is just replicating the zero byte
  • Universe is just replicating the full byte (0xFF)
  • Set complement is bitwise complement
  • Set union is bitwise OR
  • Set intersection is bitwise AND
  • Symmetric set difference is bitwise XOR, whereas asymmetric difference is a combination of bitwise complement and bitwise OR
  • Membership testing is checking whether the corresponding bit is set
  • Inserting and deleting an element is setting or clearing a corresponding bit respectively

This is more efficient in both time and space, and because this relies exclusively on one primop per operation (or at most a fixed number of them), it can be heavily optimized by the Plutus Core CEK machine.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions