473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2006
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: orekinbck | last post by:
Hi There I am probably missing something fundamental here, but I cannot see a method to search the values of a generic dictionary so that I can find the key ? Of course I could enumerate...
2
by: newscorrespondent | last post by:
I have a list declared: public System.Collections.Generic.SortedList<int, System.Collections.Generic.List<MTGTracer ActiveList = new SortedList<int,List<MTGTracer>>(); The key is an integer...
4
by: Oscar Thornell | last post by:
Hi, Any comments regarding this implementation... SortedList<String, Stringlist = new SortedList<String, String>(); lock (((IList)list).SyncRoot) { foreach (Object item in list) {
4
by: semedao | last post by:
Hi, I want to implement list of key-values that can be sort by 2 ways. let's say that in the first step I wanted to make SortList based on Key = int index that cannot change and Value is another...
6
by: Nick Valeontis | last post by:
I know how to use Icomparable. However, I can't figure out how to sort a generic linked list? (without writing the algorithm) Lets say I have something like this: class...
1
by: raylopez99 | last post by:
I seem to get name collision between the Generic collection SortedList and C++.NET Framework collection SortedList. How to resolve? Here are the libraries that seem to clash:...
2
by: active | last post by:
This is where a different thread ended. The subject of this query is quite different from that of the other thread so I thought I should start a new thread. I'm using Private mItemList As...
1
by: Ethan Strauss | last post by:
Hi, I have been using a variety of generic collections and I really like them, but I have just noticed something weird ... If you have a generic Dictionary (or SortedList), you can retrieve...
1
by: Harold Howe | last post by:
Howdy all, The msdn help says this about SorteList<k,v>: "If the list is populated all at once from sorted data, SortedList is faster than SortedDictionary." My question is this: how do I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.