« first day (1766 days earlier)      last day (1078 days later) » 

3:34 AM
How to organise such a data structure:
key .......................> value
"uint16" --> std::map<u16_addr, std::vector<std::any>>
"float" --> std::map<u16_addr, std::vector<float_t>>
"double" --> std::map<u16_addr, std::vector<double_t>>

................................................................... type .....................>value
I don' think `std::map<u16_addr, std::pair<std::string, std::vector<std::any>>> ` is a good one.
Here is the right question:
How to organise such a data structure:
key .......................> value
"uint16" --> std::map<u16_addr, std::vector<uint16_t>>
"float" --> std::map<u16_addr, std::vector<float_t>>
"double" --> std::map<u16_addr, std::vector<double_t>>

................................................................... type .....................>value
I don' think `std::map<u16_addr, std::pair<std::string, std::vector<std::any>>> ` is a good one.
 
 
5 hours later…
nwp
8:37 AM
@John I don't understand what you're trying to do. Can you show a usage example of the data structure?
 
 
2 hours later…
10:44 AM
Is there any safe way to obtain the absolute value of std::numeric_limits<some_signed_int>::min() as a the_same_int_type_but_unsigned? (considering that -std::numeric_limits<some_signed_int>::max() often is different from std::numeric_limits<some_signed_int>::min())
 
@ThePirate42 I don't think it's strictly defined that there always has to be an unsigned equivalent of the same size
 
@PeterT Are you saying that, for example, signed long long could have a bigger size than unsigned long long in some implementations?
 
no, the sizes are the same, I'm just not sure how the value ranges are defined
nvm, I forgot they changed it
> However, all C++ compilers use two's complement representation, and as of C++20, it is the only representation allowed by the standard
 
11:03 AM
@PeterT Ok, but the absolute value of std::numeric_limits<some_signed_int>::min() should always be expressible as a value of type the_same_int_type_but_unsigned, right? How can I obtain safely that value?
 
In c, is it possible to create a struct that will be shared across the application? Sort of a singleton class like in c++?
 
Maybe mine is a question that I should post directly on the main site.
 
@ThePirate42 yeah, I can't immediately think of a solution without first promoting it to a larger type or bit-hacking based on the tws-complement guarantee
@Eminem sure put a static value in a function and return that
or make a global variable, which at the end of the day a singleton pretty much is
 
So i just need to put static before the type and functions outside the file will be able to use it?
 
I mean like "struct mystruct* getInstance(){static struct mystruct value; return &value;}"
 
nwp
11:09 AM
@ThePirate42 std::numeric_limits<std::make_unsigned_t<some_signed_int>>::min
Or ... maybe I misunderstood what you want.
 
@nwp Wouldn't that always be 0?
 
nwp
I'm pretty sure it would.
So it's not what you want.
 
he wants abs(std::numeric_limits<int>::min()) as an unsigned int
 
nwp
That already looks like C++.
 
that doesn't return an unsigned int
 
nwp
11:13 AM
abs(std::numeric_limits<int>::min()) + 0u :D
 
@nwp But, if I'm not mistaken, if an implementation uses two's complement representation, abs(std::numeric_limits<int>::min())should be undefined.
 
nwp
The documentation says in a comment that it's UB.
 
Wait, maybe I got it: static_cast<unsigned int>(abs(std::numeric_limits<int>::min()+1))+1
@nwp What does "UB" mean?
 
nwp
Undefined behavior. Meaning the program can do whatever it wants, including crash, give you the result you want or alternating depending on if you are testing the behavior or relying on it.
Sometimes UB results in helpful error messages.
@ThePirate42 I guess that does what you asked. You could also shorten it to 1u << 31.
 
11:34 AM
@nwp Does that mean that static_cast<std::make_unsigned_t<some_signed_int>>(std::numeric_limits<some_signed_int>::min()) is guaranteed to return the absolute value of std::numeric_limits<some_signed_int>::min()?
 
@nwp isn't this guaranteed to be equivalent to ~static_cast<int>(0)?
 
nwp
@ThePirate42 Probably. The standard says unsigned integers have modulo semantics, so you get -2147483648 % 4294967296 (the mathematical modulo, not the C++ modulo) which I'm pretty sure is 2147483648.
@Mgetz No? Mine is UB, yours is not. Also I don't understand why yours has a cast. Either way, yours is 1 less than the desired result and the wrong type.
 
@nwp it's 0539 and I'm still working on my cup of tea, and allergies are killing me ;p
 
nwp
Rip
 
🇫
 
11:47 AM
@nwp Probably
😭
 
nwp
Sorry, I have to be careful with "is it guaranteed that" type of questions to not be wrong.
 
@nwp I understand, I guess I'll make a question on the main site just to be sure. Anyway, thank you all for the help!
 
12:09 PM
Using std::map<u16_addr, std::vector<uint16>> to store some data when recieving a sting which begins with "uint16".
Using std::map<u16_addr, std::vector<uint32>> to store some data when recieving a sting which begins with "uint32".
Using std::map<u16_addr, std::vector<float>> to store some data when recieving a sting which begins with "float".
And, I can't know what the string may contain in advance.
@nwp Sorry for my poor English.
 
nwp
What stops you from using the obvious solution of if (string.find("uint16") == 0) { use(map_u16); } else if ...?
 
