home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / TUPLE.ICN < prev    next >
Text File  |  1991-07-13  |  2KB  |  64 lines

  1. ############################################################################
  2. #
  3. #   Name:    tuple.icn
  4. #
  5. #   Title:    Process n-tuples
  6. #
  7. #   Author:    William H. Mitchell
  8. #
  9. #   Date:    June 10, 1988
  10. #
  11. ############################################################################
  12. #
  13. #     This procedure implements a "tuple" feature that produces the effect
  14. #  of multiple keys.  A tuple is created by an expression of the
  15. #  form
  16. #
  17. #    tuple([exrp1, expr2, ..., exprn])
  18. #
  19. #  The result can be used in a case expression or as a table subscript.
  20. #  Lookup is successful provided the values of expr1, expr2, ..., exprn
  21. #  are the same (even if the lists containing them are not).  For example,
  22. #  consider selecting an operation based on the types of two operands.  The
  23. #  expression
  24. #
  25. #    case [type(op1), type(op2)] of  {
  26. #       ["integer", "integer"]:  op1 + op2
  27. #       ["string", "integer"] :  op1 || "+" || op2
  28. #       ["integer", "string"] :  op1 || "+" || op2
  29. #       ["string", "string"]  :  op1 || "+" || op2
  30. #       }
  31. #
  32. #  does not work, because the comparison in the case clauses compares lists
  33. #  values, which cannot be the same as control expression, because the lists
  34. #  are different, even though their contents are the same.  With tuples,
  35. #  however, the comparison succeeds, as in
  36. #
  37. #    case tuple([type(op1), type(op2)]) of {
  38. #       tuple(["integer", "integer"]):  op1 + op2
  39. #       tuple(["string", "integer"]) :  op1 || "+" || op2
  40. #       tuple(["integer", "string"]) :  op1 || "+" || op2
  41. #       tuple(["string", "string"])  :  op1 || "+" || op2
  42. #       }
  43. #
  44. ############################################################################
  45.  
  46. procedure tuple(tl)
  47.    local tb, i, e, le
  48.  
  49.    static tuptab
  50.    initial tuptab := table()    # create the root node
  51.  
  52.    /tuptab[*tl] := table()    # if there is no table for this size, make one
  53.    tb := tuptab[*tl]        # go to tuple for size of table
  54.    i := 0            # assign default value to i
  55.    every i := 1 to *tl - 1 do {    # iterate though all but last value
  56.       e := tl[i]        # ith value in tuple
  57.       /tb[e] := table()        # if it is not in the table, make a new one
  58.       tb := tb[e]        # go to table for that value
  59.       }
  60.    le := tl[i + 1]        # last value in tuple
  61.    /tb[le] := copy(tl)        # if it is new, entr a copy of the list
  62.    return tb[le]        # return the copy; it is unique
  63. end
  64.