Thread overview
BitArray Slicing
Dec 21, 2016
Ezneh
Dec 21, 2016
Eugene Wissner
Dec 21, 2016
Ezneh
Dec 21, 2016
Ilya Yaroshenko
Dec 21, 2016
Ezneh
Dec 21, 2016
Ilya Yaroshenko
December 21, 2016
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.
December 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.
December 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.
December 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
}
------
December 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:
>> 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
> }
> ------

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
December 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:
>> [...]
>
> 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

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).