I have to define many stl with different types in advance, eg: std::map<u16_addr, std::vector<uint16>> mapU16; std::map<u16_addr, std::vector<uint32>> mapU32; std::map<u16_addr, std::vector<float>> mapFloat and etc.
You see, some of them may never be used even the program have been running for a whole day.
 
nwp
I don't understand the problem. Those unused maps cost you ~48 bytes of memory each. Even if you have millions of them it's an insignificant cost of 48MB.
 
class cDemo
{
public:
...
private:
std::map<u16_addr, std::vector<boolean>> mapBln;

std::map<u16_addr, std::vector<uint16_t>> mapU16;
std::map<u16_addr, std::vector<uint32_t>> mapU32;
std::map<u16_addr, std::vector<uint64_t>> mapU64;

std::map<u16_addr, std::vector<int16_t>> mapInt16;
std::map<u16_addr, std::vector<int32_t>> mapInt32;
std::map<u16_addr, std::vector<int64_t>> mapInt64;

std::map<u16_addr, std::vector<float>> mapFloat;
std::map<u16_addr, std::vector<double>> mapDouble;
}
Something like this?
 
12:25 PM
do you always know the u16_addr -> type mapping ? Or do you mean to look up the type and the value based on the address?
so can one address be used to look up values in two different maps?
 
@PeterT u16_addr is always equivalent to uint16_t
 
ok, what I mean is do you expect
mapU16[15] and mapU32[15] to both contain different values?
 
@PeterT A same address can't be used to look up values in two different maps.But different addresses can be used to look up values in different maps.
 
so one address --> one type, one value in that type
 
@PeterT Yes, mapU16[15] and mapU32[15] to both contain different values. The std::vector<T> is used to store the recived data.
 
12:30 PM
I don't understand how " same address can't be used to look up values in two different maps" and "mapU16[15] and mapU32[15] to both contain different values" is not contradictory
 
@PeterT several addresses -> one type, e.g: mapU16[15]={1,2,3}, map16[20]={3,6,9,11}. But if 15 is the key of mapU16, it can't be used as a key for any the other maps.
 
@John I think you're misusing "address" here... in c++ that refers to a definite point in memory space generally
 
@Mgetz You can think "address" as a simple value whose type is always uint16
 
then why not just use one unordered map and a std::variant?
@John that's not an address in c++, you'll confuse people. That's a key
if I say address I'm talking about a pointer
e.g. the result of std::addressof
 
@Mgetz Sorry for misleading you. It's a concept defined by Modbus. I would be more careful next time.
@Mgetz I use std::map<u16_addr, std::pair<varType, std::vector<std::any>>> at now. But I don't think it's a good method.
 
12:43 PM
@John without knowing what you're trying to do it's hard to say. But honestly you could just use a variant in most cases?
it would at least make the memory complexity simpler
 
@Mgetz Because the data received are all uint16 if receiving "uint16". std:: variant can't guarantee this.
 
I think he meant
std::variant<std::vector<uint16>, std::vector<uint32>,...>
not
std::vector<std::variant<uint16,uint32,...>>
 
@PeterT Thank you. It seems work. Let me think for a while.
@PeterT @Mgetz @PeterT There is still a problem. std::variant needs c++17 support and g++ 5.4.0(the compiler which I use) doesn't support c++17.
 
RIP
why so old?
 
1:00 PM
@Mgetz Because the supplier only suports up to g++ 5.4. And we seldom needs to enable c++17 support in our projects even which are developed on MSVC 2017.
 
nwp
You should probably encapsulate std::pair<varType, std::vector<std::any>> into a better interface at least.
 
@PeterT If I understand you correctly, I should declare a variable with the type of std::map<addr_t, std::variant<std::vector<uint16>, std::vector<uint32>,std::vector<float>>. Am I right? And addr_t is equivalent to uint16_t.
 
it's one way to do it, but that is honestly a bit long of a type and you can just make your own tagged-union like type if the code is rather contained
 
@nwp How? Could you please explain that in more detail for me? A simple code snippet may work.
@nwp To be honest, I know it's not a good method. But it's the only method that I can think of by myself. So, how to make it better?
@PeterT I see, thank you.
 
nwp
1:19 PM
@John Maybe something like this.
 
 
3 hours later…
4:35 PM
Is there any way to make
std::binary_search
search for a number that satisfies a condition inside a range of natural numbers, without constructing first an array of that range of natural numbers? Is it better to just reimplement a binary search? (obviously I already know that a numbers that satisfies the condition exist and it's unique)
 
you could create a custom iterator to do that, but honestly, I would just re-imlement binary search
at least I think it would be faster for me to implement binary search, than to implement a custom iterator class with like 8 operator overloads or so
 
 
4 hours later…
8:41 PM
@ThePirate42 also make sure to use golden section search :-)
 
9:09 PM
@Mikhail I searched it up and I didn't understand anything 😅
 
10:04 PM
@ThePirate42 If you know the number is present you don't want std::binary_search. You probably want lower_bound or upper_bound binary_search just returns a bool to indicate whether a number is present. lower_bound and upper_bound give you the location of the number.
Second, implementing a custom iterator is really pretty easy (at last in my experience).
 

« first day (1766 days earlier)      last day (1078 days later) »