472,952 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,952 software developers and data experts.

upper_bound

In frustration I made this code using equal_range.
It does not behave in the way I expected however.
It finds the correct breakpoint in the set but finds it as
the first iterator. why?

I would like to do this with upper_bound instead.
//-----
#include <stdlib.h>
#include <math.h>
#include <set>
#include <iostream>
#include <algorithm>
#define random(A) (rand()%A)
class ObjectIn2DSpace
{
private:
double xpos;
double ypos;
static std::set<ObjectIn2DSpace*> AllObjects;
public:
double GetXPos(){return xpos;}
double GetYPos(){return ypos;}
ObjectIn2DSpace(){
xpos=random(100000)*(1/double(random(999)+1));
ypos=random(100000)*(1/double(random(999)+1));
AllObjects.insert(this);
}
~ObjectIn2DSpace(){
AllObjects.erase(this);
}
static void ClearAll()
{
while(!AllObjects.empty())
delete *(AllObjects.begin());
AllObjects.clear();
}
static std::set<ObjectIn2DSpace*>::iterator GetBegin(){return
AllObjects.begin();}
static std::set<ObjectIn2DSpace*>::iterator GetEnd(){return
AllObjects.end();}
static bool IsEnd(std::set<ObjectIn2DSpace*>::iterator it){return
it==AllObjects.end();}
};
std::set<ObjectIn2DSpace*> ObjectIn2DSpace::AllObjects;

class CompDist
{
ObjectIn2DSpace * CenterOn;
double MaxDistance;
public:
double DistanceTo(ObjectIn2DSpace* A)
{
double dx= CenterOn->GetXPos()-A->GetXPos();
double dy= CenterOn->GetYPos()-A->GetYPos();
return sqrt((dx*dx)+(dy*dy));
}

CompDist(){MaxDistance=-1.0;}
void SetCenterOn(ObjectIn2DSpace * In){CenterOn=In;}
ObjectIn2DSpace * GetCenterOn() {return CenterOn;}
void SetMaxDistance(double In){MaxDistance=In;}
bool operator()(ObjectIn2DSpace* A,ObjectIn2DSpace* B)
{
if(MaxDistance>=0)
{
double AD=std::min(DistanceTo(A),MaxDistance);
double BD=std::min(DistanceTo(B),MaxDistance);
if((AD==MaxDistance)||(BD==MaxDistance))
return false;
return true;
}
return DistanceTo(A)<DistanceTo(B);
}
};
template<class c>
void DumpDistances(c &Sorted,CompDist & Comp)
{
c::iterator it=Sorted.begin();
std::string s;
char itoabuf[15];
while(it!=Sorted.end())
{
std::cout<<itoa( int(Comp.DistanceTo(*it)),itoabuf,10);
std::cout<<"\r\n";
it++;
}
std::cout<<"\r\n";
}

int main() {
int n=10000;
srand(time(0));
while(n--)
new ObjectIn2DSpace();
CompDist Comp;
Comp.SetCenterOn(new ObjectIn2DSpace());
std::set<ObjectIn2DSpace*,CompDist>
Sorted(ObjectIn2DSpace::GetBegin(),ObjectIn2DSpace ::GetEnd(),Comp);

double MaxDist=500.0;

Comp.SetMaxDistance(MaxDist);
std::pair<std::set<ObjectIn2DSpace*,CompDist>::ite rator,
std::set<ObjectIn2DSpace*,CompDist>::iterator>
range=std::equal_range(Sorted.begin(),Sorted.end() ,Comp.GetCenterOn(),Comp);

Comp.SetMaxDistance(-1);
std::set<ObjectIn2DSpace*,CompDist>
SortedMax(Sorted.begin(),range.first,Comp);
DumpDistances(SortedMax,Comp);

ObjectIn2DSpace::ClearAll();
}

Feb 15 '06 #1
2 2259

je****@alphacash.se wrote:
In frustration I made this code using equal_range.
It does not behave in the way I expected however.
It finds the correct breakpoint in the set but finds it as
the first iterator. why?


Please post the smallest possible snippet of code that shows your
problem.
For such a function, that should be less than 15 lines, I think.

HTH,
Michiel Salters

Feb 15 '06 #2
I just wanted to post compilable code.
This is my binary predicate:
bool CompDist::operator()(ObjectIn2DSpace* A,ObjectIn2DSpace* B)
{
if(MaxDistance>=0)
{
double AD=std::min(DistanceTo(A),MaxDistance);
double BD=std::min(DistanceTo(B),MaxDistance);
if((AD==MaxDistance)||(BD==MaxDistance))
return false;
return true;
}
return DistanceTo(A)<DistanceTo(B);
}

This is the equal_range usage I'm not satisfied with:
Comp.SetMaxDistance(MaxDist);
std::pair<std::set<ObjectIn2DSpace*,CompDist>::ite rator,
std::set<ObjectIn2DSpace*,CompDist>::iterator>

range=std::equal_range(Sorted.begin(),Sorted.end() ,Comp.GetCenterOn(),Comp)*;
Comp.SetMaxDistance(-1);
std::set<ObjectIn2DSpace*,CompDist>
SortedMax(Sorted.begin(),range.first,Comp);

Feb 16 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Erik Arner | last post by:
Hi, let's say I have a std::map<std::string,int> and I want to search the map for all keys that start with "foo". The regexp equivalent is to search for "foo*", or perhaps "^foo*". At present...
3
by: asdf | last post by:
Can you create std::set::upper_bound function just using std::set::lower_bound ?
3
by: Diego Martins | last post by:
Andrew Koenig wrote:
3
by: could.net | last post by:
I have a class board, like this: struct board { int x1, x2, h; int time1, time2; void calculate_time(); void assign(int x1__,int x2__,int h__); }; And I have n boards stored in an array...
5
by: desktop | last post by:
In section 23.1.2 table 69 upper and lower bound are defined like: a.lower_bound(k) iterator; logarithmic returns an iterator pointing to the first element with key not less than k ...
4
by: dineshchothe | last post by:
I am developing one application in java(swing) font converter. In which I have to upload .doc file .Then if that file contains marathi,english text then I want to change marathi font to unicode.My...
3
by: Henrik Goldman | last post by:
Hello, Assume we have a std::map<time_t, int>. Now I would like to find all values (second) which fits in (within the time range of first) between first of April 1st. and end of July. Notice...
0
by: =?Utf-8?B?QmVybmFyZA==?= | last post by:
I have an xml fragment (doc): <charge_details> <charge> <type>Maintenance Charge</type> <currency>GBP</currency> <percentage>0.35000</percentage> <frequency>Monthly</frequency> <term>31</term>...
1
by: Felix T. | last post by:
I have a class called Interval(type.ObjectType) that is supposed to mimic closed mathematical intervals. Right now, it has a lot of methods like this: def __add__(self,other): if type(other) in...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.