Valhalla Legends Forums Archive | .NET Platform | [C#] Keyword Coloring

AuthorMessageTime
Hell-Lord
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...
[code]string[] keywords = {"word","word",
                  "word","something"
                  "so on","more"};[/code]

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?
November 26, 2007, 7:17 AM
Myndfyr
Doing it for a web app or winforms?

IMO, keyword colorizing is one of my least favorite activities.
November 26, 2007, 10:08 AM
Hell-Lord
I'm doing it in a winform.
November 26, 2007, 10:26 AM
BreW
Not C#, but this might help:
[code]
#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);
}
[/code]
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)
November 26, 2007, 3:57 PM
St0rm.iD
In general, a hash table is the best data structure for this. Go for it.
November 27, 2007, 12:02 AM
Myndfyr
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?
November 27, 2007, 3:04 AM
Hell-Lord
I'm thinking of going with the Hash Table because i am only really dealing with one column of data.
November 27, 2007, 5:21 AM
Myndfyr
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.
November 27, 2007, 6:54 AM
Hell-Lord
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 author=MyndFyre[vL] link=topic=17191.msg175052#msg175052 date=1196146486]
If you REALLY want performance for multiple additions without reallocation hits, use a LinkedList<T>.[/quote]

Never thought of that, ill test it out now.
November 27, 2007, 7:58 AM
Myndfyr
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.
November 27, 2007, 2:57 PM
St0rm.iD
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)...
November 27, 2007, 4:08 PM
Myndfyr
[quote author=Banana fanna fo fanna link=topic=17191.msg175058#msg175058 date=1196179702]
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)...
[/quote]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 author=Hell-Lord link=topic=17191.msg175051#msg175051 date=1196140879]
i am only really dealing with one column of data.
[/quote]
...which means the data should be nonassociative.
November 27, 2007, 4:21 PM
St0rm.iD
[quote author=MyndFyre[vL] link=topic=17191.msg175059#msg175059 date=1196180486]
[quote author=Banana fanna fo fanna link=topic=17191.msg175058#msg175058 date=1196179702]
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)...
[/quote]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 author=Hell-Lord link=topic=17191.msg175051#msg175051 date=1196140879]
i am only really dealing with one column of data.
[/quote]
...which means the data should be nonassociative.
[/quote]

Missed that...my bad
November 27, 2007, 6:46 PM
Smarter
Yeah, I was going to suggest a Dictionary:

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

Along those lines?
November 30, 2007, 5:30 AM
Hell-Lord
There stored in a string array though.
November 30, 2007, 5:56 AM
Myndfyr
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.
November 30, 2007, 8:50 AM

Search