Click here to Skip to main content
13,091,145 members (63,228 online)
Rate this:
Please Sign up or sign in to vote.
See more:
my assignment is to create a DBMS iv'e created 3 classes witch build an hierarchy similar to the DataTable and DataRow Classes
Entity (T)-> Table -> Record -> Field
the Table is created from an Entity using Reflection
why and how i created them is not the issue .
what i need now is to index the List inside the Table according the the Records fields in order to optimize "query's" , so as to avoid a linear probe of all the Records
lets say for example i need to index using a field called "unitPrice" i need a collection
witch would optimize the search for lets say " .. where unitPrice >= 14"
i thought of using a dictionary
Dictionary<K,int> dict
the value being the index of the Record in the list , but i need a collection witch would except non-unique keys as well as would have the possibility to optimize range query's
so Dictionary , SortedDictionary ,HashTable or SortedList are out of the question because i need non-unique keys to be inserted ,
and a hashtable wouldn't help with range query's any ideas of what collection i could use would be most appreciated


iv'e found out i could use something called a LookUp witch is created by from a list , but i need to some how create an expression witch would give me the value of the record as the key (14) and the different positions as the values lets say ( records 15 ,500 ,67 hold a unit price witch is equal to 14 )

further more i thought the hashtable would know how to handle duplicate keys .... isn't that the all point of hashing

10x in advance .eran.
Posted 14-Jul-11 18:45pm
BobJanova 15-Jul-11 5:15am
Do you really need to be able to handle non unique keys? Most RDBs only allow you to have a unique key, though you can have a non unique index.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The point of hashing and Dictionary is using the unique keys bur also fast search by the key, which you can call indexing. The speed of search is of O(1), see[^]. If you think about it, you will understand that a non-unique key cannot be used with indexing. However, you can use objects with the same key with Dictionary.

For this purpose, for an element type ELEMENT and key type KEY you need to use the combined collection type:

System.Collections.Generic.Dictionary<KEY, System.Collections.Generic.List<ELEMENT>>

Isn't this clear? This is how to add an element to such combined collection:

using KeyedCollection = System.Collections.Generic.Dictionary<Key, System.Collections.Generic.List<int>>;
using ElementList = System.Collections.Generic.List<int>;
class Key {/*...*/} //I don't know what do you need -- anything
KeyedCollection Dictionary new KeyedCollection();
void AddKey(Key key, int element) {
    ElementList list;
    if (!Dictionary.TryGetValue(key, out list)) {
       list = new ElementList();
       Dictionary.Add(key, list);
    } //if

If the key is already in dictionary, you get the dictionary value (which is list), and add your element to it, otherwise you add a new list.

You can do the similar thing using SortedDictionary and SortedList.

RakeshMeena 15-Jul-11 2:49am
My 5 SA!
SAKryukov 15-Jul-11 3:12am
Thank you, Rakesh.
SAKryukov 15-Jul-11 5:11am
I just fixed the code sample: list.Add(element).

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 15 Jul 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100