'
'  StringSet.lss
'
'  Time-stamp: <2003-03-24 23:25:12 Daniel Eriksson>
'
'  Date        Author           Changes
'  ----------  ---------------  ------------------------------------
'  2003-03-09  Daniel Eriksson  Created
'

Public Class StringSet
' A collection that contains no duplicate elements. You can
' use this data structure to eliminate doubles. Element comparison
' is case sensitive.
'
' Example:
'   Dim fruits As New StringSet
'   Print "Apple #1:", fruits.Add("Apple") ' True
'   Print "Banana #1:", fruits.Add("Banana") ' True
'   Print "Apple #2:", fruits.Add("Apple") ' False
'
'
  Private m_elements List As Integer
  Private m_count As Long
  
  Public Sub New
    m_count = 0
  End Sub
  
  Public Function Add(element As String) As Variant
  ' Adds element to the set if it is not already present.
  '
  ' Returns true if the set did not already contain 
  ' element; otherwise, returns false
    If Contains(element) Then
      Add = False
    Else
      m_elements(element) = 0
      m_count = m_count + 1
      Add = True
    End If
  End Function
  
  Public Function Contains(element As String) As Variant
  ' Checks if the set contains element.
  '
  ' Returns true if the set did not already contain 
  ' element; otherwise, returns false
    If Iselement(m_elements(element)) Then
      Contains = True
    Else
      Contains = False
    End If
  End Function
  
  Public Function Remove(element As String) As Variant
  ' Removes element from the set it is present.
  '
  ' Returns true if the set contained element; 
  ' otherwise, returns false.
    If Contains(element) Then
      m_count = m_count - 1
      Erase m_elements(element)
      Me.Remove = True
    Else
      Me.Remove = False
    End If
  End Function
  
  Public Property Get Size As Long
    Size = m_count
  End Property
  
  Public Function IsEmpty As Variant
    Me.IsEmpty = (m_count = 0)
  End Function
  
  Public Function Elements As Variant
    If m_count = 0 Then 
      Elements = Null
      Exit Function
    End If
    
    Dim elems() As String
    Redim elems(m_count - 1) As String
    
    Dim i As Long
    i = 0
    Forall e In m_elements
      elems(i) = Listtag(e)
      i = i + 1
    End Forall
    
    Elements = elems
  End Function
End Class