What is this use of std::map doing? - c++

Can anyone explain the output I am getting from this simple program using std::map. Note that I insert p into the map, but not q yet it says it found them both, but also says there is only 1 element in the map!
#include <map>
#include <iostream>
struct screenPoint {
float x = 0, y = 0;
screenPoint(float x_, float y_): x{x_}, y{y_}{}
};
bool operator<(const screenPoint& left, const screenPoint& right){
return left.x<right.x&&left.y<right.y;
}
std::map<screenPoint, float> positions;
int main(int argc, const char * argv[]) {
auto p = screenPoint(1,2);
auto q = screenPoint(2,1);
positions.emplace(p,3);
auto f = positions.find(p);
auto g = positions.find(q);
if (f == positions.end()){
std::cout << "f not found";
} else {
std::cout << "f found";
}
std::cout << std::endl;
if (g == positions.end()){
std::cout << "g not found";
} else {
std::cout << "g found";
}
std::cout << std::endl;
std::cout << "number elements: " << positions.size() << "\n";
return 0;
}
Output:
f found
g found
number elements: 1

The problem is with the way you defined the comparison functor, in this case. The two elements, p, and q, have the same x and y, just inverted.
Your logic checks that the x of one is less than that of the other, as well as the ys. This can never evaluate to true, for these inputs.
Try this snippet:
int main()
{
auto p = screenPoint(1,2);
auto q = screenPoint(2,1);
std::cout << std::boolalpha << (p < q) << " " << (q < p) << std::endl;
}
It will print out
false false
So p is not less than q, and q is not less than p. As far as the map is concerned, that makes them equivalent.

In order to use a data type in an std::map, it must have a particular ordering called a strict weak ordering (https://en.wikipedia.org/wiki/Weak_ordering). This means that the inequality operator (<) obeys a very specific set of rules. The operator you specified however is not a weak ordering. In particular, given two screenPoints, a and b constructed from (1,2) and (2,1) respectively, you will see that it is false both that a < b and that b < a. In a strict weak ordering, this would be required to imply that a == b, which is not true!
Because your inequality operator does not meet the requirement of a strict weak ordering, map ends up doing unexpected things. I recommend reading up more details on what this ordering is, and reading/thinking about why map requires it. In the short term, you can redefine your operator as follows:
bool operator<(const screenPoint& left, const screenPoint& right){
if (left.x != right.x) return left.x < right.x;
else return (left.y < right.y);
}

Related

Using std::set container for range items

I'd like to store a bunch of range items in std::set container.
This data structure should provide fast decision whether a specific input range contained by one of the ranges that the set currently holds, by overloading the comparison of std::set in order use the set::find method to check one of the items in set contain the input range argument.
It should also support range item that represents a single point (start_range == end_range).
Here's my implementation :
#include <iostream>
#include <map>
#include <set>
using std::set;
using std::map;
class range : public std::pair<int,int>
{
public:
range(int lower, int upper)
{
if (upper < lower)
{
first = upper;
second = lower;
}
else
{
first = lower;
second = upper;
}
}
range(int val)
{
first = second = val;
}
bool operator<(range const & b) const
{
if (second < b.first)
{
return true;
}
return false;
}
};
And here's how I test my data structure:
int main(int argc, const char * argv[])
{
std::map<int, std::set<range>> n;
n[1].insert(range(-50,-40));
n[1].insert(range(40,50));
n[2].insert(range(-30,-20));
n[2].insert(range(20,30));
n[3].insert(range(-20,-10));
n[3].insert(range(10,20));
range v[] = {range(-50,-41), range(30,45), range(-45,-45), range(25,25)};
int j[] = {1,2,3};
for (int l : j)
{
for (range i : v)
{
if (n[l].find(i) != n[l].end())
{
std::cout << l << "," << i.first << "," << i.second << " : "
<< n[l].find(range(i))->first << " "
<< n[l].find(range(i))->second << std::endl;
}
}
}
}
and here are the results I get:
1,-50,-41 : -50 -40 --> good
1,30,45 : 40 50 --> bad
1,-45,-45 : -50 -40 --> good
2,30,45 : 20 30 --> bad
2,25,25 : 20 30 --> good
So as you can see, my code does support perfectly well single point range (-45 is contained by range (-50,-40) and 25 is contained by by range (20,30))
However, as for wider ranges, my current operator < is capable of finding the contained relationship which is equal for the set terminology (meaning that for ranges a and b a<b && a<b.
Is there anyway to change this operator to make it work ?
Sounds like a perfect match for using Boost Interval Container Library. In short, you can
#include <boost/icl/interval_set.hpp>
// Helper function template to reduce explicit typing:
template <class T>
auto closed(T&& lower, T&& upper)
{
return boost::icl::discrete_interval<T>::closed(std::forward<T>(lower),
std::forward<T>(upper));
}
boost::icl::interval_set<int> ranges;
ranges.insert(closed(1, 2));
ranges.insert(closed(42, 50));
std::cout << contains(ranges, closed(43, 46)) << "\n"; // true
std::cout << contains(ranges, closed(42, 54)) << "\n"; // false
This should easily be pluggable into your std::map and be usable without further adjustments.
Your operator < defines partial order:
(30,45) < (40, 50) == false and simultaneously (40, 50) < (30, 45) == false so in terms of std::set and std::map they are equal. That is why you got these results.
There is a paper about partial order: https://en.wikipedia.org/wiki/Partially_ordered_set
You might want use std::unordered_map or define somehow total order for your ranges.
I suggest operator < that compares the arithmetical mean of range bounds, i.e.
(a, b) < (c, d) if and only if (a+b)/2 < (c+d)/2 for total order. Note that you might want use float for arithmetical mean.
For testing I suggest the following code draft (I write here from scratch and didn't tested it). -1 meanst that are no range that contains this
int range::firstContainsMe(const std::vector<range> rangesVec)
{
for (size_t i = 0; i < rangesVec; i++) {
if (lower >= rangesVec[i].lower && upper <= rangesVec[i].upper) {
return i;
}
}
return -1;
}
Your comparison operator is unsuitable.
If you wish to use any container or algorithm based on ordering in C++, the ordering relation needs to be a Strict Weak Ordering Relation. The definition can be found on Wikipedia, in short the following rules must be respected:
Irreflexivity: For all x in S, it is not the case that x < x.
Asymmetry: For all x, y in S, if x < y then it is not the case that y < x.
Transitivity: For all x, y, z in S, if x < y and y < z then x < z.
Transitivity of Incomparability: For all x, y, z in S, if x is incomparable with y (neither x < y nor y < x hold), and y is incomparable with z, then x is incomparable with z.
Your comparison operator fails, and therefore is unsuitable. In general, a quick way of obtaining a good comparison operator is to do what tuples do:
bool operator<(range const & b) const
{
return std::tie(first, second) < std::tie(b.first, b.second);
}
You want a map, not a set.
In order to solve your problem, you want a map, not a set.
For disjoint intervals, a map from lower-bound to upper-bound is sufficient:
std::map<int, int> intervals;
The .lower_bound and .upper_bound operations allow finding the closest key in O(log N) time, and from there containment is quickly asserted.
For non-disjoint intervals, things get trickier I fear, and you'll want to start looking into specialized data-structures (Interval Trees for example).

How is this conditional being parsed?

I've been really clumsy and ran into a bug due to my own fault. Basically:
#include <iostream>
float a = 4.f;
float b = 6.f;
int main()
{
if (!a < b)
{
std::cout << "a is not less than b";
}
else std::cout << "a is less than b";
std::cin.ignore();
return 0;
}
So this conditional if statement always evaluates to true. If I wrap it in brackets as !(a < b) it works. So I understand I need the brackets.
My question is how is my wrong example parsed?
if (!a < b)
a has float value 4.f, does it turn 4.f from float value to boolean value, which is true, and then negate it to false, and then compare the false boolean value to b, which is 6.f float? How would it go about comparing a false boolean value to a float value of 6.f? If it converts 6.f float to boolean value the 6.f would turn into true, and the statement would be if (false < true), which doesn't make sense. Is this the way it's working?
I'm not sure if it's turned to if (false < true) because:
float a = 4.f;
float b = -50.f;
int main()
{
if (!a < b)
{
std::cout << "a is not less than b";
}
else std::cout << "a is less than b";
std::cin.ignore();
return 0;
}
Always ends up false. I thought that -50.f should be turned to true. On the other hand, if I assign to an intermediary variable it's a different case:
float a = 4.f;
float b = -50.f;
int main()
{
bool temp = b;
if (!a < temp)
{
std::cout << "a is not less than b";
}
else std::cout << "a is less than b";
std::cin.ignore();
return 0;
}
Now it's always true.
Thanks to user apple apple for figuring this out, it turns out it parses as follows:
if (!a < b)
if (!4.f < 6.f) // Changes 4.f float to false boolean
if (false < 6.f)
if (0.f < 6.f) // Promotes false boolean to 0.f
if you check the operator precedence, you can see !(Logical NOT) would be evaluate first, so
!a < b would be parsed as (!a) < b
since a != 0, (!a) => false
it becomes false < b which is same as 0 < b
Try printing !a
std::cout << !a;
Logically negating a positive (or negative) value in C++ will give 0. Since 0 is less than 4.0, it is true.
There is additional information in this answer.

Building a large boost unordered_map with cpp_int

I am writing some code in c++ for a class assignment that requires work with multiprecision library such as boost. Basically, I need to build a hash table with some large integers and then lookup a certain value in that table.
When I use h, g, p that are commented out - the code runs fine and very quickly. Once I switch to those that are not commented out, it throws a memory exception at line: hash_str>::iterator got = mp.find(lkp);
I am just starting out with c++ and pretty sure that something is way off, because this should run rather quickly, even with large numbers.
#include <boost/unordered_map.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/math/special_functions/pow.hpp>
using namespace std;
using namespace boost::multiprecision;
template <typename T>
struct hash_str
{
size_t operator()( const T& t ) const
{
return std::hash<std::string>()
( t.str() );
}
};
int main()
{
boost::unordered_map<cpp_int, cpp_int, hash_str<cpp_int>> mp;
//boost::unordered_map<hash_str<cpp_int>, cpp_int, hash_str<cpp_int>> mp;
cpp_int k;
cpp_int h( "3239475104050450443565264378728065788649097520952449527834792452971981976143292558073856937958553180532878928001494706097394108577585732452307673444020333" );
cpp_int g( "11717829880366207009516117596335367088558084999998952205599979459063929499736583746670572176471460312928594829675428279466566527115212748467589894601965568" );
//cpp_int g = 1010343267;
//cpp_int h = 857348958;
//cpp_int p = 1073676287;
cpp_int p( "13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084171" );
int b = pow( 2, 20 );
cpp_int denom;
cpp_int inv = powm( g, p - 2, p );
//building a hash table of all values h/g^x1
for ( cpp_int x = 1; x < b; ++x )
{
// go through all 2^20 values up to b, calculate the function h/g^x1,
// then hash it to put into table
denom = powm( inv, x, p );
k = ( h *denom ) % p;
mp.insert( std::make_pair( k, x ) );
}
cpp_int lkp;
for ( int v = 1; v < b; ++v )
{
//cpp_int gb = pow(g, b);
lkp = powm( g, v*b, p );
//looking for a match for g^b^x0 in map mp; when found we need to find x
//which is x1 and then calc 'x'
boost::unordered::unordered_map<cpp_int, cpp_int, hash_str<cpp_int>>::iterator got = mp.find( lkp );
// Check if iterator points to end of map or if we found our value
if ( got != mp.end() )
{
std::cout << "Element Found - ";
//std::cout << got->first << "::" << got->second << std::endl;
}
/*else
{
std::cout << "Element Not Found" << std::endl;
}*/
}
return 0;
}
Just in case, here is the exception I get:
Unhandled exception at 0x768F2F71 in MiM.exe: Microsoft C++ exception: boost::exception_detail::clone_impl > at memory location 0x0109EF5C.
The hash function is pretty atrocious because it allocates a temporary string only to hash it. The string will have log(bits)/log(10) bytes of length.
The point of the hash is that it's a relatively fast way to compare numbers. With a hash that expensive, you're better of with a regular Tree container (std::map<> e.g.).
I haven't checked your formulas (especially around h/g^x1 because I'm not even sure that x represents x1). Outside of that issue,
I think there is a correctness issue with v * b overflowing the int capacity at least if you're on a 32-bit integer compiler.
I've cleaned up a little bit and it runs
#include <boost/math/special_functions/pow.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/unordered_map.hpp>
#include <chrono>
namespace bmp = boost::multiprecision;
using namespace std::chrono_literals;
using Clock = std::chrono::high_resolution_clock;
template <typename T> struct hash_str {
size_t operator()(const T &t) const { return std::hash<std::string>()(t.str()); }
};
template <typename T> struct hash_bin {
size_t operator()(const T &t) const {
return boost::hash_range(t.backend().limbs(), t.backend().limbs()+t.backend().size());
}
};
int main() {
using bmp::cpp_int;
boost::unordered_map<cpp_int, cpp_int, hash_bin<cpp_int> > mp;
#if 1
cpp_int const h("32394751040504504435652643787280657886490975209524495278347924529719819761432925580738569379585531805328"
"78928001494706097394108577585732452307673444020333");
cpp_int const g("11717829880366207009516117596335367088558084999998952205599979459063929499736583746670572176471460312928"
"594829675428279466566527115212748467589894601965568");
cpp_int const p("13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690"
"031858186486050853753882811946569946433649006084171");
#else
cpp_int const g = 1010343267;
cpp_int const h = 857348958;
cpp_int const p = 1073676287;
#endif
int constexpr b = 1 << 20;
cpp_int const inv = powm(g, p - 2, p);
{
auto s = Clock::now();
// building a hash table of all values h/g^x1
for (cpp_int x = 1; x < b; ++x) {
// go through [1, b), calculate the function h/g^x1,
// then hash it to put into table
cpp_int denom = powm(inv, x, p);
cpp_int k = (h * denom) % p;
mp.emplace(std::move(k), x);
}
std::cout << "Built map in " << (Clock::now() - s)/1.0s << "s\n";
}
{
auto s = Clock::now();
for (cpp_int v = 1; v < b; ++v) {
//std::cout << "v=" << v << " b=" << b << "\n";
// cpp_int gb = pow(g, b);
cpp_int const lkp = powm(g, v * b, p);
// looking for a match for g^b^x0 in map mp; when found we need to find x
// which is x1 and then calc 'x'
auto got = mp.find(lkp);
// Check if iterator points to end of map or if we found our value
if (got != mp.end()) {
std::cout << "Element Found - ";
//std::cout << got->first << " :: " << got->second << "\n";
}
}
std::cout << "Completed queries in " << (Clock::now() - s)/1.0s << "s\n";
}
}
It runs in 1m4s for me.
Built map in 24.3809s
Element Found - Completed queries in 39.2463s
...
Using hash_str instead of hash_bin takes 1m13s:
Built map in 30.3923s
Element Found - Completed queries in 42.488s

for loop doesn't execute

I've written a naive (only accepts integer exponents) power function for complex numbers (a home made class) using a simple for loop that multiplies the result for the original number n times:
C pow(C c, int e) {
C res = 1;
for (int i = 0; i==abs(e); ++i) res=res*c;
return e > 0 ? res : static_cast<C>(1/res);
}
When I try to execute this, e.g.
C c(1,2);
cout << pow(c,3) << endl;
I always get 1, because the for loop doesn't execute (I checked).
Here's the full code:
#include <cmath>
#include <stdexcept>
#include <iostream>
using namespace std;
struct C {
// a + bi in C forall a, b in R
double a;
double b;
C() = default;
C(double f, double i=0): a(f), b(i) {}
C operator+(C c) {return C(a+c.a,b+c.b);}
C operator-(C c) {return C(a-c.a,b-c.b);}
C operator*(C c) {return C(a*c.a-b*c.b,a*c.b+c.a*b);}
C operator/(C c) {return C((a*c.a+b*c.b)/(pow(c.a,2)+pow(c.b,2)),(b*c.a - a*c.b)/(pow(c.a,2)+pow(c.b,2)));}
operator double(){ if(b == 0)
return double(a);
else
throw invalid_argument(
"can't convert a complex number with an imaginary part to a double");}
};
C pow(C c, int e) {
C res = 1;
for (int i = 0; i==abs(e); ++i) {
res=res*c;
// check wether the loop executes
cout << res << endl;}
return e > 0 ? res : static_cast<C>(1/res);
}
ostream &operator<<(ostream &o, C c) { return c.b ? cout << c.a << " + " << c.b << "i " : cout << c.a;}
int main() {
C c(1,2), d(-1,3), a;
cout << c << "^3 = " << pow(c,3) << endl;}
What you wrote will read as follows:
for (int i = 0; i == abs(e); ++i)
initialize i with 0 and while i is equal to the absolute value of e (i.e. 3 at the beginning of the function call), do something
It should rather be
for (int i = 0; i < abs(e); ++i)
Tip: the code will throw at the first iteration due to the double conversion operator (and caused by a*c.b + c.a*b), but this is another issue: fix your complex (i.e. with imaginary part) printing function or implement a pretty printing method or such.
you should be using i != abs(e) or i < abs(e) as for loop condition. Currently you are using i == abs(e) which will fail in first try because:
i = 0
abs(e) = 3
so 0 == 3 is false and hence for loop will not execute.

Box2D vector () operator

I was reading the Box2D source code. In b2Vec2 there is the () operator being overloaded, but I did not understand what it is supposed to do. I read the manual and the reference of this method but still did not get what it means to Read from an indexed element and write to an indexed element, and both methods have the same body return (&x)[i]. What does this mean and do?
Thanks to a previous comment (but it was removed for some reason), I got an idea and tested it out, and it turns out this will allow me to access and write to x and y using indices 0 and 1 respectively.
For example:
#include <iostream>
using namespace std;
class clazz {
public:
float x, y;
clazz(float x_, float y_) : x(x_), y(y_) {}
float operator () (int i) const {
return (&x)[i];
}
float& operator () (int i) {
return (&x)[i];
}
};
int main() {
clazz f (3, 4);
cout << "f: x = " << f(0) << " y = " << f(1) << endl; // printed => f: x = 3 y = 4
f(0) = 6;
f(1) = 6;
cout << "f: x = " << f(0) << " y = " << f(1) << endl; // printed => f: x = 6 y = 6
return 0;
}
As you found out it's an accessor function to the individual elements in the vector class. The reason there are two functions is due to const functions need access to the value of the element without needing to modify it. Note that you could return a const reference here as well but this is not necessary in your case since it is operating on a float.
Hopefully there are asserts in place for making sure that code isn't indexing out of the range since that is quite easy to do, especially when you have are using a signed variable like in your example.