[swift-evolution] Contiguous storage byte arrays
Ted F.A. van Gaalen
tedvgiosdev at gmail.com
Wed Feb 17 08:53:28 CST 2016
Hello Johan, thanks for your reply.
It would be nice if there was some kind of attribute like
var bytes: [UInt8] contiguous = ... // to declare a contiguous array.
Ok then, I'd suggest creating a new Swift type with an internal fixed length
and contiguous byte array inside. Perhaps to be implemented in a Swift library.
This swift class example here is just a Model
because to a programmer [Swift any type] arrays are undefined whether to be
be contiguous or not. No error handling is implemented in this examplel)
final class ByteArray // Note: Just a model for a lower level implementation!
{
private var bytebuffer = [UInt8]() // Not! Must be an internal contiguous byte array.
init(contents: [UInt8]) // Constructor.
{
for byte in contents // load bytes into contiguous!! buffer
{
bytebuffer.append(byte)
}
}
func setByte(index index: Int, byte: UInt8) // set a byte in the buffer
{
if index.outsideRange(0..<bytebuffer.count) // should be valid index
{
// Aarrggh! throw: subscripting error
}
bytebuffer[index] = byte
}
// other functions could be added to clear, shift etc.
func address() -> Any
{
return bytebuffer // use as someExternalFunc(bytes.address,…)
}
func length() -> Int
{
return bytebuffer.count
}
}
// usage example:
let bytes = ByteArray(contents: someUInt8array )
// or -other constructor- make a byte array of 512 zero bytes:
let bytes = ByteArray(length: 512, fillWith: 0)
bytes.setByte(index: 0, byte: midiCommandByte)
bytes.setByte(index: 1, byte: midiStatByte)
// define subscriptors? so that we could just write: bytes[idx] = value
var offset = 2 // load databytes
for b in midiData
{
bytes.setByte(index: offset, byte: b)
offset++
}
// pass the address of the contiguous byte buffer within (this instance of) ByteArray to some external function:
someExternalFunc(&bytes.address, . . . )
Regards
Ted
> On 14.02.2016, at 20:26, Johan Jensen <jj at johanjensen.dk> wrote:
>
> 4 Tuples: please take a look at this code:
>
> static var z: UInt8 = 0 // initalize tuple with 256 UInt8 values, bytes:
>
> // Silly: why not an array instead of this.. a tuple is needed.. length must be exact 256..
> // know of no other way to create a tuple with 256 elements...
> var midiDataTuple = (z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
> z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
> z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
> z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
> z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
> z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z)
>
>
> func midiSend(status: Int, val1: Int, val2 :Int)
> {
> var midipacket = MIDIPacket()
>
> midipacket.timeStamp = 0
> midipacket.length = 3
> midipacket.data = midiDataTuple //<-=<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>
> midipacket.data.0 = UInt8(status)
> midipacket.data.1 = UInt8(val1 )
> midipacket.data.2 = UInt8(val2 )
>
> var midipacketlist = MIDIPacketList(numPackets: 1, packet: midipacket)
>
> MIDIReceived(midiSource, &midipacketlist)
> }
>
> I can't treat tuples as an array, which in this case would be handy to initialize all the tuple elements.
>
> IfTrue: Why are tuples the only data type to use for unmanaged byte arrays?
>
> There's currently some work on a proposal for Contiguous Variables (A.K.A. Fixed Sized Array Type) <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160125/007984.html> which should help with the creation of tuples. (It can be read in a better fashion on gmane <http://thread.gmane.org/gmane.comp.lang.swift.evolution/4809>)
>
> — Johan
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160217/eff707cb/attachment.html>
More information about the swift-evolution
mailing list