473,761 Members | 5,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1630

"Sims" <si*********@ho tmail.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.d k> wrote in message
news:40******** *************@d read14.news.tel e.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*********@ho tmail.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*********@ho tmail.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*********@hot mail.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*********@hot mail.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*********@hot mail.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*********@hot mail.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

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

Similar topics

5
2328
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 up the class link and tel_list. I set up a class person with strings for name, town, and number. I just don't know how to set up the classes w/ their methods (constructors and operations). I'm stuck on the assignment operator and the add and...
1
3062
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
2488
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 aspx page. But the problem is these tend to really slow the development -- it takes up to 15 seconds for the page to come up in VS.NET design environment, so I'm thinking about taking these out and populating the controls dynamically using the...
7
3806
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 that I can use to retrieve such information? If not, what are the options? Please, any suggestions and pointers will be very much appreciated! Dave
0
1204
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 of objects so BOConnector creates an internal list of one object. It needs one object because when you bind to a grid etc at design time it needs to get a list of properties to make the columns. My BOConnector works just fine with grids and...
17
3044
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 else? The reason for this question is that I had an assignment in which I was
22
8045
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 solution. Being that recursion is frowned upon in embedded software, the answer was not what the interviewer expected, but alas it was correct. I asked some friends how they would have answered and another answer is to reverse the list and then...
9
7907
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 List<as a property. Consider the example below, the class "ListTest" contains a private "List<>" called "strings" - it also provides a public method to add to that list,
0
3697
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 database calls (using a separate DB handler class). "combo1" contains a list of countries and is fairly static, it can be added to but no other external events cause a change in its population. "combo2" however is populated with a list of options...
0
9538
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10123
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9909
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9788
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8794
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5241
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3889
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3481
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2765
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.