471,071 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

Generic SortedList Problem C++ VS2005

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

Sep 27 '06 #1
2 1891
>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.

--
Vladimir Nesterovsky


Sep 27 '06 #2
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
On Wed, 27 Sep 2006 07:51:52 +0200, "Vladimir Nesterovsky"
<vl******@nesterovsky-bros.comwrote:
>>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.
Sep 27 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by newscorrespondent | last post: by
4 posts views Thread by Oscar Thornell | last post: by
6 posts views Thread by Nick Valeontis | last post: by
2 posts views Thread by active | last post: by
1 post views Thread by Ethan Strauss | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.