Click here to Skip to main content
15,878,959 members
Articles / Programming Languages / C++
Article

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

Rate me:
Please Sign up or sign in to vote.
3.71/5 (4 votes)
5 Aug 2002CPOL 205.6K   1.4K   37   37
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
DemonS7728-Oct-10 0:13
DemonS7728-Oct-10 0:13 
Questionis this still supported Pin
maciekboni20-Jun-09 5:37
maciekboni20-Jun-09 5:37 
AnswerRe: is this still supported Pin
Ray Virzi20-Jun-09 8:18
Ray Virzi20-Jun-09 8:18 
GeneralRe: is this still supported Pin
maciekboni30-Jun-09 18:54
maciekboni30-Jun-09 18:54 
GeneralRe: is this still supported Pin
Ray Virzi30-Jun-09 19:45
Ray Virzi30-Jun-09 19:45 
GeneralRe: is this still supported Pin
maciekboni1-Jul-09 0:31
maciekboni1-Jul-09 0:31 
GeneralI'm stumped Pin
Jörgen Sigvardsson8-Nov-04 12:57
Jörgen Sigvardsson8-Nov-04 12:57 
GeneralRe: I'm stumped Pin
Ray Virzi8-Nov-04 16:01
Ray Virzi8-Nov-04 16:01 
GeneralRe: I'm stumped Pin
Jörgen Sigvardsson9-Nov-04 9:19
Jörgen Sigvardsson9-Nov-04 9:19 
GeneralRe: I'm stumped Pin
lathot25-May-05 9:02
lathot25-May-05 9:02 
QuestionHow about Judy Arrays? Pin
Anonymous8-Sep-03 4:53
Anonymous8-Sep-03 4:53 
AnswerRe: How about Judy Arrays? Pin
Ray Virzi8-Sep-03 6:02
Ray Virzi8-Sep-03 6:02 
QuestionDoesn't compile in Visual Studio .NET 2003? Pin
Chris Coble9-Jun-03 9:41
Chris Coble9-Jun-03 9:41 
AnswerRe: Doesn't compile in Visual Studio .NET 2003? Pin
Ray Virzi11-Jun-03 20:35
Ray Virzi11-Jun-03 20:35 
GeneralRe: Doesn't compile in Visual Studio .NET 2003? [modified] Pin
Stefan Gebauer18-Aug-07 23:10
Stefan Gebauer18-Aug-07 23:10 
Questionwhere is the source code ? Pin
mwdev4-Apr-03 18:55
mwdev4-Apr-03 18:55 
AnswerRe: where is the source code ? Pin
Ray Virzi4-Apr-03 20:59
Ray Virzi4-Apr-03 20:59 
Generalsome small extensions Pin
4-Oct-02 3:51
suss4-Oct-02 3:51 
GeneralRe: some small extensions Pin
Ray Virzi4-Oct-02 20:06
Ray Virzi4-Oct-02 20:06 
GeneralRe: some small extensions Pin
George Anescu7-Oct-02 5:55
George Anescu7-Oct-02 5:55 
GeneralRe: some small extensions Pin
Ray Virzi7-Oct-02 16:35
Ray Virzi7-Oct-02 16:35 
Generalperformance issues Pin
M.6-Aug-02 1:25
M.6-Aug-02 1:25 
GeneralRe: performance issues Pin
MaxLock6-Aug-02 5:20
MaxLock6-Aug-02 5:20 
GeneralRe: performance issues Pin
Ray Virzi7-Aug-02 15:11
Ray Virzi7-Aug-02 15:11 
GeneralRe: performance issues Pin
MaxLock8-Aug-02 6:43
MaxLock8-Aug-02 6:43 

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

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