B
Bruce Arnold
I have a program that works fine using Remove and Add to update
a value. The program processes a log file from a router and
counts the hits based on url. It bothers me to use Remove/Add when
all I want to do is change a value. I can get the index using
IndexOfKey. There is no "SetByIndex" in Generic. Note:
the list has two variables named "value" and "Value" that
I can see in the debugger. I need to change the lower case
"value" but it is protected from change.
....Bruce
COMPILE WITH vc++ 7 Visual Studio 2005 CLR
void Process(void)
{
int i=0, count;
Generic::SortedList<String^, int> ^mUrl = gcnew
Generic::SortedList<String^, int>();
try
{
StreamReader^ sr = gcnew StreamReader( LOGFILE );
String ^ hDelims = ":\n\r[]";
array<Char>^delimiter = hDelims->ToCharArray();
array<String^>^ hTokens = nullptr;
try
{
String^ slineBuff;
while ( slineBuff = sr->ReadLine() ) // Read 179827
lines.
{
hTokens = slineBuff->Split(delimiter,5);
int n = hTokens->Length;
if (n>2 && hTokens[1] == "ALLOW")
{
#if 1==0
//--------------------------------------------------------
// This code may be faster if I can make it work.
int idx = mUrl->IndexOfKey(hTokens[2]);
if (idx >= 0)
{
count = mUrl->value[idx] ; // but LowerCase
value is private.
mUrl->value[idx] = ++count;
}
else
mUrl->Add(hTokens[2], 1);
//--------------------------------------------------------
#else
// This code works fine but is probably slower.
if (mUrl->TryGetValue(hTokens[2],count))
{
mUrl->Remove(hTokens[2]);
mUrl->Add(hTokens[2], ++count);
}
else
mUrl->Add(hTokens[2], 1);
//--------------------------------------------------------
#endif
}
}
}
a value. The program processes a log file from a router and
counts the hits based on url. It bothers me to use Remove/Add when
all I want to do is change a value. I can get the index using
IndexOfKey. There is no "SetByIndex" in Generic. Note:
the list has two variables named "value" and "Value" that
I can see in the debugger. I need to change the lower case
"value" but it is protected from change.
....Bruce
COMPILE WITH vc++ 7 Visual Studio 2005 CLR
void Process(void)
{
int i=0, count;
Generic::SortedList<String^, int> ^mUrl = gcnew
Generic::SortedList<String^, int>();
try
{
StreamReader^ sr = gcnew StreamReader( LOGFILE );
String ^ hDelims = ":\n\r[]";
array<Char>^delimiter = hDelims->ToCharArray();
array<String^>^ hTokens = nullptr;
try
{
String^ slineBuff;
while ( slineBuff = sr->ReadLine() ) // Read 179827
lines.
{
hTokens = slineBuff->Split(delimiter,5);
int n = hTokens->Length;
if (n>2 && hTokens[1] == "ALLOW")
{
#if 1==0
//--------------------------------------------------------
// This code may be faster if I can make it work.
int idx = mUrl->IndexOfKey(hTokens[2]);
if (idx >= 0)
{
count = mUrl->value[idx] ; // but LowerCase
value is private.
mUrl->value[idx] = ++count;
}
else
mUrl->Add(hTokens[2], 1);
//--------------------------------------------------------
#else
// This code works fine but is probably slower.
if (mUrl->TryGetValue(hTokens[2],count))
{
mUrl->Remove(hTokens[2]);
mUrl->Add(hTokens[2], ++count);
}
else
mUrl->Add(hTokens[2], 1);
//--------------------------------------------------------
#endif
}
}
}