PermaLink BetterListStr V2 (now with fewer bugs!)04/20/2007 07:26 AM
In my last post, I mentioned an error in BetterListStr, a class I've been using for nearly a year now. Jens Augustiny of LIGONET has kindly pointed out a fix which I have (finally) incorporated. That, along with a few other small improvments, can be seen below.

The major fix here is the addition of a second list within the class to enable the use of the first key to get the second (numeric) list index. That permits a list item to be updated, not just added. The overhead for the second index should be acceptable, but I'm not really sure how to check on the memory usage of variables/classes/etc within Lotusscript. You still can't delete from the class without causing problems (except LIFO deletion).

I built some code to test this class. I hope to release this soon as part of a larger project.

'BetterListStr class - a modified version of BetterList. It's a list that keeps a count of its contents. ' ' This one has been enhanced by what I'll call a double index. In other words, each item can be accessed ' by the traditional "key", but it can also be reached via BetterListStr.GetItem( cstr(i) ) where i is between 1 and # of ' items in the list. I happen to think this is a cool idea. ' ' double-indexing doesn't work if you remove items from the list (though LIFO removal would be ok) ' ' Not really well-tested for blank keys ' Public Class BetterListStr Private m_list List As String Private m_xreflist List As String Private m_count As Integer Property Get Count As Integer Count = m_count End Property Public Function DeleteList Erase m_list Erase m_xreflist m_count = 0 End Function Public Sub new m_count = 0 End Sub Sub Delete '<----------------------------------- Call Me.DeleteList End Sub Public Function DeleteItem(key As String) As Integer '<---------------------------- Dim rval As Integer 'if the key is in the list, erase the object If ( Iselement(m_list(key)) ) Then Erase m_list(key) ' do the same with the second index Erase m_List(m_xreflist(key)) m_count = m_count-1 rval = True Else 'if there's no such key, warn of an error rval = False Print "Item " & key & " not found. It could not be deleted." End If DeleteItem = rval End Function Public Function AddItem(key As String, item As String) As Integer '<-------------------- 'just add the item if the key doesn't exist If ( Not Iselement(m_List(key)) ) Then m_list(key) = item m_count = m_count+1 'a double index so we can walk the list from the count alone m_list( Cstr(m_count) ) = item ' x-ref the second index: m_xreflist (key) = Cstr(m_count) Else 'if the key does exist, erase the current object in the list 'and add the new one Erase m_List(key) m_list(key) = item ' do the same with the second index Erase m_List(m_xreflist(key)) m_List(m_xreflist(key)) = item End If End Function Public Function GetItem(key As String) As String '<----------------------------------- On Error ErrListItemDoesNotExist Goto NoSuchItem GetItem = m_list(key) OK: Exit Function NoSuchItem: 'return a value of "" if the key was not found Print "List item " & key & " not found." GetItem = "" Resume OK End Function 'see if there's an object in the list for a given key '<----------------------------------- Public Function IsInList(key As String) As Integer IsInList = Iselement(m_List(key)) End Function End Class
This LotusScript was converted to HTML using the ls2html routine,
provided by Julian Robichaux at nsftools.com.
-Bruce Bruce Perry 617-740-0416 BELL (Building Educated Leaders for Life) - www.bellnational.org

Technorati:
This page has been accessed 298 times. .
Comments :v

1. Jens05/04/2007 08:18:45 PM
Homepage: http://www.ligonet.ch


Sure, your deleteItem method calls for troubles, if you try to delete an item in the middle of the list.

The simplest solution for this problem is to remove the following line:

m_count = m_count-1

That way you produce holes in the list like in a Swiss Cheese so you have to provide for the case, that you do not find an item through the second index. There are other strategies, that may be used to make it even more flexible, that is to remove the holes:

Move down all the entries from the deletion point up to the end of the list and remove the last list entry instead:

m_count = m_count-1
for i=val(m_xreflist(key)) to m_count
m_list(cstr(i))=m_list(cstr(i+1))
next
erase m_list(cstr(i))

Warning: Not tested and not checked for any extreme conditions ....

Shouldn't have too much of an impact except for really big lists




2. Bruce Perry05/09/2007 09:52:06 PM
Homepage: http://www.bhrpconsulting.com


Not decreasing m_count when doing deletes from the middle of the list would work, but at the cost of not knowing the number of items in the list. Since having a count of items was my first goal in creating this class, I'll leave things as they stand for the moment.<br><br>
Under some circumstances, not decrementing the count would work fine. If you never need a count of the items in the list, there's no problem.<br><br>
I suspect you're correct about the performance of the code for actually doing the deletes. Unless the list is huge or the number of deletes is huge, the impact shouldn't be excessive.




3. Jens05/29/2007 04:39:39 PM
Homepage: http://www.ligonet.ch


Sure

Replace m_count with m_index and m_count is free to do the counting work. Just a simple example that using one single variable for two independent, distinct functions may heart someday




Add Manual Trackback
Please enter the details of the trackback post. Your trackback will not appear on the site until it has been verified. This won't be immediate, as trackbacks are validated on a scheduled basis. Be patient.











Powered By :

BlogSphere V1.2 Beta 3

Join The WebLog Revolution at BlogSphere.net

Contact Me
The BlogRoll
What is a Jagular?
It is a malapropism for jaguar found in Winnie the Pooh and has nothing to do with jugulars.

usually

for more information about jagulars try here
Lotus Domino ND6 RSS OPML List RSS News Feed RSS Comments Feed Geo URL netcraft RSS Validator Blog Admin Lotus Geek Chris. A. Brandlehner Open Notes Picture Database OpenNTF CoComment Integrated BlogSphere
Calendar
No calendar found.
By Category
Hot Blogs
No Hot Blogs
Monthly Archive