Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(The question initially was a failing binary search on a <code>StringList</code>, and the reason turned out to be that the list was not sorted while the search was executed. See comments on the question.) <hr> What I would do would be to keep the stringlist sorted at all times, and instead of a 'Name=Value' pair to keep the frequency of occurrence, use the <a href="http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Classes_TStrings_Objects.html" rel="nofollow"><code>Objects</code></a> property for that. This would help avoid the Integer&lt;->String conversion the code originally had and can be achieved with sth like: <code>list.Objects[i] := Pointer(f + 1);</code></p> <p><p>Use the stringlist's <a href="http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Classes_TStringList_Find.html" rel="nofollow"><code>Find</code></a> method to find out if an entry already existed or not. <code>Find</code> executes a binary search itself, you don't need 3rd party code to do that. If the item exists already, just increment the frequency. If the item does not exist, the <code>Index</code> parameter that is set by the function is the place where the item should be inserted (see the documentation link above). Call the protected method <code>InsertItem</code> to insert the new item. The reason for the <em>protected</em> call is to avoid the find method run again needlessly (see <code>TStringList.AddObject</code> function in 'classes.pas'). This would be achieved with sth. like:</p> <pre><code>type TSL = class(TStringList); var i: Integer; s: string; begin s := 'Joe'; if list.Find(s, i) then list.Objects[i] := Pointer(Integer(list.Objects[i]) + 1) else TSL(list).InsertItem(i, s, Pointer(1)); end; </code></pre> <p><br />But you really need to profile and test alternatives, otherwise it is all based on guesswork.</p> <p><strong>edit:</strong> I would like to note that if there's no adding/deleting items outside of this procedure, the stringlist's sorted property could be kept un-set, and then the <em>protected</em> hack would be unnecessary as one could use the <a href="http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Classes_TStringList_InsertObject.html" rel="nofollow"><code>InsertObject</code></a> method then.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload