473,395 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Verctor/List what is best for my design?

Hi,

I know this is slightly OT but before i can get to the programming part i
want to make sure that i have the structure covered.

I have a 2D map and items in my list/vector will be lines on the map, with a
start and finish point within the map, so an item would have a start pos
S(x,y) and an end pos E(x,y).

so my structure would be something like

struct _POS{
int x;
int y;
};

struct _ITEMLOC{
_POS pos_start;
_POS pos_end;
};

So in effect it would be a straight line going from one point to another.

If i have a vector with hundreds of items i want to retrieve all the items
that fall within a certain square.

something like Search( top, left, bottom, right). And that would return all
the items whose line are within that square, (either partially or totally).

Does anybody know how i could achieve that?

Many thanks in advance.
Sims
Jul 22 '05 #1
14 1607

"Sims" <si*********@hotmail.com> skrev i en meddelelse
news:bu************@ID-162430.news.uni-berlin.de...
Hi,

I know this is slightly OT but before i can get to the programming part i
want to make sure that i have the structure covered.

I have a 2D map and items in my list/vector will be lines on the map, with a start and finish point within the map, so an item would have a start pos
S(x,y) and an end pos E(x,y).

so my structure would be something like

struct _POS{
This is a nonportable name. Use a better name.
int x;
int y;
};

struct _ITEMLOC{
And so is this. The reason: all identifiers containing two consecutive
underscores or beginning with one underscore and an uppercase letter are
reserved for the implementation (the compiler).
_POS pos_start;
_POS pos_end;
};

So in effect it would be a straight line going from one point to another.

If i have a vector with hundreds of items i want to retrieve all the items
that fall within a certain square.

something like Search( top, left, bottom, right). And that would return all the items whose line are within that square, (either partially or totally).
Does anybody know how i could achieve that?

Many thanks in advance.
Sims


There really is no big difference between a list and a vector here, but the
vector would be faster in this case. You just have to look at the entiere
solution to determine what operations are needed. But if you stick to
std::containers and iterators, it should not be a problem to replace the
container and try both alternatives.

Kind regards
Peter
Jul 22 '05 #2

"Peter Koch Larsen" <pk*@mailme.dk> wrote in message
news:40*********************@dread14.news.tele.dk. ..
[SNIP]

There really is no big difference between a list and a vector here, but

the vector would be faster in this case.


IMHO this depends very much on what the user does more often - insert/delete
or search for elements. However, you're of course right that both containers
are easily exchangeable. Thus this discussion might not really lead to
something unless it is tested for the actual case.

[SNIP]

Chris
Jul 22 '05 #3

"Sims" <si*********@hotmail.com> wrote in message
news:bu************@ID-162430.news.uni-berlin.de...
Hi,
[SNI] something like Search( top, left, bottom, right). And that would return all the items whose line are within that square, (either partially or totally).


In your case you might also consider the use of a set<> container, which
usually is implemented in terms of a balanced binary tree. Supplying an
appropriate sorting criterion might reduce your search times in comparison
to a vector or a list.

Regards
Chris
Jul 22 '05 #4


In your case you might also consider the use of a set<> container, which
usually is implemented in terms of a balanced binary tree. Supplying an
appropriate sorting criterion might reduce your search times in comparison
to a vector or a list.

Regards
Chris


Thanks for the replies,
The elements are created once, there is no delete or insert afterward.

How would i do a search for a two dimensional array?

I know how to do a search in a list that is sorted, (in alphabetical order
for example), but how would search for items that are start/end points given
a square?

Also i will have in the region of 200 000 items does that make a dif when it
comes to list/vector?

Sims
Jul 22 '05 #5

"Sims" <si*********@hotmail.com> wrote in message
news:bu************@ID-162430.news.uni-berlin.de...

In your case you might also consider the use of a set<> container, which
usually is implemented in terms of a balanced binary tree. Supplying an
appropriate sorting criterion might reduce your search times in comparison to a vector or a list.

Regards
Chris


Thanks for the replies,
The elements are created once, there is no delete or insert afterward.

How would i do a search for a two dimensional array?


Why would you want to use a 2D array? I'd propose for example a collection
of collections (e.g. vector of vectors) for this.

I know how to do a search in a list that is sorted, (in alphabetical order
for example), but how would search for items that are start/end points given a square?
The search algorithms of the standard library accept a user defined unary
predicate function that can be used to supply your search criterium.
Furthermore you could pre-sort your collection based on the starting
coordinates for example.

Also i will have in the region of 200 000 items does that make a dif when it comes to list/vector?
That's difficult to say. In general vectors are fast when inserting or
deleting at the end of the collections, whereas lists are better if
inserting/deletion is required anywhere. On the other hand vectors provide
random access which is not supplied by the list class. Furthermore (in
comparison to sets for example) vectors are considered to be "slow" for
searching and lists are considered "very slow" with respect to this
requirement. As not many insertion/deletion statements will be needed but
searching is required quite often I'd suggest thinking about using a set<>.

Sims


Chris

Jul 22 '05 #6
In article <bu************@ID-162430.news.uni-berlin.de>,
si*********@hotmail.com says...
Hi,

I know this is slightly OT but before i can get to the programming part i
want to make sure that i have the structure covered.

I have a 2D map and items in my list/vector will be lines on the map, with a
start and finish point within the map, so an item would have a start pos
S(x,y) and an end pos E(x,y).
[ ... ]
So in effect it would be a straight line going from one point to another.

If i have a vector with hundreds of items i want to retrieve all the items
that fall within a certain square.


IMO, the representation you've chosen is probably less than optimal for
the job at hand, at least assuming you're doing searches relatively
frequently.

For frequent searches, I'd instead represent the points in polar
coordinates, and convert the corners of the rectangle to polar
coordinates as well.

What this will do is let you define an area roughly similar to a slice
of a donut (with a size and position defined by your square), and _very_
quickly eliminate the vast majority of lines that don't fall within that
area. Once you've done that, you can check the remaining lines to see
whether they really intersect your square, or just happen to be close to
it. This test is substantially more expensive, so eliminating it most
of the time will probably be a real win.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #7
In article <bu************@ID-162430.news.uni-berlin.de>,
si*********@hotmail.com says...

[ ... ]
Thanks for the replies,
The elements are created once, there is no delete or insert afterward.


In that case, you almost certainly want to use a vector rather than a
set or map -- the latter offer logarithmic time on insertions and
deletions, but in exchange for this, they normally lose some searching
speed (at least on most processors).

The reason is simple: they're normally implemented as trees, with each
node individually allocated, so they have poor locality of reference. A
vector is always contiguous, so it generally has substantially better
locality of reference, making it much more cache-friendly.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #8
Jerry Coffin wrote:

In article <bu************@ID-162430.news.uni-berlin.de>,
si*********@hotmail.com says...
Hi,

I know this is slightly OT but before i can get to the programming part i
want to make sure that i have the structure covered.

I have a 2D map and items in my list/vector will be lines on the map, with a
start and finish point within the map, so an item would have a start pos
S(x,y) and an end pos E(x,y).


[ ... ]
So in effect it would be a straight line going from one point to another.

If i have a vector with hundreds of items i want to retrieve all the items
that fall within a certain square.


IMO, the representation you've chosen is probably less than optimal for
the job at hand, at least assuming you're doing searches relatively
frequently.

For frequent searches, I'd instead represent the points in polar
coordinates, and convert the corners of the rectangle to polar
coordinates as well.

What this will do is let you define an area roughly similar to a slice
of a donut (with a size and position defined by your square), and _very_
quickly eliminate the vast majority of lines that don't fall within that
area.


Never heard of that idea.
But ... to speed up the testing (using cartesian coordinates), simple techniques
have been developed in the 60's. The OP should consult any literature on
computer graphics and look up the section which talkes about window clipping
using 'end point codes' (Cohen-Sutherland clipping).

Or google:

http://www.google.com
"window clipping end point code"

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #9
Jerry Coffin wrote:

In article <bu************@ID-162430.news.uni-berlin.de>,
si*********@hotmail.com says...

[ ... ]
Thanks for the replies,
The elements are created once, there is no delete or insert afterward.


In that case, you almost certainly want to use a vector rather than a
set or map -- the latter offer logarithmic time on insertions and
deletions, but in exchange for this, they normally lose some searching
speed (at least on most processors).


But still sorting the squares eg. on the left x-coordinate can speed
up the process.
The reason is simple: Walking linearly through the vector and testing
the primitiv against it, once you found a square, which has a left
x-coordinate greater then the maximum x coordinate of the primitive
(lies entirely to the right of the primtive) you know that all other
squares will also be completely right of the primtive and thus the
test can be terminated early.

But as always: profile, if it makes a difference. Especially with
computer graphics it's not always obvious where the time is spent.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #10

"Karl Heinz Buchegger" <kb******@gascad.at> skrev i en meddelelse
news:40***************@gascad.at...
Jerry Coffin wrote:

In article <bu************@ID-162430.news.uni-berlin.de>,
si*********@hotmail.com says...

[ ... ]
Thanks for the replies,
The elements are created once, there is no delete or insert afterward.


In that case, you almost certainly want to use a vector rather than a
set or map -- the latter offer logarithmic time on insertions and
deletions, but in exchange for this, they normally lose some searching
speed (at least on most processors).


But still sorting the squares eg. on the left x-coordinate can speed
up the process.
The reason is simple: Walking linearly through the vector and testing
the primitiv against it, once you found a square, which has a left
x-coordinate greater then the maximum x coordinate of the primitive
(lies entirely to the right of the primtive) you know that all other
squares will also be completely right of the primtive and thus the
test can be terminated early.

