Click here to Skip to main content
Licence CPOL
First Posted 6 Mar 2001
Views 161,498
Downloads 1,105
Bookmarked 35 times

STL containers map, set, and list with fast array access

By | 5 Aug 2002 | Article
Source code for STL compliant container classes that add fast indexing capability to existing container types

Introduction

This source code contains five STL compliant container classes with the following names:

index_list<T>
index_set<T,Cmp>
index_multiset<T,Cmp>
index_map<Key,T,Cmp>
index_multimap<Key,T,Cmp>

Each of these implements the same interface as the STL class in the second part of its name. In addition, each one also implements the STL interface for the vector<T> class, providing fast array access via operator[] and at(). T is the type of the contained class. Key is the type of the lookup key for pair-associative containers. Cmp is the comparison predicate and must be a binary predicate with arguments of type T for set and type Key for map. If you do not provide a comparison predicate, it will default to std::less<Arg>.

The classes are all derived from an AVL tree that I borrowed form Adreas Jager (copyright included) and modified to allow for the indexed access. Each tree node contains an extra long integer that holds the number of child nodes below it. The algorithm to update these counts does not add to the complexity of any of the original tree algorithms. Since the interfaces are documented in the STL, I will simply provide the complexity analysis below. I believe I have covered the most-used functions of these containers, but some functions may not be included. Please contact me if you wish to add another STL function and I will be glad to update the source.

Complexity Analysis

index_list<T>

An index_list is a sequence which combines the interfaces of array, deque, and list.

Here are the complexity comparisons with other sequence containers:

array deque list index_list
Indexed access 1 1 (N) lgN
Insert/delete N N 1 1+
Front insertion(N) 1+ 1 1+
Back insertion 1+ 1+ 1 1+

It tends to work best for large sets (N >> 1).

It also has the extra function insert(const T&), usually used for sets, which chooses an insert position that keeps the tree balanced with a minimal number of rotation operations.

index_set<T,Cmp>
index_multiset<T,Cmp>
index_map<Key,T,Cmp>
index_multmap<Key,T,Cmp>

These classes implement the STL interfaces for set, multiset, map, and multimap respectively, along with the indexed access (operator[], at()). Here are the complexity comparisons with other associative containers:

arraylistsetindex_set/map etc.
Indexed access 1 (N) (N+) lgN
Insert/delete N 1 1+ 1+
Search lgNN lgN lgN

Enjoy!

History

21 July 2002 - updated downloads

6 Aug 2002 - updated downloads

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ray Virzi



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberDemonS770:13 28 Oct '10  
Questionis this still supported Pinmembermaciekboni5:37 20 Jun '09  
AnswerRe: is this still supported PinmemberRay Virzi8:18 20 Jun '09  
GeneralRe: is this still supported Pinmembermaciekboni18:54 30 Jun '09  
GeneralRe: is this still supported PinmemberRay Virzi19:45 30 Jun '09  
GeneralRe: is this still supported Pinmembermaciekboni0:31 1 Jul '09  
GeneralI'm stumped PinmemberJörgen Sigvardsson12:57 8 Nov '04  
GeneralRe: I'm stumped PinmemberRay Virzi16:01 8 Nov '04  
GeneralRe: I'm stumped PinmemberJörgen Sigvardsson9:19 9 Nov '04  
GeneralRe: I'm stumped Pinmemberlathot9:02 25 May '05  
QuestionHow about Judy Arrays? PinsussAnonymous4:53 8 Sep '03  
AnswerRe: How about Judy Arrays? PinmemberRay Virzi6:02 8 Sep '03  
QuestionDoesn't compile in Visual Studio .NET 2003? PinmemberChris Coble9:41 9 Jun '03  
AnswerRe: Doesn't compile in Visual Studio .NET 2003? PinmemberRay Virzi20:35 11 Jun '03  
GeneralRe: Doesn't compile in Visual Studio .NET 2003? [modified] PinmemberStefan Gebauer23:10 18 Aug '07  
Questionwhere is the source code ? Pinmembermwdev18:55 4 Apr '03  
AnswerRe: where is the source code ? PinmemberRay Virzi20:59 4 Apr '03  
Generalsome small extensions PinmemberGeorge Anescu3:51 4 Oct '02  
GeneralRe: some small extensions PinmemberRay Virzi20:06 4 Oct '02  
GeneralRe: some small extensions PinmemberGeorge Anescu5:55 7 Oct '02  
GeneralRe: some small extensions PinmemberRay Virzi16:35 7 Oct '02  
Generalperformance issues Pinsussm.1:25 6 Aug '02  
GeneralRe: performance issues PinsussMaxim Locktyukhin5:20 6 Aug '02  
GeneralRe: performance issues PinmemberRay Virzi15:11 7 Aug '02  
GeneralRe: performance issues PinsussMaxim Locktyukhin6:43 8 Aug '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 6 Aug 2002
Article Copyright 2001 by Ray Virzi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid