473,548 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constant time search in list.

Hi,
I am not sure if this belongs to this group. Anyway, my question is as
follows: I have a list (STL list) whose elements are pairs of integers (STL
pairs, say objects of class T). When I create a new object of class T, I
would like to check if this object already exists in the list: meaning one
having same integers. This can be done in linear time in a list, and
probably faster if I use STL Set instead of list. I am wondering however if
its possible to do it in constant time, and use list the the same time. I
read something about using lookup function on a hash, but I don't want to go
away from using a list.
Arranging the elements of the list is not important to me, hence a Set is
not exactly what I am looking for.

Thanks,
Amit.

Feb 21 '07 #1
6 3988
* Amit Bhatia:
Hi,
I am not sure if this belongs to this group. Anyway, my question is as
follows: I have a list (STL list) whose elements are pairs of integers (STL
pairs, say objects of class T). When I create a new object of class T, I
would like to check if this object already exists in the list: meaning one
having same integers. This can be done in linear time in a list, and
probably faster if I use STL Set instead of list. I am wondering however if
its possible to do it in constant time, and use list the the same time. I
read something about using lookup function on a hash, but I don't want to go
away from using a list.
Arranging the elements of the list is not important to me, hence a Set is
not exactly what I am looking for.
You can use a hash in addition to the list; package both in some wrapper
object that enforces the policy of updating the hash when adding or
removing an element. You can also use a std::map, if as you indicate
they keys are unique, but it depends on what functionality of lists you
are using. Also, it won't buy you constant time, but logarithmic time,
which is often in practice good enough.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 21 '07 #2
* Amit Bhatia:
>Hi,
I am not sure if this belongs to this group. Anyway, my question is as
follows: I have a list (STL list) whose elements are pairs of integers (STL
pairs, say objects of class T). When I create a new object of class T, I
would like to check if this object already exists in the list: meaning one
having same integers. This can be done in linear time in a list, and
probably faster if I use STL Set instead of list. I am wondering however if
its possible to do it in constant time, and use list the the same time. I
read something about using lookup function on a hash, but I don't want to go
away from using a list.
Arranging the elements of the list is not important to me, hence a Set is
not exactly what I am looking for.

You can use a hash in addition to the list; package both in some wrapper
object that enforces the policy of updating the hash when adding or
removing an element. You can also use a std::map, if as you indicate
they keys are unique, but it depends on what functionality of lists you
are using. Also, it won't buy you constant time, but logarithmic time,
which is often in practice good enough.
Thanks. I am using constant time removal and insertion of elements in
between mainly. Could you please explain in a slightly more detail how to go
about using the hash in addition to a list (I have never used a hash
before)?

As of now, I am using the following:

Class A{

//;
Vector<List<Cla ss B tree;
//;;
};

If class B is not an STL pair, but each object can still be uniquely
identified by 2 integers, then I don't think that changes much ?

Thanks,
Amit.

Feb 21 '07 #3
On Feb 21, 10:23 pm, Amit Bhatia <amit.bhatia.no s...@nospam.com >
wrote:
* Amit Bhatia:
Hi,
I am not sure if this belongs to this group. Anyway, my question is as
follows: I have a list (STL list) whose elements are pairs of integers(STL
pairs, say objects of class T). When I create a new object of class T,I
would like to check if this object already exists in the list: meaningone
having same integers. This can be done in linear time in a list, and
probably faster if I use STL Set instead of list. I am wondering however if
its possible to do it in constant time, and use list the the same time.. I
read something about using lookup function on a hash, but I don't wantto go
away from using a list.
Arranging the elements of the list is not important to me, hence a Set is
not exactly what I am looking for.
You can use a hash in addition to the list; package both in some wrapper
object that enforces the policy of updating the hash when adding or
removing an element. You can also use a std::map, if as you indicate
they keys are unique, but it depends on what functionality of lists you
are using. Also, it won't buy you constant time, but logarithmic time,
which is often in practice good enough.

Thanks. I am using constant time removal and insertion of elements in
between mainly. Could you please explain in a slightly more detail how togo
about using the hash in addition to a list (I have never used a hash
before)?
I think Alf meant that you could create a class that contains both a
hashmap and a list, and when you insert an element in your class you
insert it both in your list and the hashmap. This gives you constant
insertion time when the element is not already stored, but linear time
if it is or when deleting (since you then have to search the list).

A hashmap is a datastructure on its own, just like a list or map, in C+
+ you need to have a fairly new standard library which is updated to
TR1 or use a third party implementation. The one in TR1 is called
unordered_map and works just like a map, you can insert items using
the []operator. The difference from a map is that inserting and
finding elements in general is faster (though I think worst case is
linear).
As of now, I am using the following:

