Click here to Skip to main content
15,910,886 members
Home / Discussions / C#
   

C#

 
GeneralRe: Get control form its NAME Pin
Joel Lucsy3-Aug-05 4:11
Joel Lucsy3-Aug-05 4:11 
GeneralChanging Code Access Permissions Pin
2-Aug-05 12:36
suss2-Aug-05 12:36 
GeneralRe: Changing Code Access Permissions Pin
Judah Gabriel Himango2-Aug-05 16:24
sponsorJudah Gabriel Himango2-Aug-05 16:24 
GeneralRe: Changing Code Access Permissions Pin
timbobsteve2-Aug-05 17:10
timbobsteve2-Aug-05 17:10 
GeneralEmbedding managed DirectX in a WinForms Custom Control Pin
benwalker2-Aug-05 11:52
benwalker2-Aug-05 11:52 
GeneralRe: Embedding managed DirectX in a WinForms Custom Control Pin
Judah Gabriel Himango2-Aug-05 16:28
sponsorJudah Gabriel Himango2-Aug-05 16:28 
QuestionPattern matching Hashtable keys? Pin
Dewclaws2-Aug-05 11:29
Dewclaws2-Aug-05 11:29 
AnswerRe: Pattern matching Hashtable keys? Pin
Lars Niedziolka2-Aug-05 13:20
Lars Niedziolka2-Aug-05 13:20 
Hi Dewclaws,

A Hastable is not the right collection for your problem.

How does a hashtable works

A hashtable build a array with N bags: bag #0 .. bag #N-1.
If you add a (key;value)-pair in the hashtable, the hashtable first compute the hashcode of you key.
For this it uses key.GetHashCode().
Than its arrange the hashcode in one of the bags. In most cases if it uses the bag #(hashcode%N).
In the bag, its store the pair of key and value. In a bag can be more than one pair.
If you search a value of a key, the hashtable compute the hashcode again and looks only in this one bag.

Example with 5 Bags:
Key     Hashcode Bag
tommy   23    -> 3
frang   43    -> 3
fratiti 12    -> 2
tom     45    -> 0
eddtom  23    -> 3
frankie 67    -> 2
blue    56    -> 1

bag #0: (tom;object)
bag #1: (blue;object)
bag #2: (fratiti;object)
bag #3: (tommy;object) (frang;object) (eddtom;object)
bag #4: 

If you select N large enough (about 1x..1.5x the number of keys) you have a mean search cost of O(n).

Solution 1
A solution is to use one sorted list of pairs or two sorted list of keys respectively values.
ArrayList keys   = new ArrayList();
ArrayList values = new ArrayList();

public void Insert(string key, object value)
{
  int index = keys.BinarySearch(key);
  if (index >= 0) // key already in the keys list
  {
    values[index] = value;
  }
  else
  {
    keys.Insert(~index, key);
    values.Insert(~index, value);
  }
}


If you search all (key;value)-pairs in which the key starts with a special string use binary search again.
public struct Range { public int From; public To };

public Range Find(string keyStart)
{
  Range range;
  range.From = keys.BinarySearch(keyStart);
  range.To   = keys.BinarySearch(keyStart + char.MaxValue);
  if (range.From < 0) range.From = ~range.From;
  if (range.To < 0) range.To = ~range.To - 1;
  return range;
}


Solution 2
Build a search tree.
The root node contains for each letter (symbol) at the first position a subnode. Each subnode for each letter (symbol) at the second position a subnode. And so on.
This solution is a bit faster than Solution 1, but a lot more complex to implement.

Example:
[ROOT]
 |- b
 |  '-----------------> (blue;object)
 |- e
 |  '-----------------> (eddtom;object)
 |- f
 |  '-r
 |    '-a
 |      |-n
 |      | |-g
 |      | | '---------> (frang;object)
 |      | '-k
 |      |   '---------> (frankie;object)
 |      '-t
 |        '-----------> (fratiti;object)
 '- t
    '-o
      '-m
        |-------------> (tom;object)
        '-m
          '-----------> (tommy;object)

If you search for the keys which starts with tom, you walk the path t-o-m and select all subnodes.

Solution 3
If you look for all keys, which contains a string in any position, you can walk throw the full list.



Hope, one solution helps you.

Niedzi
GeneralRegular expression help - end of text Pin
Luis Alonso Ramos2-Aug-05 9:05
Luis Alonso Ramos2-Aug-05 9:05 
GeneralRe: Regular expression help - end of text Pin
Guffa2-Aug-05 9:15
Guffa2-Aug-05 9:15 
GeneralRe: Regular expression help - end of text Pin
Luis Alonso Ramos2-Aug-05 9:21
Luis Alonso Ramos2-Aug-05 9:21 
GeneralRe: Regular expression help - end of text Pin
Luis Alonso Ramos2-Aug-05 9:24
Luis Alonso Ramos2-Aug-05 9:24 
GeneralRe: Regular expression help - end of text Pin
Guffa2-Aug-05 9:49
Guffa2-Aug-05 9:49 
GeneralRe: Regular expression help - end of text Pin
Luis Alonso Ramos2-Aug-05 10:00
Luis Alonso Ramos2-Aug-05 10:00 
Questionstatic class for database access? Pin
theStorminMormon2-Aug-05 8:58
theStorminMormon2-Aug-05 8:58 
AnswerRe: static class for database access? Pin
Guffa2-Aug-05 9:11
Guffa2-Aug-05 9:11 
GeneralRe: static class for database access? Pin
theStorminMormon2-Aug-05 9:14
theStorminMormon2-Aug-05 9:14 
GeneralRe: static class for database access? Pin
Luis Alonso Ramos2-Aug-05 9:32
Luis Alonso Ramos2-Aug-05 9:32 
GeneralRe: static class for database access? Pin
Guffa2-Aug-05 9:36
Guffa2-Aug-05 9:36 
AnswerRe: static class for database access? Pin
Luis Alonso Ramos2-Aug-05 9:13
Luis Alonso Ramos2-Aug-05 9:13 
GeneralRe: static class for database access? Pin
theStorminMormon2-Aug-05 9:16
theStorminMormon2-Aug-05 9:16 
GeneralRe: static class for database access? Pin
Luis Alonso Ramos2-Aug-05 9:26
Luis Alonso Ramos2-Aug-05 9:26 
GeneralEmulate a twain interface Pin
Oliver Lange2-Aug-05 8:25
Oliver Lange2-Aug-05 8:25 
GeneralSupressing Security Error Dialog Pin
RB@Emphasys2-Aug-05 7:53
RB@Emphasys2-Aug-05 7:53 
GeneralRe: Supressing Security Error Dialog Pin
mav.northwind2-Aug-05 20:06
mav.northwind2-Aug-05 20:06 

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.