473,606 Members | 2,885 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::Sorted List<String^, int^mUrl = gcnew
Generic::Sorted List<String^, int>();

try
{
StreamReader^ sr = gcnew StreamReader( LOGFILE );
String ^ hDelims = ":\n\r[]";
array<Char>^del imiter = hDelims->ToCharArray( );
array<String^>^ hTokens = nullptr;

try
{
String^ slineBuff;
while ( slineBuff = sr->ReadLine() ) // Read 179827
lines.
{
hTokens = slineBuff->Split(delimite r,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(hTo kens[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(hT okens[2],count))
{
mUrl->Remove(hToke ns[2]);
mUrl->Add(hTokens[2], ++count);
}
else
mUrl->Add(hTokens[2], 1);

//--------------------------------------------------------
#endif
}
}
}

Sep 27 '06 #1
2 2025
>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::Sorted List 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(hT okens[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******@neste rovsky-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::Sorte dList 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
31651
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 through the Values collection but that seems a little long winded. I ended up using a sorted list because it has the IndexOfValue method. However I don't need the sorting.
2
2053
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 and the value is a list of MTGTracer. The documentation states:
4
2725
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
6761
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 SortedList like this: class OtherSortedList : SortedList { ..... int GetTotalAmountOfAllItems().... }
6
6951
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 DisJunctiveConstraintList : LinkedList<DisjunctiveConstraint>
1
3896
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: System::Collections::SortedList, System::Collections::Generic::SortedList, using namespace System::Collections; using namespace System::Collections::Generic; Below is a working version of the generic template SortedList, which
2
1325
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 SortedList(Of String, StringWithInteger) To populate a generic CombBox StringWithInteger has two properties Str and Value
1
1631
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 elements from the list by index (I think). So: Dictionary<string, stringThisStringDictionary = new Dictionary<string, string>(); ---Code to populate dictionary---
1
3091
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 initialize a SortedList all at once from sorted data?
0
8031
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7962
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8443
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8107
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6792
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5971
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3945
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.