Generic SortedList Problem C++ VS2005

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
}
}
}
 
B

Bruce Arnold

Vladimir, thanks for the help.
Here's the "final" working code based on your tip.

// This code is about 40 percent faster on a 250,000 line
// datafile with 6000 unique url's. 5 secs to 3.
// -------------------------------------
if (mUrl->TryGetValue(hTokens[2],count))
mUrl[hTokens[2]] = 1 + count;
else
mUrl->Add(hTokens[2], 1);

....Bruce


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.

Generic::SortedList has updateable indexer property (mUrl[key]). You may
also consider to use hash table instead of sorted list, as "If insertion
causes
a resize, the operation is O(n)." for sorted list, and "If Count is less
than the capacity, this method approaches an O(1) operation." for hash
table.
 
V

Vladimir Nesterovsky

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.

Generic::SortedList has updateable indexer property (mUrl[key]). You may
also consider to use hash table instead of sorted list, as "If insertion
causes
a resize, the operation is O(n)." for sorted list, and "If Count is less
than the capacity, this method approaches an O(1) operation." for hash
table.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top