Valhalla Legends Archive

Programming => General Programming => .NET Platform => Topic started by: Hell-Lord on November 26, 2007, 01:17 AM

Title: [C#] Keyword Coloring
Post by: Hell-Lord on November 26, 2007, 01:17 AM
I need to color some keywords in C# and was wondering if adding the keyword to a HashTable would be the best way?

The keywords are stored like so...
string[] keywords = {"word","word",
                   "word","something"
                   "so on","more"};


Any ideas that could be more efficient that using a HashTable?

Edit:
Hmm might it be easier to store all the keywords in a text file, then put all the words in an array?
Title: Re: [C#] Keyword Coloring
Post by: MyndFyre on November 26, 2007, 04:08 AM
Doing it for a web app or winforms?

IMO, keyword colorizing is one of my least favorite activities.
Title: Re: [C#] Keyword Coloring
Post by: Hell-Lord on November 26, 2007, 04:26 AM
I'm doing it in a winform.
Title: Re: [C#] Keyword Coloring
Post by: brew on November 26, 2007, 09:57 AM
Not C#, but this might help:

#define NUM_KEYWORDS (7)
const unsigned int keywordlens[] = {2, 3, 5, 5, 4, 8, 6};
const char *keywords[] = {
"if",
"for",
"while",
"var32",
"void",
"function",
"return"
};

LRESULT CALLBACK RTBProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) {
if (iMsg == WM_CHAR) {
char asdf[128];
CHARRANGE CharRange;
TEXTRANGE textrange;
for (int i = 0; i != NUM_KEYWORDS; i++) {
SendMessage(hwnd, EM_EXGETSEL, 0, (LPARAM)&CharRange);
ZeroMemory(asdf, sizeof(asdf));
textrange.chrg.cpMin = CharRange.cpMin - keywordlens[i];
textrange.chrg.cpMax = CharRange.cpMin;
textrange.lpstrText = asdf;
SendMessage(hwnd, EM_GETTEXTRANGE, 0, (LPARAM)&textrange);
if (!strcmp(asdf, keywords[i])) {
CharRange.cpMax = CharRange.cpMin;
CharRange.cpMin -= keywordlens[i];
SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&CharRange);
cfFormat.crTextColor = 0x00FF0000;
SendMessage(hWnd_rtb, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cfFormat);
CharRange.cpMin += keywordlens[i];
CharRange.cpMax = CharRange.cpMin;
SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&CharRange);
cfFormat.crTextColor = 0x00FFFFFF;
SendMessage(hWnd_rtb, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cfFormat);
goto _end;
}
}
} else if ((iMsg == WM_LBUTTONUP) || (iMsg == WM_RBUTTONUP)) {
SendMessage(hWnd_rtb, EM_EXGETSEL, 0, (LPARAM)&CurrentLineRange);
}
_end:
UpdateStatusBar();
return CallWindowProc(oldRTBProc, hwnd, iMsg, wParam, lParam);
}

the keyword len array is really just there to speed it up so i don't have to call strlen on static strings (my method is quite slow, i needed to optimize it)
Title: Re: [C#] Keyword Coloring
Post by: Banana fanna fo fanna on November 26, 2007, 06:02 PM
In general, a hash table is the best data structure for this. Go for it.
Title: Re: [C#] Keyword Coloring
Post by: MyndFyre on November 26, 2007, 09:04 PM
I'm curious as to why a hash table is the best?  Why not an array list, a linked list, anything else that isn't an associative/dictionary thing?
Title: Re: [C#] Keyword Coloring
Post by: Hell-Lord on November 26, 2007, 11:21 PM
I'm thinking of going with the Hash Table because i am only really dealing with one column of data.
Title: Re: [C#] Keyword Coloring
Post by: MyndFyre on November 27, 2007, 12:54 AM
That's pretty much a reason to not go with a hash table.

Go with a plain old array.  If you can't deal with that, go with an ArrayList/List<T>.  If you REALLY want performance for multiple additions without reallocation hits, use a LinkedList<T>.  If you want order, use a List<T> and provide a Comparison<T> delegate.  But for god's sake, don't use a Hashtable to store nonkeyed data.
Title: Re: [C#] Keyword Coloring
Post by: Hell-Lord on November 27, 2007, 01:58 AM
 I was under the impression that with multiple columns of data an array would be best or a data table. But for something so simple a Hash Table would be best suited.

Quote from: MyndFyre[vL] on November 27, 2007, 12:54 AM
If you REALLY want performance for multiple additions without reallocation hits, use a LinkedList<T>.

Never thought of that, ill test it out now.
Title: Re: [C#] Keyword Coloring
Post by: MyndFyre on November 27, 2007, 08:57 AM
List<T> is more memory-performant than a LinkedList<T> as long as it doesn't need to be resized.  It also performs about as quickly as an array, roughly O(1).  LinkedList<T> is O(n) for searches, but just about O(1) for extensions (while List<T> is O(n) for extensions).

For single columns of data, arrays, array lists, and linked lists are always the best.
Title: Re: [C#] Keyword Coloring
Post by: Banana fanna fo fanna on November 27, 2007, 10:08 AM
doesn't he need to store what color each keyword is going to be? i feel like a hashtable would be the perfect solution seeing as it's designed to map keys (keywords) to values (colors)...
Title: Re: [C#] Keyword Coloring
Post by: MyndFyre on November 27, 2007, 10:21 AM
Quote from: Banana fanna fo fanna on November 27, 2007, 10:08 AM
doesn't he need to store what color each keyword is going to be? i feel like a hashtable would be the perfect solution seeing as it's designed to map keys (keywords) to values (colors)...
If that were the case tben yes, a hashtable (actually, a Dictionary<TKey, TValue>) would be the best way to do it.  But he said:
Quote from: Hell-Lord on November 26, 2007, 11:21 PM
i am only really dealing with one column of data.
...which means the data should be nonassociative.
Title: Re: [C#] Keyword Coloring
Post by: Banana fanna fo fanna on November 27, 2007, 12:46 PM
Quote from: MyndFyre[vL] on November 27, 2007, 10:21 AM
Quote from: Banana fanna fo fanna on November 27, 2007, 10:08 AM
doesn't he need to store what color each keyword is going to be? i feel like a hashtable would be the perfect solution seeing as it's designed to map keys (keywords) to values (colors)...
If that were the case tben yes, a hashtable (actually, a Dictionary<TKey, TValue>) would be the best way to do it.  But he said:
Quote from: Hell-Lord on November 26, 2007, 11:21 PM
i am only really dealing with one column of data.
...which means the data should be nonassociative.

Missed that...my bad
Title: Re: [C#] Keyword Coloring
Post by: Smarter on November 29, 2007, 11:30 PM
Yeah, I was going to suggest a Dictionary:


Dictionary<string, Color> keyWords = new Dictionary<string, Color>();
keyWords.Add("word", Color.Red);


Along those lines?
Title: Re: [C#] Keyword Coloring
Post by: Hell-Lord on November 29, 2007, 11:56 PM
There stored in a string array though.
Title: Re: [C#] Keyword Coloring
Post by: MyndFyre on November 30, 2007, 02:50 AM
if you need to store specific colors with each word a Dictionary is most appropriate. If you need to add or remove words at runtime then a List is most appropriate. If not, an array is best.