473,786 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL list.size() operation - O(1) or O(n) ?

Hi,

I have had trouble determining whether the STL list.size() operation
is O(1) or O(n). I know the list is a doubly-linked list, so if the
size() operation begins at the head, then counts to the end -
obviously its O(n). However, keeping track of inserts and deletes
isn't that tough, so its conceivable (to me!) that size() could be
O(1). For my application, the lists can be quite long (millions of
elements), so the answer greatly impacts my design decisions.

OR is this compiler dependent? (I am using gcc 3.2.2.)

Thanks, Brett
Jul 19 '05
12 10953
On Sat, 12 Jul 2003 01:00:15 +0000, Howard Hinnant wrote:
[snip]......
I agree with all of it, but:

-----------

All that being said, I'd still love to see:

void splice(iterator position, list& x,
iterator first, iterator last, size_type n);

Precondition: n == distance(first, last)

as an addition to the existing interface. I believe such an addition
would settle this debate once and for all. And then we could make
std::container: :size() O(1), instead of "should be" O(1).


How would the client know n, again, distance would be a O(N)
operation......

It would be better to just stick to the current solution. Actually, there
are many ways to implement this particular splice O(N) for size() == (1).
I'd like to know which one would be the best.
Some that come to mind are:

1. Individually decrement size for the other list, and increment the
current size, while splicing the individual elements (seems like
inefficient).
2. Splice the range into a temporary list, and then the size for that list
is known, so its fine. Again, here, we will have to use a special splice,
which does not do anything to size, so it can be O(1). Then, we subtract,
and add size() of the temporary list to the other list, and *this.
3. Same thing as (2), but in a loop.

Regards,
-Dhruv.


Jul 19 '05 #11
In article <pa************ *************** *@gmx.net>, Dhruv
<dh*******@gmx. net> wrote:

| > -----------
| >
| > All that being said, I'd still love to see:
| >
| > void splice(iterator position, list& x,
| > iterator first, iterator last, size_type n);
| >
| > Precondition: n == distance(first, last)
| >
| > as an addition to the existing interface. I believe such an addition
| > would settle this debate once and for all. And then we could make
| > std::container: :size() O(1), instead of "should be" O(1).
|
| How would the client know n, again, distance would be a O(N)
| operation......

Often the work of defining the range to splice from is O(N) whether you
count the number of elements or not.

Please reread:

In article <11************ ************@me trowerks.com>, Howard Hinnant
<hi*****@metrow erks.com> wrote:

| But in one place I'm splicing in a range. And in that particular
| example, I already know n before I call splice. It is a waste to have
| list::splice recompute it.

In article <pa************ *************** *@gmx.net>, Dhruv
<dh*******@gmx. net> continued:

| It would be better to just stick to the current solution.

In article <11************ ************@me trowerks.com>, Howard Hinnant
<hi*****@metrow erks.com> wrote:

| as an addition to the existing interface.

How does this addition to the current interface make the situation
worse? If you don't know n, just don't supply it. I'm not proposing
to remove any existing signatures. And the addition does not create
any ambiguities or other problems that I can see.

void splice(iterator position, list& x);
void splice(iterator position, list& x, iterator i);
void splice(iterator position, list& x, iterator first, iterator last);
void splice(iterator position, list& x, iterator first, iterator last,
size_type n); // proposed

| Actually, there
| are many ways to implement this particular splice O(N) for size() == (1).
| I'd like to know which one would be the best.
| Some that come to mind are:
|
| 1. Individually decrement size for the other list, and increment the
| current size, while splicing the individual elements (seems like
| inefficient).

Seems inefficient to me too.

| 2. Splice the range into a temporary list, and then the size for that list
| is known, so its fine. Again, here, we will have to use a special splice,
| which does not do anything to size, so it can be O(1). Then, we subtract,
| and add size() of the temporary list to the other list, and *this.

How does the temporary list know the size?

| 3. Same thing as (2), but in a loop.

You've lost me on that one.

The best way I know of to implement

void splice(iterator position, list& x, iterator first, iterator last);

(assuming O(1) size)

is to first check if the range [first, last) is empty and if so, do
nothing. Then if this != &x, compute the distance [first, last) and
subtract that amount from x.size() and add it to this->size(). Then
(regardless if this == &x or not) twiddle the links at first and at
last to disconnect [first, last) from x, and connect it to *this.

No need to visit each node, except for computing distance(first, last).
No need for a temporary list.

In the proposed variant, there is no need to compute distance(first,
last) as it is supplied by the client. Though a debug build might
compute distance(first, last) anyway just to make sure the client knows
what he's talking about.

--
Howard Hinnant
Metrowerks
Jul 19 '05 #12
On Mon, 14 Jul 2003 18:05:58 +0000, Howard Hinnant wrote:
In article <pa************ *************** *@gmx.net>, Dhruv
<dh*******@gmx. net> wrote:

| > -----------
| >
| > All that being said, I'd still love to see:
| >
| > void splice(iterator position, list& x,
| > iterator first, iterator last, size_type n);
| >
| > Precondition: n == distance(first, last)
| >
| > as an addition to the existing interface. I believe such an addition
| > would settle this debate once and for all. And then we could make
| > std::container: :size() O(1), instead of "should be" O(1).
|
| How would the client know n, again, distance would be a O(N)
| operation......

Often the work of defining the range to splice from is O(N) whether you
count the number of elements or not.

Please reread:

In article <11************ ************@me trowerks.com>, Howard Hinnant
<hi*****@metrow erks.com> wrote:

