Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C#
Article

Fixed Index Array

Rate me:
Please Sign up or sign in to vote.
3.25/5 (8 votes)
18 Nov 2008CPOL 16.1K   77   19   2
'FixIndexArray' provides direct access like a List, and has a fixed index position like a Dictionary.

Introduction

The idea about the FixIndexArray class came to me when I was looking for an array structure which will allow:

  • To have direct access to an item by index, like a List<T>.
  • To guarantee the same index during deleting an item, like a Dictionary<int,T> (analogue of a DB primary key).
  • To have higher performance of deleting/adding an item than a Dictionary<int,T>.

As a result, Ideveloped the class FixIndexArray. The final performance result shown on the picture below proves that it meets all the above requirements.

1c.JPG

Using the code

Using the class cFixIndexArr is very straightforward, and API is very similar to List<T>.

C#
//
// This code demonstrates how to use Enumerator  
//
private void PrintArr(cFixIndexArr<int> arr) {
    Console.Write("Array : "); 
    foreach(int val in arr) {
        Console.Write(string.Format("{0}, ", val));
    }
}
//
// This code demonstrates the most common use cases  
// 
[Test]
public void UseCase() {
    Console.WriteLine(
      "Create the FixIndexArr of integers with default capacity 10"
    );  
    cFixIndexArr<int> arr = new cFixIndexArr<int>(10);
    Console.WriteLine("Load array: {0,10,20,...90}");
    for(int i=0; i<10; i++) {
        arr.Add(i*10); 
    }
    this.PrintArr(arr);
    Console.WriteLine("\r\n\r\nDelete elements 1, 3, 5, 7, 9"); 
    arr.Remove(1); 
    arr.Remove(3); 
    arr.Remove(5);
    arr.Remove(7);
    arr.Remove(9);
    this.PrintArr(arr);
    Console.WriteLine(
       "\r\n\r\nAdd element '11', '22', '33', '44', '55', '66','77'"
    );
    arr.Add(11);
    arr.Add(22);
    int index33 = arr.Add(33);
    arr.Add(44);
    arr.Add(55);
    arr.Add(66);
    arr.Add(77);
    this.PrintArr(arr);
    Console.WriteLine("\r\n\r\nReplacing element 33 by 3333");
    arr[index33] = 3333;
    this.PrintArr(arr);
    Console.WriteLine(
       "\r\n\r\nDelete element 3333 and use it for element 4444"
    );
    arr.Remove(index33);
    arr[index33] = 4444;
    this.PrintArr(arr);
}

This code produces the following result:

2c.JPG

How does it work?

The idea behind is simple: The class FixIndexArr<T> uses an array valArr of type List<T> for elements itself, and a helper array deletedIndexArr of type List<cItem> for tracking the deleted indexes. The structure cItem is a helper, and contains information about deleted indices in a double linked list manner:

3c.JPG

License

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


Written By
Software Developer (Senior) SSI
United States United States
Developer Pascal, c, c++, VB, Java, C#

Comments and Discussions

 
QuestionHave you tried KeyedCollection&lt;k,&gt;? Pin
Paul B.25-Nov-08 3:44
Paul B.25-Nov-08 3:44 
AnswerRe: Have you tried KeyedCollection&lt;k,&gt;? Pin
mpustovoyt25-Nov-08 15:29
mpustovoyt25-Nov-08 15:29 
Thanks a lot for question. You are right I didn't try KeyedCollection<int,> before, but I made some tests now and here are results:
1) This class is abstract and we have to create instanse class based on KeyedCollection;
2) It has performance of Dictionary for adding items and performance of List for deleting:

------------ KeyedCollection int,int ---------
ms to load 1000000 items to KeyedCollection : 203
ms to access 1000000 items from KeyedCollection : 78
ms to delete 1000 items from KeyedCollection : 625

The performance results:
------ Testing List----------
ms to load 1000000 items to List int : 15
ms to access 10000000 items to List int :78
ms to delete 1000 items from List int :921

---------- Dictionary -----------
ms to load 1000000 items to Dictionary int,int :171
ms to access 10000000 items in Dictionary int,int : 546
ms to delete 1000000 items from Dictionary int,int :62

------------ FixIndexArr int ---------
ms to load 1000000 items to FixIndexArr int :78
ms to access 10000000 items in FixIndexArr int :140
ms to raw access 10000000 items in FixIndexArr int :109
ms to delete 1000000 items from FixIndexArr int :140
ms to set 1000000 items in FixIndexArr int :187

Conclusion: It has the same problem as List<>: the deleting speed is too low

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.