Class A{

//;
Vector<List<Cla ss B tree;
//;;

};

If class B is not an STL pair, but each object can still be uniquely
identified by 2 integers, then I don't think that changes much ?
Sorry, but I can't see the reason to use such a construct, what is the
vector good for if you store the pair in the list? Do you use the
first integer to index into the vector and then store the pair in the
list? If you do you only have to store the second integer in the list,
since the first was used to index.

--
Erik Wikström

Feb 22 '07 #4
>>* Amit Bhatia:
>>>Hi,
I am not sure if this belongs to this group. Anyway, my question is as
follows: I have a list (STL list) whose elements are pairs of integers (STL
pairs, say objects of class T). When I create a new object of class T, I
would like to check if this object already exists in the list: meaning one
having same integers. This can be done in linear time in a list, and
probably faster if I use STL Set instead of list. I am wondering however if
its possible to do it in constant time, and use list the the same time. I
>>>read something about using lookup function on a hash, but I don't want to
go
away from using a list.
Arranging the elements of the list is not important to me, hence a Set is
not exactly what I am looking for.
>>You can use a hash in addition to the list; package both in some wrapper
object that enforces the policy of updating the hash when adding or
removing an element. You can also use a std::map, if as you indicate
they keys are unique, but it depends on what functionality of lists you
are using. Also, it won't buy you constant time, but logarithmic time,
which is often in practice good enough.

Thanks. I am using constant time removal and insertion of elements in
between mainly. Could you please explain in a slightly more detail how to go
about using the hash in addition to a list (I have never used a hash
before)?

I think Alf meant that you could create a class that contains both a
hashmap and a list, and when you insert an element in your class you
insert it both in your list and the hashmap. This gives you constant
insertion time when the element is not already stored, but linear time
if it is or when deleting (since you then have to search the list).

A hashmap is a datastructure on its own, just like a list or map, in C+
+ you need to have a fairly new standard library which is updated to
TR1 or use a third party implementation. The one in TR1 is called
unordered_map and works just like a map, you can insert items using
the []operator. The difference from a map is that inserting and
finding elements in general is faster (though I think worst case is
linear).
>As of now, I am using the following:

Class A{

//;
Vector<List<Cl ass B tree;
//;;

};

If class B is not an STL pair, but each object can still be uniquely
identified by 2 integers, then I don't think that changes much ?

Sorry, but I can't see the reason to use such a construct, what is the
vector good for if you store the pair in the list? Do you use the
first integer to index into the vector and then store the pair in the
list? If you do you only have to store the second integer in the list,
since the first was used to index.
I am using a vector since I am organizing objects of class B according to
certain criteria in different lists. So the index of the vector has nothing
to do with the pair of integers that uniquely identify objects of class B.
I think I am using g++ version 3.3 or so. So from what I understand,
hash_map still does not invalidate iterators pointing to objects upon
deletion and insertion, but does not allow insertion or deletion in constant
time? does the hash_map also has the ability to return iterator in case one
instance of the object is found (in constant time)?

Thanks,
Amit.

Feb 22 '07 #5
On Feb 22, 3:57 pm, Amit Bhatia <amit.bhatia.no s...@nospam.com wrote:
>* Amit Bhatia:
Hi,
I am not sure if this belongs to this group. Anyway, my question isas
follows: I have a list (STL list) whose elements are pairs of integers (STL
pairs, say objects of class T). When I create a new object of class T, I
would like to check if this object already exists in the list: meaning one
having same integers. This can be done in linear time in a list, and
probably faster if I use STL Set instead of list. I am wondering however if
its possible to do it in constant time, and use list the the same time. I
>>read something about using lookup function on a hash, but I don't want to
go
away from using a list.
Arranging the elements of the list is not important to me, hence a Set is
not exactly what I am looking for.
>You can use a hash in addition to the list; package both in some wrapper
object that enforces the policy of updating the hash when adding or
removing an element. You can also use a std::map, if as you indicate
they keys are unique, but it depends on what functionality of lists you
are using. Also, it won't buy you constant time, but logarithmic time,
which is often in practice good enough.
Thanks. I am using constant time removal and insertion of elements in
between mainly. Could you please explain in a slightly more detail howto go
about using the hash in addition to a list (I have never used a hash
before)?
I think Alf meant that you could create a class that contains both a
hashmap and a list, and when you insert an element in your class you
insert it both in your list and the hashmap. This gives you constant
insertion time when the element is not already stored, but linear time
if it is or when deleting (since you then have to search the list).
A hashmap is a datastructure on its own, just like a list or map, in C+
+ you need to have a fairly new standard library which is updated to
TR1 or use a third party implementation. The one in TR1 is called
unordered_map and works just like a map, you can insert items using
the []operator. The difference from a map is that inserting and
finding elements in general is faster (though I think worst case is
linear).

