August 06, 2015 std.stream.MemoryStream deprecated, range is the alternative? | ||||
|---|---|---|---|---|
| ||||
class Test {
MemoryStream m_stream;
this(MemoryStream stream) {
m_stream = stream;
}
void write(byte val) {
m_stream.write(val);
}
byte read() {
byte val;
m_stream.read(val);
return val;
}
}
void main() {
byte[] read = [0, 2, 4, 6, 8, 10, 12];
auto t = new Test(read);
byte val = t.read();
t.write(1);
byte val1 = t.m_stream.data;
writeln(val, val1);
}
since memorystream is deprecated how do i do something like this with Input and Output ranges? How can i fill up an array with ranges like you can do with streams?
Thanks.
| ||||
August 07, 2015 Re: std.stream.MemoryStream deprecated, range is the alternative? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to chris | On Thursday, 6 August 2015 at 17:01:32 UTC, chris wrote:
> since memorystream is deprecated how do i do something like this with Input and Output ranges? How can i fill up an array with ranges like you can do with streams?
> Thanks.
The InputRange primitives already exist for arrays, they are located in std.array, as well as the functions to insert elements. To achieve more advanced mutations use std.algorithm.
---
import std.stdio;
import std.array;
import std.algorithm;
byte B(T)(T t){return cast(byte) t;}
struct FillerDemo
{
private byte cnt;
byte front(){return cnt;}
void popFront(){++cnt;}
@property bool empty(){return cnt == 8;}
}
void main(string[] args)
{
auto rng = [0.B, 2.B, 4.B, 6.B, 8.B, 10.B, 12.B];
// reads then advances, destructively
byte val = rng.front;
writeln(val);
rng.popFront;
// fills with an array
insertInPlace(rng, 0, [-4.B, -2.B, 0.B]);
writeln(rng);
rng = rng.init;
// fills with a compatible range
insertInPlace(rng, 0, *new FillerDemo);
writeln(rng);
// std.algorithm
reverse(rng);
}
---
Note, if you don't know yet, that ranges are consumed. The front is lost each time popFront() is called.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply