Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / September 2006

Tip: Looking for answers? Try searching our database.

Generic SortedList Problem C++ VS2005

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bruce Arnold - 27 Sep 2006 01:47 GMT
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
               }
           }
       }
Vladimir Nesterovsky - 27 Sep 2006 06:00 GMT
>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
[quoted text clipped - 4 lines]
> 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
Bruce Arnold - 27 Sep 2006 06:35 GMT
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
[quoted text clipped - 11 lines]
>than the capacity, this method approaches an O(1) operation." for hash
>table.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.