Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #83921 > unrolled thread

Anyone ever used vector<bool> ?

Started byMuttley@dastardlyhq.com
First post2022-05-03 15:16 +0000
Last post2022-06-01 14:40 +0200
Articles 15 on this page of 95 — 21 participants

Back to article view | Back to comp.lang.c++


Contents

  Anyone ever used vector<bool> ? Muttley@dastardlyhq.com - 2022-05-03 15:16 +0000
    Re: Anyone ever used vector<bool> ? Ben <ben.usenet@bsb.me.uk> - 2022-05-03 16:39 +0100
      Re: Anyone ever used vector<bool> ? Muttley@dastardlyhq.com - 2022-05-03 16:12 +0000
        Re: Anyone ever used vector<bool> ? Bo Persson <bo@bo-persson.se> - 2022-05-03 20:25 +0200
          Re: Anyone ever used vector<bool> ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-05-03 12:00 -0700
          Re: Anyone ever used vector<bool> ? Juha Nieminen <nospam@thanks.invalid> - 2022-05-04 10:50 +0000
    Re: Anyone ever used vector<bool> ? Muttley@dastardlyhq.com - 2022-05-03 16:14 +0000
    Re: Anyone ever used vector<bool> ? Jack Lemmon <invalid@invalid.net> - 2022-05-03 17:33 +0100
      Re: Anyone ever used vector<bool> ? Manfred <noname@add.invalid> - 2022-05-03 19:13 +0200
        Re: Anyone ever used vector<bool> ? Muttley@dastardlyhq.com - 2022-05-04 09:13 +0000
      Re: Anyone ever used vector<bool> ? Juha Nieminen <nospam@thanks.invalid> - 2022-05-04 10:45 +0000
        Re: Anyone ever used vector<bool> ? Paavo Helde <eesnimi@osa.pri.ee> - 2022-05-04 14:09 +0300
          Re: Anyone ever used vector<bool> ? Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2022-05-04 14:39 -0700
            Re: Anyone ever used vector<bool> ? red floyd <no.spam.here@its.invalid> - 2022-05-04 15:17 -0700
              Re: Anyone ever used vector<bool> ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-05-05 01:57 -0400
            Re: Anyone ever used vector<bool> ? Paavo Helde <eesnimi@osa.pri.ee> - 2022-05-05 09:12 +0300
          Re: Anyone ever used vector<bool> ? Juha Nieminen <nospam@thanks.invalid> - 2022-05-05 06:17 +0000
            Re: Anyone ever used vector<bool> ? Ben <ben.usenet@bsb.me.uk> - 2022-05-05 10:49 +0100
              Re: Anyone ever used vector<bool> ? Öö Tiib <ootiib@hot.ee> - 2022-05-05 04:29 -0700
                Re: Anyone ever used vector<bool> ? Ben <ben.usenet@bsb.me.uk> - 2022-05-05 12:56 +0100
                  Re: Anyone ever used vector<bool> ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-05-05 21:37 +0200
                    Re: Anyone ever used vector<bool> ? Ben <ben.usenet@bsb.me.uk> - 2022-05-05 21:13 +0100
                      Re: Anyone ever used vector<bool> ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-05-06 10:39 +0200
                        Re: Anyone ever used vector<bool> ? Ben <ben.usenet@bsb.me.uk> - 2022-05-06 11:54 +0100
                          Re: Anyone ever used vector<bool> ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-05-06 14:29 +0200
                            Re: Anyone ever used vector<bool> ? Ben <ben.usenet@bsb.me.uk> - 2022-05-06 17:05 +0100
                            Re: Anyone ever used vector<bool> ? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-06 10:22 -0700
                              Re: Anyone ever used vector<bool> ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-05-06 22:18 +0200
                                Re: Anyone ever used vector<bool> ? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-05-06 13:51 -0700
                                Re: Anyone ever used vector<bool> ? Muttley@dastardlyhq.com - 2022-05-07 08:41 +0000
                                  Re: Anyone ever used vector<bool> ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-05-07 12:33 +0200
                                    Re: Anyone ever used vector<bool> ? Muttley@dastardlyhq.com - 2022-05-07 16:12 +0000
                                      Re: Anyone ever used vector<bool> ? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-07 09:37 -0700
                                        Re: Anyone ever used vector<bool> ? Muttley@dastardlyhq.com - 2022-05-09 09:02 +0000
                                          Re: Anyone ever used vector<bool> ? Bo Persson <bo@bo-persson.se> - 2022-05-09 12:14 +0200
                                            Re: Anyone ever used vector<bool> ? Muttley@dastardlyhq.com - 2022-05-09 15:36 +0000
                                              Re: Anyone ever used vector<bool> ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-05-09 11:56 -0400
                                                Re: Anyone ever used vector<bool> ? Muttley@dastardlyhq.com - 2022-05-09 16:18 +0000
                                                  Re: Anyone ever used vector<bool> ? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-09 09:38 -0700
                                                    Re: Anyone ever used vector<bool> ? Muttley@dastardlyhq.com - 2022-05-10 15:55 +0000
                                                      Re: Anyone ever used vector<bool> ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-05-10 13:28 -0400
                                                  Re: Anyone ever used vector<bool> ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-05-10 10:16 -0400
                                                    Re: Anyone ever used vector<bool> ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-05-10 18:40 +0200
                                                    Re: Anyone ever used vector<bool> ? Manfred <noname@add.invalid> - 2022-05-10 19:50 +0200
                                                      Re: Anyone ever used vector<bool> ? Öö Tiib <ootiib@hot.ee> - 2022-05-12 05:44 -0700
                                                        Re: Anyone ever used vector<bool> ? Manfred <noname@add.invalid> - 2022-05-12 17:24 +0200
                                                          Re: Anyone ever used vector<bool> ? Öö Tiib <ootiib@hot.ee> - 2022-05-12 20:11 -0700
                                                        Re: Anyone ever used vector<bool> ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-05-12 12:53 -0400
                                                        Re: Anyone ever used vector<bool> ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-05-12 22:53 -0400
                                                          Re: Anyone ever used vector<bool> ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-05-13 18:34 +0200
                                                            Re: Anyone ever used vector<bool> ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-05-13 23:46 -0400
                                                              Re: Anyone ever used vector<bool> ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-05-14 11:43 +0200
                                                                Re: Anyone ever used vector<bool> ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-05-15 07:48 -0700
                                                                  Re: Anyone ever used vector<bool> ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-05-16 03:30 +0200
                                                                    Re: Anyone ever used vector<bool> ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-05-15 22:57 -0400
                                                                      Re: Anyone ever used vector<bool> ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-05-17 05:30 -0700
                                                                    Re: Anyone ever used vector<bool> ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-05-17 05:19 -0700
                                                              Re: Anyone ever used vector<bool> ? Juha Nieminen <nospam@thanks.invalid> - 2022-05-16 05:46 +0000
                                                                Re: Anyone ever used vector<bool> ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-05-17 05:37 -0700
                                                      Re: Anyone ever used vector<bool> ? wij <wyniijj2@gmail.com> - 2022-05-12 08:05 -0700
                                              Re: Anyone ever used vector<bool> ? scott@slp53.sl.home (Scott Lurndal) - 2022-05-09 17:20 +0000
                                                Re: Anyone ever used vector<bool> ? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-05-09 12:05 -0700
                                              Re: Anyone ever used vector<bool> ? Bo Persson <bo@bo-persson.se> - 2022-05-09 21:57 +0200
                                                Re: Anyone ever used vector<bool> ? wij <wyniijj2@gmail.com> - 2022-05-09 13:56 -0700
                                          Re: Anyone ever used vector<bool> ? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-09 08:33 -0700
                                          Re: Anyone ever used vector<bool> ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-05-09 11:56 -0400
                                            Re: Anyone ever used vector<bool> ? Manfred <noname@add.invalid> - 2022-05-09 18:13 +0200
                                              Re: Anyone ever used vector<bool> ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-05-10 10:14 -0400
                                                Re: Anyone ever used vector<bool> ? scott@slp53.sl.home (Scott Lurndal) - 2022-05-10 14:31 +0000
                                      Re: Anyone ever used vector<bool> ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-05-07 18:38 +0200
                                      Re: Anyone ever used vector<bool> ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-05-07 18:28 -0400
                                        Re: Anyone ever used vector<bool> ? Manfred <noname@add.invalid> - 2022-05-08 18:37 +0200
                                          Re: Anyone ever used vector<bool> ? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-05-08 11:50 -0700
                                          Re: Anyone ever used vector<bool> ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-05-08 18:18 -0400
                                Re: Anyone ever used vector<bool> ? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-07 09:45 -0700
                                  Re: Anyone ever used vector<bool> ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-05-07 19:12 +0200
                                    Re: Anyone ever used vector<bool> ? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-07 20:36 -0700
                                      Re: Anyone ever used vector<bool> ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-05-08 08:26 +0200
                          Re: Anyone ever used vector<bool> ? scott@slp53.sl.home (Scott Lurndal) - 2022-05-06 13:55 +0000
                  Re: Anyone ever used vector<bool> ? Juha Nieminen <nospam@thanks.invalid> - 2022-05-06 06:35 +0000
                    Re: Anyone ever used vector<bool> ? Ben <ben.usenet@bsb.me.uk> - 2022-05-06 11:29 +0100
                      Re: Anyone ever used vector<bool> ? Juha Nieminen <nospam@thanks.invalid> - 2022-05-09 04:58 +0000
                        Re: Anyone ever used vector<bool> ? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-05-08 22:30 -0700
    Re: Anyone ever used vector<bool> ? Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-05-03 14:41 -0700
    Re: Anyone ever used vector<bool> ? Andrea Venturoli <ml.diespammer@netfence.it> - 2022-05-04 08:20 +0200
    Re: Anyone ever used vector<bool> ? wij wij <wyniijj2@gmail.com> - 2022-05-04 03:21 -0700
      Re: Anyone ever used vector<bool> ? Muttley@dastardlyhq.com - 2022-05-04 10:52 +0000
    Re: Anyone ever used vector<bool> ? Juha Nieminen <nospam@thanks.invalid> - 2022-05-04 10:47 +0000
      Re: Anyone ever used vector<bool> ? Muttley@dastardlyhq.com - 2022-05-04 10:53 +0000
    Re: Anyone ever used vector<bool> ? Bonita Montero <Bonita.Montero@gmail.com> - 2022-06-01 05:46 +0200
      Re: Anyone ever used vector<bool> ? Bonita Montero <Bonita.Montero@gmail.com> - 2022-06-01 07:31 +0200
      Re: Anyone ever used vector<bool> ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-06-01 14:20 +0200
        Re: Anyone ever used vector<bool> ? Bonita Montero <Bonita.Montero@gmail.com> - 2022-06-01 14:23 +0200
          Re: Anyone ever used vector<bool> ? "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-06-01 14:26 +0200
            Re: Anyone ever used vector<bool> ? Bonita Montero <Bonita.Montero@gmail.com> - 2022-06-01 14:40 +0200

Page 5 of 5 — ← Prev page 1 2 3 4 [5]


#83961

FromBen <ben.usenet@bsb.me.uk>
Date2022-05-06 11:29 +0100
Message-ID<874k239cpc.fsf@bsb.me.uk>
In reply to#83959
Juha Nieminen <nospam@thanks.invalid> writes:

> Ben <ben.usenet@bsb.me.uk> wrote:
>>>> > That being said, std::list couldn't be used for this, especially now 
>>>> > that std::list::splice() is not an O(1) operation (which would pretty 
>>>> > much defeat the purpose).
>>>>
>>>> It's O(1) in C++11, 14 and 20 (at least in the public drafts).
>>>
>>> It has 6 overloads. Most are O(1) but one case where we transfer iterator
>>> range from other list to this list is linear in length of said range.
>> 
>> Obviously, but that's surely not what the OP was talking about.  That
>> one could never have been, and will never be O(1).
>
> That's exactly the operation I was talking about: Splicing a segment of
> a list into another list. Which is (quite trivially) an O(1) operation
> when you have the necessary iterators (which in the algorithm I mentioned
> you have, at that point).

It's not /trivially/ constant time since, as has been pointed out, the
spliced sub-list list needs to be scanned for length.  I don't see how
it could be constant time unless std::list::size's constraint were to be
relaxed.

> It used to be O(1) in C++98 std::list,

Not in the draft I have.  And it's been the same in C++ 03, 11, 14 and
20.

> but now they made it an O(n)
> operation, destroying one of the few (if not the only) strengths of
> std::list over any other data container.

I don't think they changed it.  And I don't see how it could be anything
but linear if the length has to be maintained.

I agree that there is a trade-off here, and that C++'s choice did not
suit your algorithm, but they didn't change anything, and the common
cases of splicing into a list always have been O(1).

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#83993

FromJuha Nieminen <nospam@thanks.invalid>
Date2022-05-09 04:58 +0000
Message-ID<t5a70v$jos$1@gioia.aioe.org>
In reply to#83961
Ben <ben.usenet@bsb.me.uk> wrote:
>> That's exactly the operation I was talking about: Splicing a segment of
>> a list into another list. Which is (quite trivially) an O(1) operation
>> when you have the necessary iterators (which in the algorithm I mentioned
>> you have, at that point).
> 
> It's not /trivially/ constant time since, as has been pointed out, the
> spliced sub-list list needs to be scanned for length.  I don't see how
> it could be constant time unless std::list::size's constraint were to be
> relaxed.

You don't need to know the length of the lists, or the list segments,
for splicing (nor does the algorithm which I mentioned).

std::list::size() was an O(n) operation in C++98, which allowed all
splicing operations to be O(1).

Then they went and destroyed the most useful feature unique to doubly
linked lists for no reason.

>> It used to be O(1) in C++98 std::list,
> 
> Not in the draft I have.  And it's been the same in C++ 03, 11, 14 and
> 20.

No, I'm pretty certain that all splicing operations were O(1), and
std::list::size() was O(n) in C++98. They demanded an O(1) std::list::size()
only in later standards.

Why are we even arguing about this? Splicing (any variant) can *trivially*
be done in constant-time for doubly-linked lists (when you have the
iterators pointing to the desired positions). This is an actually useful
feature of doubly-linked lists in some algorithms. Nobody is talking about
the speed of std::list::size(). Why are you arguing this?

>> but now they made it an O(n)
>> operation, destroying one of the few (if not the only) strengths of
>> std::list over any other data container.
> 
> I don't think they changed it.  And I don't see how it could be anything
> but linear if the length has to be maintained.

Yes, they did thcange it. And you don't need to know the length of a
linked list (in constant time) in order to handle one.

> I agree that there is a trade-off here, and that C++'s choice did not
> suit your algorithm, but they didn't change anything

They absolutely did. The requirement for an O(1) std::list::size() was
not in C++98.

[toc] | [prev] | [next] | [standalone]


#83995

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2022-05-08 22:30 -0700
Message-ID<t5a8tc$h1u$1@dont-email.me>
In reply to#83993
On 5/8/2022 9:58 PM, Juha Nieminen wrote:
> Ben <ben.usenet@bsb.me.uk> wrote:
>>> That's exactly the operation I was talking about: Splicing a segment of
>>> a list into another list. Which is (quite trivially) an O(1) operation
>>> when you have the necessary iterators (which in the algorithm I mentioned
>>> you have, at that point).
>>
>> It's not /trivially/ constant time since, as has been pointed out, the
>> spliced sub-list list needs to be scanned for length.  I don't see how
>> it could be constant time unless std::list::size's constraint were to be
>> relaxed.
> 
> You don't need to know the length of the lists, or the list segments,
> for splicing (nor does the algorithm which I mentioned).
> 
> std::list::size() was an O(n) operation in C++98, which allowed all
> splicing operations to be O(1).

No. Neither.

In C++98 it was _suggested_ that `std::list<>::size()` should be an O(1) 
operation, but not required ("should" , not "shall" in "Note A" in 
23.1/5). The choice was ultimately left to the implementation. The 
implementation was free to choose between O(1) `size()` with O(n) 
`splice()` or the other way around.

C++11 explicitly demanded O(1) `size()`.

> Then they went and destroyed the most useful feature unique to doubly
> linked lists for no reason.
> 
>>> It used to be O(1) in C++98 std::list,
>>
>> Not in the draft I have.  And it's been the same in C++ 03, 11, 14 and
>> 20.
> 
> No, I'm pretty certain that all splicing operations were O(1), and
> std::list::size() was O(n) in C++98. They demanded an O(1) std::list::size()
> only in later standards.

See above.

It was GCC's implementation of C++ standard library that not only 
insisted on O(1) `std::list<>::splice()` in C++98, but actively refused 
to adopt the C++11 requirements after its release due to "problems with 
binary compatibility".

BTW, what is the current state of affairs in GCC?

-- 
Best regards,
Andrey

[toc] | [prev] | [next] | [standalone]


#83932

FromMalcolm McLean <malcolm.arthur.mclean@gmail.com>
Date2022-05-03 14:41 -0700
Message-ID<55fc7477-e5d8-496b-b1c0-e96c2d45b106n@googlegroups.com>
In reply to#83921
On Tuesday, 3 May 2022 at 16:16:42 UTC+1, Mut...@dastardlyhq.com wrote:
> I need to store ~70K images in memory as B&W bitmaps so any chance of reducing 
> memory usage is a bonus. I've never used vector<bool> because of all the 
> warnings that it doesn't play nice with other parts of the STL. Anyone know 
> any different? 
> 
> Unfortunately I need something that be dynamically sized or I'd use std::bitset 
> but that has to be sized at compile time which is no use.
>
I used one today. I have two lists of open paths, and I want to join paths which abut
each other. With special rules when you have two or more choices about which path 
to join to which.
So I used a matrix of bools set to true for joinable ends.
There was no need to use a reference or an iterator. The point of a matrix is that the
elements are addressed by index. Since the number of paths is always going to be 
quite small, packing into bits is overkill, but use of "bool" documents that the value
must be true / false. 

[toc] | [prev] | [next] | [standalone]


#83933

FromAndrea Venturoli <ml.diespammer@netfence.it>
Date2022-05-04 08:20 +0200
Message-ID<t4t5vs$1pc0$1@gioia.aioe.org>
In reply to#83921
On 5/3/22 17:16, Muttley@dastardlyhq.com wrote:

Yes I did and it served me well.

Just don't expect it can play nicely wherever a "real" vector class 
would, but if it's enough for your use case, go ahead.

[toc] | [prev] | [next] | [standalone]


#83935

Fromwij wij <wyniijj2@gmail.com>
Date2022-05-04 03:21 -0700
Message-ID<a99f9710-2867-4ac0-87ef-0ea48ed24d02n@googlegroups.com>
In reply to#83921
On Tuesday, 3 May 2022 at 23:16:42 UTC+8, Mut...@dastardlyhq.com wrote:
> I need to store ~70K images in memory as B&W bitmaps so any chance of reducing 
> memory usage is a bonus. I've never used vector<bool> because of all the 
> warnings that it doesn't play nice with other parts of the STL. Anyone know 
> any different? 
> 
> Unfortunately I need something that be dynamically sized or I'd use std::bitset 
> but that has to be sized at compile time which is no use.

Nearly all in C++ standard library are not necessary for average programmers
(the first C++ years may partly need), they are small utilities (you learn real 
'programming' skill or those deep engineering/artificial concepts/rules?).
Anyone chooses C++ largely eventually has to understand what is inside and 
code the utility himself.

70k(bytes) is quite small of image data.
I would probably use VLInt (Variable Length Integer) class of my library,
because VLInt contains a member set_bit(size_t,bool) suitable for simple B&W
bitmap's basic r/w.
VLInt is 'dynamic-sized', memory usage is approximately MSB/8 (less than 70k/8)

--------- Usage example of VLInt for printing prime numbers
#include <Wy.stdio.h>
#include <CSCall/VLInt.h>

using namespace Wy;

typedef VLInt<unsigned int> VLType;

// [Syn] Print prime numbers less or equal to n
//
// Algo: sieve
//
void print_prime(size_t n)
{
 Errno r;
 VLType barr;  // bit=0 =prime

 for(size_t i=2; i<=n; ++i) {
   if(barr.bit(i)) {
     continue;
   }
   // Cond: i is a prime number
   for(size_t j=i+i; j<=n; j+=i) {
     if((r=barr.set_bit(j,true))!=Ok) {
       WY_THROW(r);
     }
   }
 }

 // print prime number (from 2)
 size_t pcnt=0;
 for(size_t i=2; i<=n; ++i) {
   if(barr.bit(i)==false) {
     cout << wrd(i) << ' ';
     ++pcnt;
   }
 }
 cout << WY_ENDL;
 cout << "pi(" << n << ")= " << pcnt << WY_ENDL;
};

int main()
try {
 print_prime(1000000);
 cout << "Ok" WY_ENDL;
 return 0;
}
catch(const Errno& e) {
 cerr << wrd(e) << WY_ENDL;
 return -1;
}
catch(const Errno& e) {
 cerr << wrd(e) << WY_ENDL;
}
catch(const Errno& e) {
 cerr << wrd(e) << WY_ENDL;
 return -1;
}
catch(...) {
 cerr << "unknown stack unwind" << WY_ENDL;
 throw;
};

[toc] | [prev] | [next] | [standalone]


#83939

FromMuttley@dastardlyhq.com
Date2022-05-04 10:52 +0000
Message-ID<t4tlt8$pif$1@gioia.aioe.org>
In reply to#83935
On Wed, 4 May 2022 03:21:03 -0700 (PDT)
wij wij <wyniijj2@gmail.com> wrote:
>On Tuesday, 3 May 2022 at 23:16:42 UTC+8, Mut...@dastardlyhq.com wrote:
>> I need to store ~70K images in memory as B&W bitmaps so any chance of
>reducing 
>> memory usage is a bonus. I've never used vector<bool> because of all the 
>> warnings that it doesn't play nice with other parts of the STL. Anyone know 
>> any different? 
>> 
>> Unfortunately I need something that be dynamically sized or I'd use
>std::bitset 
>> but that has to be sized at compile time which is no use.
>
>Nearly all in C++ standard library are not necessary for average programmers
>(the first C++ years may partly need), they are small utilities (you learn
>real 
>'programming' skill or those deep engineering/artificial concepts/rules?).
>Anyone chooses C++ largely eventually has to understand what is inside and 
>code the utility himself.

I'm quite capable of writing a bitmap class but given I'm writing an image
recognition algo I have enough on my plate without having to write basic
data storage classes.

>70k(bytes) is quite small of image data.
>I would probably use VLInt (Variable Length Integer) class of my library,
>because VLInt contains a member set_bit(size_t,bool) suitable for simple B&W
>bitmap's basic r/w.

Very nice.

[toc] | [prev] | [next] | [standalone]


#83937

FromJuha Nieminen <nospam@thanks.invalid>
Date2022-05-04 10:47 +0000
Message-ID<t4tljs$eui$2@gioia.aioe.org>
In reply to#83921
Muttley@dastardlyhq.com wrote:
> I need to store ~70K images in memory as B&W bitmaps so any chance of reducing
> memory usage is a bonus.

Using std::vector<bool> is fine.

If you can use Boost, you may consider using boost::dynamic_bitset.
It has many utilities that std::vector<bool> doesn't have (and is ostensibly
more efficient).

[toc] | [prev] | [next] | [standalone]


#83940

FromMuttley@dastardlyhq.com
Date2022-05-04 10:53 +0000
Message-ID<t4tlv9$qec$1@gioia.aioe.org>
In reply to#83937
On Wed, 4 May 2022 10:47:26 -0000 (UTC)
Juha Nieminen <nospam@thanks.invalid> wrote:
>Muttley@dastardlyhq.com wrote:
>> I need to store ~70K images in memory as B&W bitmaps so any chance of
>reducing
>> memory usage is a bonus.
>
>Using std::vector<bool> is fine.
>
>If you can use Boost, you may consider using boost::dynamic_bitset.
>It has many utilities that std::vector<bool> doesn't have (and is ostensibly
>more efficient).

I rather gave up on boost. C++ since 2011 does pretty much does everything I 
need but it perhaps not in this case.

[toc] | [prev] | [next] | [standalone]


#84404

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-06-01 05:46 +0200
Message-ID<t76nei$o3l$1@dont-email.me>
In reply to#83921
I use my own class

template<typename RandIt>
concept bool_cursor_iterator = bit_cursor_iterator<RandIt>;

template<bool_cursor_iterator BcIt>
struct bool_cursor
{
	using value_type = bool;
	using difference_type = std::ptrdiff_t;
	struct reference;
	bool_cursor() = delete;
	constexpr bool_cursor( BcIt begin, std::ptrdiff_t offset = 0 ) noexcept;
	constexpr bool_cursor( bool_cursor const &other ) noexcept;
	constexpr bool_cursor &operator =( bool_cursor const &other ) noexcept;
	constexpr bool operator ==( bool_cursor const &other ) const noexcept = 
default;
	constexpr bool operator !=( bool_cursor const &other ) const noexcept = 
default;
	constexpr bool_cursor operator +( difference_type offset ) const noexcept;
	constexpr bool_cursor operator -( difference_type offset ) const noexcept;
	constexpr bool_cursor &operator ++() noexcept;
	constexpr bool_cursor &operator --() noexcept;
	constexpr bool_cursor operator ++( int ) noexcept;
	constexpr bool_cursor operator --( int ) noexcept;
	constexpr difference_type operator -( bool_cursor const &other ) const 
noexcept;
	constexpr bool operator <( bool_cursor const &other ) const noexcept;
	constexpr bool operator <=( bool_cursor const &other ) const noexcept;
	constexpr bool operator >( bool_cursor const &other ) const noexcept;
	constexpr bool operator >=( bool_cursor const &other ) const noexcept;
	constexpr bool_cursor &operator +=( difference_type offset ) noexcept;
	constexpr bool_cursor &operator -=( difference_type offset ) noexcept;
	constexpr reference operator *() const noexcept;
	constexpr reference operator []( difference_type offset ) const noexcept;
	struct reference
	{
		constexpr operator bool() const noexcept;
		constexpr bool operator =( bool f ) const noexcept;
	private:
		friend struct bool_cursor;
		constexpr reference( BcIt it, unsigned char shift ) noexcept;
		BcIt m_it;
		unsigned char m_shift;
	};
	constexpr void swap( bool_cursor &other ) noexcept;
private:
	BcIt m_it;
	unsigned char m_shift;
};

template<bool_cursor_iterator BcIt>
constexpr bool_cursor<BcIt>::bool_cursor( BcIt begin, std::ptrdiff_t 
offset ) noexcept :
	m_it( begin + (offset >> 3) ),
	m_shift( (unsigned char)offset & 7 )
{
}

template<bool_cursor_iterator BcIt>
constexpr bool_cursor<BcIt>::bool_cursor( bool_cursor const &other ) 
noexcept :
	m_it( other.m_it ),
	m_shift( other.m_shift )
{
}

template<bool_cursor_iterator BcIt>
constexpr bool_cursor<BcIt> &bool_cursor<BcIt>::operator =( bool_cursor 
const &other ) noexcept
{
	m_it = other.m_it;
	m_shift = other.m_shift;
	return *this;
}

template<bool_cursor_iterator BcIt>
constexpr bool_cursor<BcIt> bool_cursor<BcIt>::operator +( 
difference_type offset ) const noexcept
{
	using namespace std;
	assert(in_range_add( (ptrdiff_t)m_shift, offset ));
	ptrdiff_t iBit = (ptrdiff_t)m_shift + offset;
	return bool_cursor( m_it + (iBit >> 3), (unsigned char)iBit & 7 );
}

template<bool_cursor_iterator BcIt>
constexpr bool_cursor<BcIt> bool_cursor<BcIt>::operator -( 
difference_type offset ) const noexcept
{
	using namespace std;
	assert(in_range_sub( (ptrdiff_t)m_shift, offset ));
	ptrdiff_t iBit = (ptrdiff_t)m_shift - offset;
	return bool_cursor( m_it + (iBit >> 3), (unsigned char)iBit & 7 );
}

template<bool_cursor_iterator BcIt>
constexpr bool_cursor<BcIt> &bool_cursor<BcIt>::operator ++() noexcept
{
	unsigned char newShift = m_shift + 1 & 7;
	m_it += newShift < m_shift;
	m_shift = newShift;
	return *this;
}

template<bool_cursor_iterator BcIt>
constexpr bool_cursor<BcIt> &bool_cursor<BcIt>::operator --() noexcept
{
	unsigned char newShift = m_shift - 1 & 7;
	m_it -= newShift > m_shift;
	m_shift = newShift;
	return *this;
}

template<bool_cursor_iterator BcIt>
constexpr bool_cursor<BcIt> bool_cursor<BcIt>::operator ++( int ) noexcept
{
	bool_cursor ret( *this );
	++*this;
	return ret;
}

template<bool_cursor_iterator BcIt>
constexpr bool_cursor<BcIt> bool_cursor<BcIt>::operator --( int ) noexcept
{
	bool_cursor ret( *this );
	--*this;
	return ret;
}

template<bool_cursor_iterator BcIt>
constexpr typename bool_cursor<BcIt>::difference_type 
bool_cursor<BcIt>::operator -( bool_cursor const &other ) const noexcept
{
	using namespace std;
	assert((ptrdiff_t)(m_it - other.m_it) * 8 / 8 == (m_it - other.m_it) && 
in_range_add( (ptrdiff_t)(m_it - other.m_it), (ptrdiff_t)(signed 
char)(m_shift - other.m_shift) ));
	return (ptrdiff_t)(m_it - other.m_it) + (ptrdiff_t)(signed 
char)(m_shift - other.m_shift);
}

template<bool_cursor_iterator BcIt>
constexpr bool bool_cursor<BcIt>::operator <( bool_cursor const &other ) 
const noexcept
{
	return m_it < other.m_it || m_it == other.m_it && m_shift < other.m_shift;
}

template<bool_cursor_iterator BcIt>
constexpr bool bool_cursor<BcIt>::operator <=( bool_cursor const &other 
) const noexcept
{
	return m_it < other.m_it || m_it == other.m_it && m_shift <= other.m_shift;
}

template<bool_cursor_iterator BcIt>
constexpr bool bool_cursor<BcIt>::operator >( bool_cursor const &other ) 
const noexcept
{
	return m_it > other.m_it || m_it == other.m_it && m_shift > other.m_shift;
}

template<bool_cursor_iterator BcIt>
constexpr bool bool_cursor<BcIt>::operator >=( bool_cursor const &other 
) const noexcept
{
	return m_it > other.m_it || m_it == other.m_it && m_shift >= other.m_shift;
}

template<bool_cursor_iterator BcIt>
constexpr bool_cursor<BcIt> &bool_cursor<BcIt>::operator +=( 
difference_type offset ) noexcept
{
	assert(in_range_add( (ptrdiff_t)m_shift, offset ));
	ptrdiff_t iBit = (ptrdiff_t)m_shift + offset;
	m_it += iBit >> 3;
	m_shift = (unsigned char)iBit & 7;
	return *this;
}

template<bool_cursor_iterator BcIt>
constexpr bool_cursor<BcIt> &bool_cursor<BcIt>::operator -=( 
difference_type offset ) noexcept
{
	assert(in_range_sub( (ptrdiff_t)m_shift, offset ));
	ptrdiff_t iBit = (ptrdiff_t)m_shift - offset;
	m_it -= iBit >> 3;
	m_shift = (unsigned char)iBit & 7;
	return *this;
}

template<bool_cursor_iterator BcIt>
constexpr typename bool_cursor<BcIt>::reference 
bool_cursor<BcIt>::operator *() const noexcept
{
	return reference( m_it, m_shift );
}

template<bool_cursor_iterator BcIt>
constexpr bool_cursor<BcIt>::reference::operator bool() const noexcept
{
	unsigned char value;
	std::memcpy( &value, to_address( m_it ), 1 );
	return (bool)(value >> m_shift & 1);
}

template<bool_cursor_iterator BcIt>
constexpr bool bool_cursor<BcIt>::reference::operator =( bool f ) const 
noexcept
{
	using namespace std;
	unsigned char value;
	memcpy( &value, to_address( m_it ), 1 );
	value |= (unsigned char)f << m_shift;
	memcpy( to_address( m_it ), &value, 1 );
	return f;
}

template<bool_cursor_iterator BcIt>
constexpr typename bool_cursor<BcIt>::reference 
bool_cursor<BcIt>::operator []( difference_type offset ) const noexcept
{
	using namespace std;
	assert(in_range_add( (ptrdiff_t)m_shift, offset ));
	ptrdiff_t iBit = (ptrdiff_t)m_shift + offset;
	return reference( m_it + (iBit >> 3), (unsigned char)iBit & 7 );
}

template<bool_cursor_iterator BcIt>
constexpr bool_cursor<BcIt>::reference::reference( BcIt it, unsigned 
char shift ) noexcept :
	m_it( it ),
	m_shift( shift )
{
}

namespace std
{
	template<typename BcIt>
	constexpr void swap( bool_cursor<BcIt> &left, bool_cursor<BcIt> &right 
) noexcept
	{
		left.swap( right );
	}
}

[toc] | [prev] | [next] | [standalone]


#84407

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-06-01 07:31 +0200
Message-ID<t76ti7$agi$1@dont-email.me>
In reply to#84404
And this even more fancy. A class to handle a series of bit-fields
nearly a convenient like handling a series of dissimilar byte sequences
with a normal pointer. Conforming to C++ aliasing-constraints that
aliasing is only absolutely safe if you alias via memcpy().

#pragma once
#include <cstdint>
#include <cassert>
#include <iterator>
#include <cassert>
#include <type_traits>
#include <iterator>
#include <cstring>
#include <utility>
#include "bswap.h"
#include "range_check.h"

#if defined(_MSC_VER)
	#pragma warning(push)
	#pragma warning(disable: 4554) // operator predence
	#pragma warning(disable: 26495) // always initialize members
#endif
#if defined(__llvm__)
	#pragma clang diagnostic push
	#pragma clang diagnostic ignored "-Wshift-op-parentheses"
	#pragma clang diagnostic ignored "-Wlogical-op-parentheses"
	#pragma clang diagnostic ignored "-Wbitwise-op-parentheses"
#endif

static_assert(std::bit_floor( sizeof(max_align_t) ) == 
sizeof(max_align_t), "sizeof(max_align_t) must be a power of two");
static_assert(sizeof(std::max_align_t) >= sizeof(std::uint64_t), 
"sizeof(max_align_t) must be the same or a a larger power of two than 
sizeof(uint64_t)");

template<typename RandIt>
concept bit_cursor_iterator = std::random_access_iterator<RandIt> && 
(sizeof(std::iter_value_t<RandIt>) == 1);

template<bit_cursor_iterator BitIt>
struct bit_cursor
{
	using value_type = std::uint64_t;
	using difference_type = std::ptrdiff_t;
	bit_cursor() = delete;
	constexpr bit_cursor( BitIt begin, std::ptrdiff_t offset = 0 ) noexcept;
	constexpr bit_cursor( bit_cursor &other ) noexcept;
	constexpr bit_cursor &operator =( bit_cursor const &other ) noexcept;
	constexpr bool operator ==( bit_cursor const &other ) const noexcept;
	constexpr bool operator !=( bit_cursor const &other ) const noexcept;
	constexpr bit_cursor operator +( difference_type offset ) const noexcept;
	constexpr bit_cursor operator -( difference_type offset ) const noexcept;
	constexpr difference_type operator -( bit_cursor const &other ) const 
noexcept;
	constexpr bool operator <( bit_cursor const &other ) const noexcept;
	constexpr bool operator <=( bit_cursor const &other ) const noexcept;
	constexpr bool operator >( bit_cursor const &other ) const noexcept;
	constexpr bool operator >=( bit_cursor const &other ) const noexcept;
	constexpr bit_cursor &operator +=( difference_type offset ) noexcept;
	constexpr bit_cursor &operator -=( difference_type offset ) noexcept;
	constexpr std::uint64_t read_na( unsigned char bits ) noexcept;
	constexpr void write_na( std::uint64_t value, unsigned char bits ) 
noexcept;
	constexpr std::uint64_t read( unsigned char bits ) noexcept;
	constexpr void write( std::uint64_t value, unsigned char bits ) noexcept;
	template<unsigned char Bits>
		requires (Bits > 0 && Bits <= 64)
	constexpr std::uint64_t read_na() noexcept;
	template<unsigned char Bits>
		requires (Bits > 0 && Bits <= 64)
	constexpr void write_na( std::uint64_t value ) noexcept;
	template<unsigned char Bits>
		requires (Bits > 0 && Bits <= 64)
	constexpr std::uint64_t read() noexcept;
	template<unsigned char Bits>
		requires (Bits > 0 && Bits <= 64)
	constexpr void write( std::uint64_t value ) noexcept;
	constexpr void swap( bit_cursor &other ) noexcept;
private:
	static std::uint64_t dRead( std::uint64_t const &data ) noexcept;
	static void dWrite( std::uint64_t &data, std::uint64_t value ) noexcept;
	constexpr bit_cursor( BitIt begin, std::uint64_t *data, unsigned char 
shift ) noexcept;
	static constexpr std::uint64_t bswap( std::uint64_t value ) noexcept;
#if !defined(NDEBUG)
	BitIt m_safeData;
#endif
	std::uint64_t *m_data;
	unsigned char m_shift;
};

template<bit_cursor_iterator BitIt>
constexpr bit_cursor<BitIt>::bit_cursor( BitIt begin, std::ptrdiff_t 
offset ) noexcept :
#if !defined(NDEBUG)
	m_safeData( begin + (offset >> 3) ),
#endif
	m_data( (std::uint64_t *)((std::size_t)std::to_address( begin ) + 
(offset >> 3) & -(std::ptrdiff_t)sizeof(std::uint64_t)) ),
	m_shift( (unsigned char)((char *)std::to_address( begin ) - (char 
*)m_data) * 8 + (unsigned char)(offset & 7) )
{
}

template<bit_cursor_iterator BitIt>
constexpr bit_cursor<BitIt>::bit_cursor( bit_cursor &other ) noexcept :
#if !defined(NDEBUG)
	m_safeData( other.m_safeData ),
#endif
	m_data( other.m_data ),
	m_shift( other.m_shift )
{
}

template<bit_cursor_iterator BitIt>
constexpr bit_cursor<BitIt> &bit_cursor<BitIt>::operator =( bit_cursor 
const &other ) noexcept
{
#if !defined(NDEBUG)
	m_safeData = other.m_safeData;
#endif
	m_data = other.m_data;
	m_shift = other.m_shift;
	return *this;
}

template<bit_cursor_iterator BitIt>
constexpr bool bit_cursor<BitIt>::operator ==( bit_cursor const &other ) 
const noexcept
{
	return m_data == other.m_data && m_shift == other.m_shift;
}

template<bit_cursor_iterator BitIt>
constexpr bool bit_cursor<BitIt>::operator !=( bit_cursor const &other ) 
const noexcept
{
	return m_data != other.m_data || m_shift != other.m_shift;
}

template<bit_cursor_iterator BitIt>
constexpr bit_cursor<BitIt> bit_cursor<BitIt>::operator +( 
difference_type offset ) const noexcept
{
	using namespace std;
#if !defined(NDEBUG)
	BitIt newSafeIt( m_safeData + ((ptrdiff_t)(m_shift & 7) + (offset >> 3)) );
#else
	BitIt newSafeIt;
#endif
	assert(in_range_add( (ptrdiff_t)m_shift, offset ));
	ptrdiff_t iBit = (ptrdiff_t)m_shift + offset;
	return bit_cursor( newSafeIt, m_data + (iBit >> 6), (unsigned char)iBit 
% 64 );
}

template<bit_cursor_iterator BitIt>
constexpr bit_cursor<BitIt> bit_cursor<BitIt>::operator -( 
difference_type offset ) const noexcept
{
	using namespace std;
#if !defined(NDEBUG)
	BitIt newSafeIt( m_safeData + ((ptrdiff_t)(m_shift & 7) - (offset >> 3)) );
#else
	BitIt newSafeIt;
#endif
	assert(in_range_sub( (ptrdiff_t)m_shift, offset ));
	ptrdiff_t iBit = (ptrdiff_t)m_shift - offset;
	return bit_cursor( newSafeIt, m_data - (iBit >> 6), (unsigned char)iBit 
% 64 );
}

template<bit_cursor_iterator BitIt>
constexpr typename bit_cursor<BitIt>::difference_type 
bit_cursor<BitIt>::operator -( bit_cursor const &other ) const noexcept
{
	using namespace std;
	signed char shiftDiff = (signed char)(m_shift - other.m_shift);
	assert((m_data - other.m_data) * 64 / 64 == (m_data - other.m_data) && 
in_range_add( (m_data - other.m_data) * 64, (ptrdiff_t)shiftDiff ));
	return (m_data - other.m_data) * 64 + (ptrdiff_t)shiftDiff;
}

template<bit_cursor_iterator BitIt>
constexpr bool bit_cursor<BitIt>::operator <( bit_cursor const &other ) 
const noexcept
{
	return m_data < other.m_data || m_data == other.m_data && m_shift < 
other.m_shift;
}

template<bit_cursor_iterator BitIt>
constexpr bool bit_cursor<BitIt>::operator <=( bit_cursor const &other ) 
const noexcept
{
	return m_data < other.m_data || m_data == other.m_data && m_shift <= 
other.m_shift;
}

template<bit_cursor_iterator BitIt>
constexpr bool bit_cursor<BitIt>::operator >( bit_cursor const &other ) 
const noexcept
{
	return m_data > other.m_data || m_data == other.m_data && m_shift > 
other.m_shift;
}

template<bit_cursor_iterator BitIt>
constexpr bool bit_cursor<BitIt>::operator >=( bit_cursor const &other ) 
const noexcept
{
	return m_data > other.m_data || m_data == other.m_data && m_shift >= 
other.m_shift;
}

template<bit_cursor_iterator BitIt>
constexpr bit_cursor<BitIt> &bit_cursor<BitIt>::operator +=( 
difference_type offset ) noexcept
{
	using namespace std;
#if !defined(NDEBUG)
	(void)(m_safeData + ((ptrdiff_t)(m_shift & 7) + (offset >> 3)));
#endif
	assert(in_range_add( (ptrdiff_t)m_shift, offset ));
	ptrdiff_t iBit = (ptrdiff_t)m_shift + offset;
	m_data += iBit >> 6;
	m_shift = (unsigned char)iBit % 64;
	return *this;
}

template<bit_cursor_iterator BitIt>
constexpr bit_cursor<BitIt> &bit_cursor<BitIt>::operator -=( 
difference_type offset ) noexcept
{
	using namespace std;
#if !defined(NDEBUG)
	(void)(m_safeData + ((ptrdiff_t)(m_shift & 7) - (offset >> 3)));
#endif
	assert(in_range_sub( (ptrdiff_t)m_shift, offset ));
	ptrdiff_t iBit = (ptrdiff_t)m_shift - offset;
	m_data -= iBit >> 6;
	m_shift = (unsigned char)iBit % 64;
	return *this;
}

template<bit_cursor_iterator BitIt>
constexpr std::uint64_t bit_cursor<BitIt>::read_na( unsigned char bits ) 
noexcept
{
	using namespace std;
	assert(bits <= 64);
#if !defined(NDEBUG)
	(void)(m_safeData + ((ptrdiff_t)(m_shift & 7) + bits + 7) / 8);
#endif
	unsigned char
		shift = m_shift,
		rShift = 64 - shift;
	uint64_t
		mask = bits != 64 ? ((uint64_t)1 << bits) - 1 : (uint64_t)-1,
		value = bswap( dRead( m_data[0] ) ) >> shift & mask;
	if( bits > rShift )
		value |= bswap( dRead( m_data[1] ) ) << rShift & mask;
	return value;
}

template<bit_cursor_iterator BitIt>
constexpr void bit_cursor<BitIt>::write_na( std::uint64_t value, 
unsigned char bits ) noexcept
{
	using namespace std;
	assert(bits <= 64);
#if !defined(NDEBUG)
	(void)(m_safeData + ((ptrdiff_t)(m_shift & 7) + bits + 7) / 8);
#endif
	unsigned char
		shift = m_shift,
		rShift = 64 - shift;
	uint64_t mask = bits != 64 ? ((uint64_t)1 << bits) - 1 : (uint64_t)-1;
	assert(!(value & ~mask));
	dWrite( m_data[0], bswap( bswap( dRead( m_data[0] ) ) & ~(mask << 
shift) | value << shift ) );
	if( bits > rShift )
		dWrite( m_data[1], bswap( bswap( dRead( m_data[1] ) ) & ~(mask >> 
rShift) | value >> rShift ) );
}

template<bit_cursor_iterator BitIt>
constexpr std::uint64_t bit_cursor<BitIt>::read( unsigned char bits ) 
noexcept
{
	std::uint64_t value = read_na( bits );
	*this += (difference_type)bits;
	return value;
}

template<bit_cursor_iterator BitIt>
constexpr void bit_cursor<BitIt>::write( std::uint64_t value, unsigned 
char bits ) noexcept
{
	write_na( value, bits );
	*this += (difference_type)bits;
}

template<bit_cursor_iterator BitIt>
template<unsigned char Bits>
	requires (Bits > 0 && Bits <= 64)
constexpr std::uint64_t bit_cursor<BitIt>::read_na() noexcept
{
	using namespace std;
#if !defined(NDEBUG)
	(void)(m_safeData + ((ptrdiff_t)(m_shift & 7) + Bits + 7) / 8);
#endif
	unsigned char
		shift = m_shift,
		rShift = 64 - shift;
	constexpr uint64_t MASK = Bits != 64 ? ((uint64_t)1 << Bits) - 1 : 
(uint64_t)-1;
	uint64_t value = bswap( dRead( m_data[0] ) ) >> shift & MASK;
	if( Bits > rShift )
		value |= bswap( dRead( m_data[1] ) ) << rShift & MASK;
	return value;
}

template<bit_cursor_iterator BitIt>
template<unsigned char Bits>
	requires (Bits > 0 && Bits <= 64)
constexpr void bit_cursor<BitIt>::write_na( std::uint64_t value ) noexcept
{
	using namespace std;
#if !defined(NDEBUG)
	(void)(m_safeData + ((ptrdiff_t)(m_shift & 7) + Bits + 7) / 8);
#endif
	unsigned char
		shift = m_shift,
		rShift = 64 - shift;
	constexpr uint64_t MASK = Bits != 64 ? ((uint64_t)1 << Bits) - 1 : 
(uint64_t)-1;
	assert(!(value & ~MASK));
	dWrite( m_data[0], bswap( bswap( dRead( m_data[0] ) ) & ~(MASK << 
shift) | value << shift ) );
	if( Bits > rShift )
		dWrite( m_data[1], bswap( bswap( dRead( m_data[1] ) ) & ~(MASK >> 
rShift) | value >> rShift ) );
}

template<bit_cursor_iterator BitIt>
template<unsigned char Bits>
	requires (Bits > 0 && Bits <= 64)
constexpr std::uint64_t bit_cursor<BitIt>::read() noexcept
{
	std::uint64_t value = this->template read_na<Bits>();
	*this += (difference_type)Bits;
	return value;
}

template<bit_cursor_iterator BitIt>
template<unsigned char Bits>
	requires (Bits > 0 && Bits <= 64)
constexpr void bit_cursor<BitIt>::write( std::uint64_t value ) noexcept
{
	this->template write_na<Bits>( value);
	*this += (difference_type)Bits;
}

template<bit_cursor_iterator BitIt>
constexpr void bit_cursor<BitIt>::swap( bit_cursor &other ) noexcept
{
#if !defined(NDEBUG)
	std::swap( m_safeData, other.m_safeData );
#endif
	std::swap( m_data, other.m_data );
	std::swap( m_shift, other.m_shift );
}

template<bit_cursor_iterator BitIt>
std::uint64_t bit_cursor<BitIt>::dRead( std::uint64_t const &data ) noexcept
{
	uint64_t value;
	std::memcpy( &value, &data, sizeof(std::uint64_t) );
	return value;
}

template<bit_cursor_iterator BitIt>
void bit_cursor<BitIt>::dWrite( std::uint64_t &data, std::uint64_t value 
) noexcept
{
	std::memcpy( &data, &value, sizeof(std::uint64_t) );
}

template<bit_cursor_iterator BitIt>
constexpr bit_cursor<BitIt>::bit_cursor( BitIt begin, std::uint64_t 
*data, unsigned char shift ) noexcept :
#if !defined(NDEBUG)
	m_safeData( begin ),
#endif
	m_data( data ),
	m_shift( shift )
{
}

template<bit_cursor_iterator BitIt>
constexpr std::uint64_t bit_cursor<BitIt>::bswap( std::uint64_t value ) 
noexcept
{
	return from_little_endian( value );
}

namespace std
{
	template<typename BitIt>
	constexpr void swap( bit_cursor<BitIt> &left, bit_cursor<BitIt> &right 
) noexcept
	{
		left.swap( right );
	}
}

#if defined(_MSC_VER)
	#pragma warning(pop)
#endif
#if defined(__llvm__) || defined(__INTEL_LLVM_COMPILER)
	#pragma clang diagnostic pop
#endif

[toc] | [prev] | [next] | [standalone]


#84417

From"Alf P. Steinbach" <alf.p.steinbach@gmail.com>
Date2022-06-01 14:20 +0200
Message-ID<t77lhu$39i$1@dont-email.me>
In reply to#84404
On 1 Jun 2022 05:46, Bonita Montero wrote:
> I use my own class
> [snipalot]

Two alternatives:

* Use a `vector<Truth>` where `Truth` is your own class type `bool` 
replacement.
* Use a `deque<bool>`.

The first possibility is preferable when you have a `Truth` type that 
limits implicit conversions to conversion to and from `bool`.


Cheers,

- Alf

[toc] | [prev] | [next] | [standalone]


#84419

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-06-01 14:23 +0200
Message-ID<t77ln4$7v3$2@dont-email.me>
In reply to#84417
Am 01.06.2022 um 14:20 schrieb Alf P. Steinbach:
> On 1 Jun 2022 05:46, Bonita Montero wrote:
>> I use my own class
>> [snipalot]
> 
> Two alternatives:
> 
> * Use a `vector<Truth>` where `Truth` is your own class type `bool` 
> replacement.

That's not possible because the minimal data type a vector<> can
address is the size of a native type or structure - which in turn
is one.

> * Use a `deque<bool>`.

LOL.

[toc] | [prev] | [next] | [standalone]


#84421

From"Alf P. Steinbach" <alf.p.steinbach@gmail.com>
Date2022-06-01 14:26 +0200
Message-ID<t77lts$euh$1@dont-email.me>
In reply to#84419
On 1 Jun 2022 14:23, Bonita Montero wrote:
> Am 01.06.2022 um 14:20 schrieb Alf P. Steinbach:
>> On 1 Jun 2022 05:46, Bonita Montero wrote:
>>> I use my own class
>>> [snipalot]
>>
>> Two alternatives:
>>
>> * Use a `vector<Truth>` where `Truth` is your own class type `bool` 
>> replacement.
> 
> That's not possible because the minimal data type a vector<> can
> address is the size of a native type or structure - which in turn
> is one.

Well, you know, as Niels Bohr said about the good luck horseshoe he had 
on the wall in his office, it may work even if you don't believe in it.

<url: https://quoteinvestigator.com/2013/10/09/horseshoe-luck/>


>> * Use a `deque<bool>`.
> 
> LOL.

It also works.


Cheers,

- Alf

[toc] | [prev] | [next] | [standalone]


#84423

FromBonita Montero <Bonita.Montero@gmail.com>
Date2022-06-01 14:40 +0200
Message-ID<t77mod$out$2@dont-email.me>
In reply to#84421
Am 01.06.2022 um 14:26 schrieb Alf P. Steinbach:
> On 1 Jun 2022 14:23, Bonita Montero wrote:
>> Am 01.06.2022 um 14:20 schrieb Alf P. Steinbach:
>>> On 1 Jun 2022 05:46, Bonita Montero wrote:
>>>> I use my own class
>>>> [snipalot]
>>>
>>> Two alternatives:
>>>
>>> * Use a `vector<Truth>` where `Truth` is your own class type `bool` 
>>> replacement.
>>
>> That's not possible because the minimal data type a vector<> can
>> address is the size of a native type or structure - which in turn
>> is one.
> 
> Well, you know, as Niels Bohr said about the good luck horseshoe he had 
> on the wall in his office, it may work even if you don't believe in it.
> 
> <url: https://quoteinvestigator.com/2013/10/09/horseshoe-luck/>
> 
> 
>>> * Use a `deque<bool>`.
>>
>> LOL.
> 
> It also works.

Show me the data-type in source feeded to deque for this purpose
to make that working.

[toc] | [prev] | [standalone]


Page 5 of 5 — ← Prev page 1 2 3 4 [5]

Back to top | Article view | comp.lang.c++


csiph-web