Click here to Skip to main content
15,920,468 members
Home / Discussions / C#
   

C#

 
Questionchange datagridview column name Pin
benjamin yap4-Aug-08 5:50
benjamin yap4-Aug-08 5:50 
AnswerRe: change datagridview column name Pin
Gulfraz Khan4-Aug-08 6:06
Gulfraz Khan4-Aug-08 6:06 
GeneralRe: change datagridview column name Pin
nelsonpaixao4-Aug-08 12:53
nelsonpaixao4-Aug-08 12:53 
GeneralRe: change datagridview column name Pin
benjamin yap4-Aug-08 16:37
benjamin yap4-Aug-08 16:37 
QuestionFancy search function Pin
Dewald4-Aug-08 5:01
Dewald4-Aug-08 5:01 
AnswerRe: Fancy search function Pin
Gulfraz Khan4-Aug-08 5:13
Gulfraz Khan4-Aug-08 5:13 
AnswerRe: Fancy search function Pin
Guffa4-Aug-08 7:05
Guffa4-Aug-08 7:05 
AnswerRe: Fancy search function Pin
Guffa4-Aug-08 10:21
Guffa4-Aug-08 10:21 
Intrigued by the idea of a search trea, I tried to build a simple class that does it:

public class SearchTree<T> {

	private class UniqueList<Key> : IEnumerable<Key> {

		private Dictionary<Key, byte> _list;

		public UniqueList() {
			_list = new Dictionary<Key, byte>();
		}

		public void Add(Key value) {
			_list[value] = 1;
		}

		public Key[] ToArray() {
			Key[] result = new Key[_list.Count];
			int index = 0;
			foreach (Key value in _list.Keys) {
				result[index++] = value;
			}
			return result;
		}

		public IEnumerator<Key> GetEnumerator() {
			return _list.Keys.GetEnumerator();
		}

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
			return _list.Keys.GetEnumerator();
		}

	}

	private class NodeList : Dictionary<char, SearchNode> {

		public new SearchNode this[char c] {
			get {
				SearchNode node;
				if (!this.TryGetValue(c, out node)) {
					node = new SearchNode();
					this.Add(c, node);
				}
				return node;
			}
		}

	}

	private class SearchNode : NodeList {

		private UniqueList<T> _indexes;

		public SearchNode() {
			_indexes = new UniqueList<T>();
		}

		public void Add(string word, int pos, T index) {
			_indexes.Add(index);
			if (pos < word.Length) {
				this[word[pos]].Add(word, pos + 1, index);
			}
		}

		public T[] FindWord(string text) {
			if (text.Length == 0) {
				return _indexes.ToArray();
			} else {
				SearchNode node;
				if (this.TryGetValue(text[0], out node)) {
					return node.FindWord(text.Substring(1));
				}
				return null;
			}
		}

	}

	NodeList _nodes;

	public SearchTree() {
		_nodes = new NodeList();
	}

	public void AddWord(string word, T index) {
		for (int i = 0; i < word.Length; i++) {
			_nodes[word[i]].Add(word, i + 1, index);
		}
	}

	public T[] FindWord(string text) {
		if (text.Length > 0) {
			return _nodes[text[0]].FindWord(text.Substring(1));
		}
		return null;
	}

}

Creation of a search tree (from the string array lines):
SearchTree<int> tree = new SearchTree<int>();
for (int i = 0; i < lines.Length; i++) {
	foreach (string word in lines[i].Split(' ')) {
		tree.AddWord(word, i);
	}
}

Usage:
int[] result = tree.FindWord("a");

The result is an array of indexes for the lines where the words were found (or null if nothing was found).

I made some test data by copying text from some articles here, ending up with 3313 lines of text, 22248 words. Creating a tree from this takes around 260 ms on my computer, and searching the tree for a string like "win" gives a result of 31 lines and takes around 0.00076 ms.

The class is quite flexible, you can use most anything as index, you could even use the rows in a grid view: SearchTree<DataGridViewRow>. Searching that tree would give you an array containing the rows where the words were found.

Smile | :)

Despite everything, the person most likely to be fooling you next is yourself.

GeneralRe: Fancy search function Pin
DaveyM694-Aug-08 10:34
professionalDaveyM694-Aug-08 10:34 
GeneralRe: Fancy search function Pin
Dewald4-Aug-08 20:19
Dewald4-Aug-08 20:19 
AnswerRe: Fancy search function Pin
nelsonpaixao4-Aug-08 12:58
nelsonpaixao4-Aug-08 12:58 
QuestionHow do I make a formless windows application? Pin
I_Need_Help4-Aug-08 4:50
I_Need_Help4-Aug-08 4:50 
AnswerRe: How do I make a formless windows application? Pin
Gulfraz Khan4-Aug-08 4:58
Gulfraz Khan4-Aug-08 4:58 
AnswerRe: How do I make a formless windows application? Pin
vikas amin4-Aug-08 5:31
vikas amin4-Aug-08 5:31 
GeneralRe: How do I make a formless windows application? Pin
vikas amin4-Aug-08 7:16
vikas amin4-Aug-08 7:16 
Questiongetting result from the cmd prompt Pin
lune124-Aug-08 4:44
lune124-Aug-08 4:44 
AnswerRe: getting result from the cmd prompt [modified] Pin
vikas amin4-Aug-08 5:41
vikas amin4-Aug-08 5:41 
AnswerRe: getting result from the cmd prompt Pin
smilethat4-Aug-08 6:45
smilethat4-Aug-08 6:45 
GeneralRe: getting result from the cmd prompt Pin
lune125-Aug-08 20:58
lune125-Aug-08 20:58 
QuestionData entry validation and databinding Pin
Gulfraz Khan4-Aug-08 4:20
Gulfraz Khan4-Aug-08 4:20 
QuestionLoad Report Failed Pin
Er.bRijal4-Aug-08 3:49
Er.bRijal4-Aug-08 3:49 
QuestionRe: Load Report Failed Pin
selcuks4-Aug-08 4:43
selcuks4-Aug-08 4:43 
QuestionPass Paramters from Popup to page Pin
Agweet4-Aug-08 3:45
Agweet4-Aug-08 3:45 
AnswerRe: Pass Paramters from Popup to page Pin
Alaric_4-Aug-08 3:57
professionalAlaric_4-Aug-08 3:57 
GeneralRe: Pass Paramters from Popup to page Pin
Agweet4-Aug-08 4:07
Agweet4-Aug-08 4:07 

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.