| But in one place I'm splicing in a range. And in that particular
| example, I already know n before I call splice. It is a waste to have
| list::splice recompute it.

In article <pa************ *************** *@gmx.net>, Dhruv
<dh*******@gmx. net> continued:

| It would be better to just stick to the current solution.

In article <11************ ************@me trowerks.com>, Howard Hinnant
<hi*****@metrow erks.com> wrote:

| as an addition to the existing interface.
Oops, missed that part :-)
How does this addition to the current interface make the situation
worse? If you don't know n, just don't supply it. I'm not proposing
to remove any existing signatures. And the addition does not create
any ambiguities or other problems that I can see.

void splice(iterator position, list& x);
void splice(iterator position, list& x, iterator i);
void splice(iterator position, list& x, iterator first, iterator last);
void splice(iterator position, list& x, iterator first, iterator last,
size_type n); // proposed

| Actually, there
| are many ways to implement this particular splice O(N) for size() == (1).
| I'd like to know which one would be the best.
| Some that come to mind are:
|
| 1. Individually decrement size for the other list, and increment the
| current size, while splicing the individual elements (seems like
| inefficient).

Seems inefficient to me too.

| 2. Splice the range into a temporary list, and then the size for that list
| is known, so its fine. Again, here, we will have to use a special splice,
| which does not do anything to size, so it can be O(1). Then, we subtract,
| and add size() of the temporary list to the other list, and *this.

How does the temporary list know the size?
We provide a recalculate size function for the list, which will obviously
be private.
| 3. Same thing as (2), but in a loop.

You've lost me on that one.

I meant what you wrote.

The best way I know of to implement

void splice(iterator position, list& x, iterator first, iterator last);

(assuming O(1) size)

is to first check if the range [first, last) is empty and if so, do
nothing. Then if this != &x, compute the distance [first, last) and
subtract that amount from x.size() and add it to this->size(). Then
(regardless if this == &x or not) twiddle the links at first and at
last to disconnect [first, last) from x, and connect it to *this.

No need to visit each node, except for computing distance(first, last).
No need for a temporary list.

In the proposed variant, there is no need to compute distance(first,
last) as it is supplied by the client. Though a debug build might
compute distance(first, last) anyway just to make sure the client knows
what he's talking about.


Ok, so when the client does know the size, it will be helpful.
I was thinking that it would be highly unprobale that the client does know
the distance between 2 nodes, because at least I haven't encountered any
such situation. Maybe you have? Most of the time, I've been splicing a
single element or, the whole list (ops. for which size is known), and when
I splice in a subrange, then I have no idea of the size. Of cource, if
someone does know the size, then this addition will help then a lot.

Regards,
-Dhruv.


Jul 19 '05 #13

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

Similar topics

5
1328
by: aruna | last post by:
How do I check the size of int on a machine before runtime, at the pre-processor stage? Is it possible?
1
6380
by: abc my vclass | last post by:
How to fix the height size of Panel1 on SplitContainer?
3
4760
by: Henrik | last post by:
I need to get a list of clickable objects on the desktop and their positions. I have written a small program in C# and by using the Win32 function: WindowFromPoint -function I get a handle to the desktop but now I'm stuck. For those who need to see the code -look below IntPtr hWnd = Win32.WindowFromPoint(Cursor.Position); With the handle I can get alot of cool information such as: Caption,
1
2910
by: lecnac | last post by:
Here's some details: Server and workstation both in the same workgroup Logged into server as local Administrator Logged into workstation as a local user that is only in the Users group The local user on the workstation is also defined with same name and password on the server (and only in the Users group on the server) Server is Windows Server 2003 running IIS 6.0 Workstation is Windows XP Professional ASP.NET 2.0 (C#) web site
1
3704
by: lecnac | last post by:
Sorry for the repost. I must have done something wrong when I tried to post my reply (I can't seem to find it). Anyway, I'd really appreciate any help that anyone could provide. My issue is quickly becoming more and more urgent. I've tried the code below using the server's local Administrator user name and password. This gets me past the Access denied error but gives me a User
5
2496
by: cjt22 | last post by:
Hi there I am fairly new to Python and have not really used regular expressions before (I think this might be needed for my query) and wondered if you could help I have a step class and store in a list step instances A step instance contains variables: name, startTime etc and startTime is stored as a string %H:%M:%S
19
5430
by: Juha Nieminen | last post by:
If I'm not completely mistaken, the only reason why std::list::size() may be (and usually is) a linear-time operation is because they want std::list::splice() to be a constant-time operation, and if you execute the latter, the size of the resulting lists cannot be known without explicitly counting the sizes of the new lists. I was thinking: What if size() was an O(n) operation only *after* a splice() operation has been performed (and...
1
3776
by: kurtzky | last post by:
Hello. Good day. I have this problem with my current program right now. I have a separate report created in Seagate Crystal Reports, and I'm integrating it to my program in C#. I'm using CrystalDecisions and calls the ReportDocument class. Under that report, I have many subreports. What I want to do is to select ALL the fields in the MAIN report and put it in the DataTable, then I would put that DataTable as the DataSource of my...
1
4437
by: venkat chitta | last post by:
Hi All, I want to know how to get a list of all indexes created on a column of a table in PostgreSQL. There is a program block in our application which do this in oracle, now i want to write the same piece of code which works for PostgreSQL as well. So, i want the list of indexes on a column of a table if i know the column name at that time or list of all the indexes on all the columns of the given table. Below is the java Code written for...
0
9647
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
9492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10360
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
10108
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,...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5397
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
4064
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
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.