Skip to content

BitArray/BitSet need a way to export the underlying bitmaps in some sensible format #489

@lorentey

Description

@lorentey

We need a good way to extract the underlying bitmaps out of a BitArray/BitSet value in a reasonably efficient (and reasonably elegant) way. We also need a way to then convert that data back into a BitArray/BitSet value.

BitArray and BitSet internally store their contents in what is effectively an array of UInt64, making it subject to endianness issues. This makes it unlikely we'd want them to expose their storage directly -- at least, not without overhauling these types to use a simpler representation. (Which is not out of the question, either.)

If we want to preserve the current representation, one possible move is to offer conversions to/from raw buffer pointers (or, preferably, RawSpan/OutputRawSpan).

extension BitArray { // And BitSet
  init(bitPattern: UnsafeRawBufferPointer)
  func exportBitPattern(initializing target: UnsafeMutableRawBufferPointer)

  init(bitPattern: RawSpan)
  func exportBitPattern(to target: inout OutputRawSpan)
}

(Names are tricky, as usual.)

These new APIs need to make a decision if they use a little- or big-endian representation for the byte buffers they operate on; alternatively, this can be a user-configurable input argument.

The most elegant way of doing this would be to change the underlying representation of these types, and simply add a var bitPattern: RawSpan property that directly exposes their storage with O(1) complexity. (That too would need to make a decision on endianness; and in this case that cannot be user-configurable.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions