Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bitset with data type based upon value ranges

49 views
Skip to first unread message

Aaron Gray

unread,
Jan 6, 2023, 7:14:35 PM1/6/23
to
Hi,
I am looking for some template code to generate a type based upon a constant within value ranges, for specialization of a bitset class.

0...7 - unsigned char
8..15 - unsigned short
16...31 - unsigned int
32...63 - unsigned long
64...127 - unsigned long long
128... - unsigned long long [] - array

```
#include <cstddef>

template <size_t BITS>
class bitset {
public:
....
WORDTYPE data;
....
};
```

I am looking for solutions both with and without concepts for comparison, if its possible without and its simpler I will use that solution unless there is some real advantages to using concepts.

Many thanks in advance,

Aaron

Aaron Gray

unread,
Jan 6, 2023, 8:48:51 PM1/6/23
to
On Saturday, 7 January 2023 at 00:14:35 UTC, Aaron Gray wrote:
> Hi,
> I am looking for some template code to generate a type based upon a constant within value ranges, for specialization of a bitset class.
>
> 0...7 - unsigned char
> 8..15 - unsigned short
> 16...31 - unsigned int
> 32...63 - unsigned long
> 64...127 - unsigned long long
> 128... - unsigned long long [] - array
>
> ```
> #include <cstddef>
>
> template <size_t BITS>
> class bitset {
> public:
> ....
> WORDTYPE data;
> ....
> };
> ```

ChatGTP has given me the following :-
```
#include <type_traits>

template <size_t BITS>
class bitset {
public:
static_assert(BITS > 0, "Number of bits must be positive");

using WORDTYPE = typename std::conditional<BITS <= 8, unsigned char,
typename std::conditional<BITS <= 15, unsigned short,
typename std::conditional<BITS <= 31, unsigned int,
typename std::conditional<BITS <= 63, unsigned long,
typename std::conditional<BITS <= 127, unsigned long long,
unsigned long long[]>::type>::type>::type>::type;

WORDTYPE data;
// ...
};
```

A good start, but withh two bugs, a missing extra '>::type' and the array really wants to be a whole new template specialization with WORDTYPE :-

unsigned long long[(BITS+127)/128]

> I am looking for solutions both with and without concepts for comparison, if its possible without and its simpler I will use that solution unless there is some real advantages to using concepts.

I am thinking concepts may be necessary ?

If you would like to help on the journey it would be well appreciated :)

Aaron

Mut...@dastardlyhq.com

unread,
Jan 7, 2023, 6:19:27 AM1/7/23
to
Do you own coursework.

Öö Tiib

unread,
Jan 7, 2023, 11:49:46 AM1/7/23
to
On Saturday, 7 January 2023 at 02:14:35 UTC+2, aaron...@gmail.com wrote:
> Hi,
> I am looking for some template code to generate a type based upon a constant within value ranges, for specialization of a bitset class.
>
But it is trivial with std::conditional in <type_traits>. Also the example
in <https://en.cppreference.com/w/cpp/types/conditional> is almost what
you asked for. So what is the problem?

Otherwise you can freely get innumerable implementations of bitset.
The std::bitset, QBitArray, boost::dynamic_bitset, std::vector<bool> are more popular.
So you should be swimming over your head lake of code ... not be asking for more
of it.

Aaron Gray

unread,
Jan 7, 2023, 12:31:00 PM1/7/23
to
On Saturday, 7 January 2023 at 16:49:46 UTC, Öö Tiib wrote:
I am trying to put together a very efficient implementation to deal with fixed number of bits for dealing with lookahead sets for closure FIRST set guards for recursive decent parsers and a recursive descent parser generator. So want to use my own specific implmentation.

Aaron

Aaron Gray

unread,
Jan 7, 2023, 12:32:19 PM1/7/23
to
No I am an open source software engineer.

Aaron

Öö Tiib

unread,
Jan 7, 2023, 1:09:55 PM1/7/23
to
The std::conditional runs compile time so you can not get more efficient than
that.

Chris M. Thomasson

unread,
Jan 7, 2023, 3:45:54 PM1/7/23
to
On 1/6/2023 5:48 PM, Aaron Gray wrote:
> On Saturday, 7 January 2023 at 00:14:35 UTC, Aaron Gray wrote:
>> Hi,
>> I am looking for some template code to generate a type based upon a constant within value ranges, for specialization of a bitset class.
>>
>> 0...7 - unsigned char
>> 8..15 - unsigned short
>> 16...31 - unsigned int
>> 32...63 - unsigned long
>> 64...127 - unsigned long long
>> 128... - unsigned long long [] - array
>>
>> ```
>> #include <cstddef>
>>
>> template <size_t BITS>
>> class bitset {
>> public:
>> ....
>> WORDTYPE data;
>> ....
>> };
>> ```
>
> ChatGTP has given me the following :-
[...]

Ouch. ChatGTP seems to train itself when somebody has to correct its
generated code that it likely lifted from somebody else. Also, is the
correction correct itself?


Aaron Gray

unread,
Jan 7, 2023, 6:47:00 PM1/7/23
to
yep, its the types that matter :)

Aaron

Aaron Gray

unread,
Jan 7, 2023, 7:42:23 PM1/7/23
to
Yep, big ouch, it stung me, and wasted time.

Mut...@dastardlyhq.com

unread,
Jan 9, 2023, 4:19:06 AM1/9/23
to
What did you expect? ANNs are at their core simply statistical algorithms.
You'd probably get a similar result with a hidden markov model if it was given
enough input though admittedly it couldn't parse your question.

0 new messages