'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
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
BlogSphere V1.2 Beta 3
Join The WebLog Revolution at BlogSphere.net