• Welcome to Valhalla Legends Archive.
 

[C#] Keyword Coloring

Started by Hell-Lord, November 26, 2007, 01:17 AM

Previous topic - Next topic

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...
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?

MyndFyre

Doing it for a web app or winforms?

IMO, keyword colorizing is one of my least favorite activities.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Hell-Lord


brew

#3
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)
<3 Zorm
Quote[01:08:05 AM] <@Zorm> haha, me get pussy? don't kid yourself quik
Scio te esse, sed quid sumne? :P

Banana fanna fo fanna

In general, a hash table is the best data structure for this. Go for it.

MyndFyre

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?
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Hell-Lord

I'm thinking of going with the Hash Table because i am only really dealing with one column of data.

MyndFyre

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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

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 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.

MyndFyre

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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Banana fanna fo fanna

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)...

MyndFyre

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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Banana fanna fo fanna

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

Smarter

Yeah, I was going to suggest a Dictionary:


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


Along those lines?
Since '99

BrutalNet.Net

Hell-Lord

There stored in a string array though.