digitalmars.D.learn - BitArray Slicing
- Ezneh (11/11) Dec 21 2016 Hi, in one of my projects I have to get a slice from a BitArray.
- Eugene Wissner (6/18) Dec 21 2016 The problem is BitArray keeps multiple elements in one byte. You
- Ezneh (4/9) Dec 21 2016 I see, so I have to find another way to get only the range I want
- Ilya Yaroshenko (34/46) Dec 21 2016 Mir allows you to define simple alternative to BitArray:
- Ezneh (7/60) Dec 21 2016 Thanks, I'll check that solution to see if it fits my needs.
- Ilya Yaroshenko (6/14) Dec 21 2016 You are the first who is interested TINYMT, feel free to open a
Hi, in one of my projects I have to get a slice from a BitArray.
I am trying to achieve that like this :
void foo(BitArray ba)
{
auto slice = ba[0..3]; // Assuming it has more than 4 elements
}
The problem is that I get an error :
"no operator [] overload for type BitArray".
Is there any other way to get a slice from a BitArray ?
Thanks,
Ezneh.
Dec 21 2016
On Wednesday, 21 December 2016 at 09:08:51 UTC, Ezneh wrote:
Hi, in one of my projects I have to get a slice from a BitArray.
I am trying to achieve that like this :
void foo(BitArray ba)
{
auto slice = ba[0..3]; // Assuming it has more than 4
elements
}
The problem is that I get an error :
"no operator [] overload for type BitArray".
Is there any other way to get a slice from a BitArray ?
Thanks,
Ezneh.
The problem is BitArray keeps multiple elements in one byte. You
can't return just three bits but in the best case one byte with 8
elements.
What could be done some internal range could be returned that
gives access to the bits. But it isn't implemented.
Dec 21 2016
On Wednesday, 21 December 2016 at 09:14:04 UTC, Eugene Wissner wrote:The problem is BitArray keeps multiple elements in one byte. You can't return just three bits but in the best case one byte with 8 elements. What could be done some internal range could be returned that gives access to the bits. But it isn't implemented.I see, so I have to find another way to get only the range I want from a BitArray.
Dec 21 2016
On Wednesday, 21 December 2016 at 09:08:51 UTC, Ezneh wrote:
Hi, in one of my projects I have to get a slice from a BitArray.
I am trying to achieve that like this :
void foo(BitArray ba)
{
auto slice = ba[0..3]; // Assuming it has more than 4
elements
}
The problem is that I get an error :
"no operator [] overload for type BitArray".
Is there any other way to get a slice from a BitArray ?
Thanks,
Ezneh.
Mir allows you to define simple alternative to BitArray:
https://github.com/libmir/mir
------
struct BitMap
{
size_t* ptr;
import core.bitop;
bool opIndex(size_t index) const
{
return bt(ptr, index) != 0;
}
void opIndexAssign(bool val, size_t index)
{
if(val)
bts(ptr, index);
else
btr(ptr, index);
}
}
import mir.ndslice;
void main()
{
auto arr = new size_t[3];
auto sl = BitMap(arr.ptr).sliced(size_t.sizeof * 8 *
arr.length);
sl[4] = true;
sl[100] = true;
sl.popFrontN(3);
assert(sl[1]);
assert(sl[97]);
auto sl2 = sl[1...3]; // slicing
}
------
Dec 21 2016
On Wednesday, 21 December 2016 at 11:49:06 UTC, Ilya Yaroshenko wrote:On Wednesday, 21 December 2016 at 09:08:51 UTC, Ezneh wrote:Thanks, I'll check that solution to see if it fits my needs. As an off-topic question, is there any plan in Mir to implement the Tiny Mersenne Twister[1] algorithm (or a wrapper for it) ? [1] http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/TINYMT/index.htmlHi, in one of my projects I have to get a slice from a BitArray. I am trying to achieve that like this : void foo(BitArray ba) { auto slice = ba[0..3]; // Assuming it has more than 4 elements } The problem is that I get an error : "no operator [] overload for type BitArray". Is there any other way to get a slice from a BitArray ? Thanks, Ezneh.Mir allows you to define simple alternative to BitArray: https://github.com/libmir/mir ------ struct BitMap { size_t* ptr; import core.bitop; bool opIndex(size_t index) const { return bt(ptr, index) != 0; } void opIndexAssign(bool val, size_t index) { if(val) bts(ptr, index); else btr(ptr, index); } } import mir.ndslice; void main() { auto arr = new size_t[3]; auto sl = BitMap(arr.ptr).sliced(size_t.sizeof * 8 * arr.length); sl[4] = true; sl[100] = true; sl.popFrontN(3); assert(sl[1]); assert(sl[97]); auto sl2 = sl[1...3]; // slicing } ------
Dec 21 2016
On Wednesday, 21 December 2016 at 12:00:57 UTC, Ezneh wrote:On Wednesday, 21 December 2016 at 11:49:06 UTC, Ilya Yaroshenko wrote:You are the first who is interested TINYMT, feel free to open a PR in Mir Random https://github.com/libmir/mir-random TINYMT should not be big, only Engine itself is required (without floating point stuff and arrays generators).[...]Thanks, I'll check that solution to see if it fits my needs. As an off-topic question, is there any plan in Mir to implement the Tiny Mersenne Twister[1] algorithm (or a wrapper for it) ? [1] http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/TINYMT/index.html
Dec 21 2016









Ezneh <petitv.isat gmail.com> 