I think I am using g++ version 3.3 or so. So from what I understand,
hash_map still does not invalidate iterators pointing to objects upon
deletion and insertion, but does not allow insertion or deletion in constant
time? does the hash_map also has the ability to return iterator in case one
instance of the object is found (in constant time)?
Well, hash_map is not part of the standard library so I don't know the
specifics of that implementation. However it would be a poor
implementation of a hashmap is you could not perform insertions/
deletions in constant time (best case). I don't know about
invalidation of iterators but it's possible to make an implementation
where only iterators to the element removed are invalidated.

I would suspect that the interface of a hashmap is much the same as
that of map, with the exception that finding, inserting and deleting
elements are constant time operations, so finding an element and
getting an iterator to it in constant time should be possible.

--
Erik Wikström

Feb 23 '07 #6
On Feb 22, 3:57 pm, Amit Bhatia <amit.bhatia.no s...@nospam.com wrote:
[snip]
I think I am using g++ version 3.3 or so. So from what I understand,
hash_map still does not invalidate iterators pointing to objects upon
deletion and insertion, but does not allow insertion or deletion in constant
time?
Remember that insert/delete of a hashed container is only O(1) in the
normal case. worst case is O(n).
For the other questions, you should refer to your compilers
documentation since hashed containers are only now being accepted into
the standard. I would recommend that you upgrade the compiler as g++
should now have implemented the hash-containers that will be the part
of the next standard. As soon as you ask question about standard
containers, you are much better of in this newsgroup.

/Peter

Feb 23 '07 #7

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

Similar topics

6
2005
by: Jani Yusef | last post by:
I have a HW problem stated as shown at the top of the solution. The thing is is that I am not 100% sure wtf constant memory means. Well, I think I do but I am confused. Does my solution use contant memory in terms of the length of the list i? If so why not and how could I change it to be so? I am sure the solution is O(n) since the list must...
2
2222
by: Tim | last post by:
Please advise if you can. Presumably initialisation of members in member initialisation lists is perfomed by 'C' run-time startup. If the CRT was never started-up would those members be garbage? Which of these fundamental language support features could I expect to be absent (and anything else I might have missed): static data zeroing...
12
2722
by: Kristian Bisgaard Lassen | last post by:
Hi, How do I parameterize a template by a a allocated array of integers, which is declared static and constant, so I can make compile time optimizations depending on the content of the array. The way I have written my code makes g++ complain about it not being static. BTW I am new to the C++ language and also the templates it provides. ...
7
2146
by: Nolan Martin | last post by:
is a static functions address constant? ie.. static void func(); write_to_file(&func); Restart program... static void func(); void (*funcPtr) ();
33
5528
by: desktop | last post by:
In the C++ standard sec 23.1.2 table 69 it says that erase(q) where q is a pointer to an element can be done in amortized constant time. I guess that is not worst case since std::set is practically a red-black tree where insert/delete takes O(lg n) time. Or are there some other explanation for this complexity?
5
1694
by: =?Utf-8?B?UnlhbiBBbmRydXM=?= | last post by:
Is there a way to configure\create a datatable (strongly typed or untyped) that can have a row accessed in constant time if you are searching on the primary key? If there isn't a way, then what is the quickest way to access a row when using datatables? Thanks, Ryan
22
3585
by: Tomás Ó hÉilidhe | last post by:
I've been developing a C89 microcontroller application for a while now and I've been testing its compilation using gcc. I've gotten zero errors and zero warnings with gcc, but now that I've moved over to the micrcontroller compiler I'm getting all sorts of errors. One thing I'd like to clarify is the need (in C89) for a compile- time...
13
1845
by: bobg.hahc | last post by:
running access 2k; And before anything else is said - "Yes, Virginia, I know you can NOT use a variable to set a constant (that's why it's constant)". BUT - my problem is - I want a constant, that I can set from a variable (one time)!!! When my application starts, the user is prompted to make a selection from a list. I want that...
1
2717
by: hackerbob | last post by:
I'm trying to create a constant time event timer. Basically, a routine can set a callback to be called n ms from the current time, and the main event loop will wait until the delta between the current time and the earliest event timer has elapsed. When the list is sorted, checking for expiration is O(n) time where n is the number of timers...
0
7444
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...
0
7711
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. ...
0
7954
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7467
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...
0
7805
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...
0
3478
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1932
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
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
755
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...

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.