But as always: profile, if it makes a difference. Especially with
computer graphics it's not always obvious where the time is spent.

--
Karl Heinz Buchegger
kb******@gascad.at


The actual problem - as I understood it - was to draw all lines found in
some square. In that case, sorting would be almost useless so far as i can
see.

Kind regards
Peter
Jul 22 '05 #11
Peter Koch Larsen wrote:


The actual problem - as I understood it - was to draw all lines found in
some square. In that case, sorting would be almost useless so far as i can
see.


Aehm. Somehow I turned this around to read: some lines against
lots of squares :-)

Anyway:
Sort the lines by their minumum x coordinate. You can stop
testing the lines as soon as you find one which has a minumum
x coordinate which is bigger then the maximum x coordinate of
the square.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #12

"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:MP************************@news.clspco.adelph ia.net...
[SNIP]
For frequent searches, I'd instead represent the points in polar
coordinates, and convert the corners of the rectangle to polar
coordinates as well.


If I understand you correctly you try to eliminate the vast majority of
candidates by testing whether they fall into that "slice". In case they do
you perform the actual test whether the point is within the square. In
principle this means that you perform a projection onto the angular
coordinate to perform the coarse test. However, I don't really understand
the advantage resulting from the conversion to polar coordinates, as such
projection tests (based on cartesian coordinates) are a common technique in
collision detection. The number of minum comparisons is 1 and the maximum
number of comparisons is 2. IMHO it's just the same with the polar
coordinates, but probably I'm missing something. I'd really appreciate if
you could point out whether I'm overlooking some crucial point.

Regards
Chris
Jul 22 '05 #13

"Karl Heinz Buchegger" <kb******@gascad.at> skrev i en meddelelse
news:40***************@gascad.at...
Peter Koch Larsen wrote:


The actual problem - as I understood it - was to draw all lines found in
some square. In that case, sorting would be almost useless so far as i can see.


Aehm. Somehow I turned this around to read: some lines against
lots of squares :-)

Anyway:
Sort the lines by their minumum x coordinate. You can stop
testing the lines as soon as you find one which has a minumum
x coordinate which is bigger then the maximum x coordinate of
the square.
--
Karl Heinz Buchegger
kb******@gascad.at


Yes. This will help the filtering somewhat, but if it pays of is another
question. My gut feeling is that sorting and searching will not help much
unless it is quite accurate and that a simple sorting on max x-coordinate
will be close to useless. There are no doubt better ways to do it but then
we should be in comp.programming or perhaps comp.database.theory (my
subconsiousness says Hilbert-curves could be interesting here).

Kind regards
Peter
Jul 22 '05 #14
Peter Koch Larsen wrote:


Yes. This will help the filtering somewhat, but if it pays of is another
question. My gut feeling is that sorting and searching will not help much
unless it is quite accurate and that a simple sorting on max x-coordinate
will be close to useless.
That depends on how often the whole process is done, how often the
lines are updated etc. It can make a difference but often it is just
the case that the sort process takes longer then testing a few thousend
lines against a box (which is quite simple to do, especially if the lines
can be reoriented to always point eg. to the right and up).
That's why I said in a previous post: Try it and see if it makes a
difference.
There are no doubt better ways to do it but then
we should be in comp.programming or perhaps comp.database.theory (my
subconsiousness says Hilbert-curves could be interesting here).


comp.graphics.algorithms

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #15

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

Similar topics

5
by: Darryl B | last post by:
I can not get anywhere on this project I'm tryin to do. I'm not expecting any major help with this but any would be appreciated. The assignment is attached. The problem I'm having is trying to set...
1
by: Glen Able | last post by:
Hi, I have a collection of lists, something like this: std::list<Obj*> lists; Now I add an Obj to one of the lists: Obj* gary; lists.push_front(gary);
1
by: msnews.microsoft.com | last post by:
I'd like to hear your thoughts on best methods for populating drop down list controls. I have states and countries drop down lists that don't change often, so naturally I "hard code" them in the...
7
by: David Freeman | last post by:
Hi There! I'm trying to create a User Registration page in ASP.NET and wondering what is the best way to get the list of up-to-date Countries and Cities? Are there any Web Services on the web...
0
by: Andrew | last post by:
I have created a Component called a BOConnector that implements IBindingList so it can provide access to a list of business objects to be bound to controls. At design time there is no live list...
17
by: 2005 | last post by:
Hi In C++, are the following considered best practices or not? - passing aguments to functions (ie functions do not take any arguments ) - returning values using return statement Anything...
22
by: joshc | last post by:
In an interview for an embedded software position recently I was asked to write code, in C, for printing the contents of a linked list backwards. After a few minutes I came up with the recursive...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
0
by: Paul Hadfield | last post by:
I'm looking for thoughts on the "correct" design for this problem (DotNet 2.0 - in winforms, but that's not so important). I've got two combo boxes (combo1 and combo2), both are populating via...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.