SetOps, Contains

Purpose: Definition and discussion of set functionality, especially the function to check for membership of elements.

Back to the Chart of proposed set functionality.


::setops::contains set element

2 Arguments:

  • The set to check, as a tcl-list, by value.
  • The text of the element to check for membership in the set.

Return value:

  • A number, in the set {0, 1}. 0 = element is no member of the set. 1 = element is a member of the set.

Implementation:

 proc ::setops::contains {set element} {
    expr {[lsearch -exact $set $element] >= 0}
    #expr {[lsearch -exact $set $element] < 0 ? 0 : 1}
 }
Speed
O(n). n = Length of the list representing the set.

C implementation using a hash table:

Speed
Basically O(1), O(n) in worst-case = transformation of a string or list into the hash-table representation.

-- AK