home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / bag < prev    next >
Text File  |  2020-01-01  |  11KB  |  300 lines

  1. ; From: Dat Thuc Nguyen
  2. ; Date: Wed, 7 Apr 1999 15:33:30 +0000
  3. ; URL: http://www.smalltickle.com
  4. ;
  5. ; CONTAINER IS A KEY CONCEPT IN OBJECT-ORIENTED PROGRAMMING.
  6. ; SMALLTALK, C++, etc. HAVE STANDARD LIBRARIES OF CONTAINERS.
  7. ; THE FOLLOWING SCRIPT DEFINES THE CLASS BAG IN C-KERMIT/KERMIT 95,
  8. ; BAG OFFERS A RICH USAGE INTERFACE:
  9. ;
  10. ; bag bag_name
  11. ;   create a new object identified as bag_name.
  12. ;
  13. ; bag_name.put key value
  14. ;   deposit into the object bag_name the element
  15. ;   identified by 'key' that has the content 'value'.
  16. ;   Both key and value can be alpha, numeric, alpha-numeric.
  17. ;
  18. ; bag_name.get key
  19. ;   query the element identified by 'key'.
  20. ;
  21. ; bag_name.size
  22. ;   query the number of the elements in the bag_name.
  23. ;
  24. ; bag_name.pairs
  25. ;   query all the pairs key => value in the bag_name.
  26. ;
  27. ; bag_name.values
  28. ;   query all the values in the bag_name.
  29. ;
  30. ; bag_name.keys
  31. ;   query all the keys in the bag_name.
  32. ;
  33. ; bag_name.reset
  34. ;   remove all the elements in the bag_name.
  35. ;
  36. ; bag_name.every_value dothis
  37. ;   apply dothis to every value of bag_name.
  38. ;
  39. ; bag_name.every_key dothis
  40. ;   apply dothis to every key of bag_name.
  41. ;
  42. ; bag_name.drop key
  43. ;   remove element identified by key from bag_name.
  44. ;
  45. ; bag_name.destroy
  46. ;   remove the bag_name itself.
  47.  
  48. ;************************************************************************
  49. ;*      SERVICE IMPLEMENTATION TO ALL OBJECTS OF CLASS BAG              *
  50. ;*      TO REDUCE THE CODE DUPLICATION FOR EACH OBJECT                  *
  51. ;************************************************************************
  52.  
  53. define bag_service.reset {
  54.     _assign \%1_size 0
  55.     _assign \%1_dictionary \x02
  56. }
  57.  
  58. define bag_service.put {
  59.     local \%f \%i \%l \%m \%n \%v
  60.  
  61.     assign \%m \x02        ; STX
  62.     assign \%n \x03        ; ETX
  63.  
  64.     assign \%f \findex(\%m\%2\%n, \m(\%1_dictionary))
  65.  
  66.     xif = \%f 0 {                ; key not yet used?
  67.         _assign \%1_dictionary \m(\%1_dictionary)\%2\%n\%3\%m
  68.         assign \%z \m(\%1_size)  ; get previous size
  69.         increment \%z            ; update size
  70.         _assign \%1_size \%z     ; save size
  71.     } else {
  72.         assign \%i \feval(\%f + \flength(\%m\%2\%n))
  73.         assign \%f \findex(\%m, \m(\%1_dictionary), \%i)
  74.         assign \%l \feval(\%f - \%i)
  75.         assign \%v \fsubstring(\m(\%1_dictionary),\%i,\%l)
  76.         _assign \%1_dictionary -
  77.           \freplace(\m(\%1_dictionary),\%n\%v\%m,\%n\%3\%m)
  78.     }
  79. }
  80.  
  81. define bag_service.get {
  82.     local \%f \%i \%v \%l \%m \%n
  83.     assign \%m \x02              ; STX
  84.     assign \%n \x03              ; ETX
  85.     assign \%f \findex(\%m\%2\%n,\m(\%1_dictionary))
  86.     xif = \%f 0 {
  87.         echo \%2 IS UNKNOWN KEY IN THIS OBJECT
  88.     } else {
  89.         assign \%i \feval(\%f + \flength(\%m\%2\%n))
  90.         assign \%f \findex(\%m, \m(\%1_dictionary), \%i)
  91.         assign \%l \feval(\%f - \%i)
  92.         assign \%v \fsubstring(\m(\%1_dictionary),\%i,\%l)
  93.         echo \%2 => \%v
  94.     }
  95. }
  96.  
  97. define bag_service.pairs {
  98.     local \%f \%i \%k \%l \%m \%n \%v
  99.     assign \%m \x02        ; STX
  100.     assign \%n \x03        ; ETX
  101.     assign \%i \feval(1 + \flength(\%m))                     ; start dictionary
  102.     while < \%i \flength(\m(\%1_dictionary)) {
  103.         assign \%f \findex(\%n, \m(\%1_dictionary), \%i)     ; key pos
  104.         assign \%l \feval(\%f - \%i)                         ; key length
  105.         assign \%k \fsubstring(\m(\%1_dictionary), \%i, \%l) ; key
  106.         assign \%i \feval(\%f + \flength(\%n))
  107.         assign \%f \findex(\%m, \m(\%1_dictionary), \%i)     ; value pos
  108.         assign \%l \feval(\%f - \%i)                         ; value length
  109.         assign \%v \fsubstring(\m(\%1_dictionary), \%i, \%l) ; value
  110.         echo \%k => \%v
  111.         assign \%i \feval(\%f + \flength(\%m))               ; next key pos
  112.     }
  113. }
  114.  
  115. define bag_service.keys {
  116.     local \%f \%i \%k \%l \%m \%n
  117.     assign \%m \x02        ; STX
  118.     assign \%n \x03        ; ETX
  119.     assign \%i \feval(1 + \flength(\%m))                     ; start dictionary
  120.     while < \%i \flength(\m(\%1_dictionary)) {
  121.         assign \%f \findex(\%n, \m(\%1_dictionary), \%i)     ; key pos
  122.         assign \%l \feval(\%f - \%i)                         ; key length
  123.         assign \%k \fsubstring(\m(\%1_dictionary), \%i, \%l) ; key
  124.         assign \%i \feval(\%f + \flength(\%n))
  125.         assign \%f \findex(\%m, \m(\%1_dictionary), \%i)     ; value pos
  126.         assign \%l \feval(\%f - \%i)                         ; value length
  127.         echo \%k
  128.         assign \%i \feval(\%f + \flength(\%m))               ; next key pos
  129.     }
  130. }
  131.  
  132. define bag_service.values {
  133.     local \%f \%i \%k \%l \%m \%n \%v
  134.     assign \%m \x02        ; STX
  135.     assign \%n \x03        ; ETX
  136.     assign \%i \feval(1 + \flength(\%m))                     ; start dictionary
  137.     while < \%i \flength(\m(\%1_dictionary)) {
  138.         assign \%f \findex(\%n, \m(\%1_dictionary), \%i)     ; key pos
  139.         assign \%l \feval(\%f - \%i) ; key length
  140.         assign \%i \feval(\%f + \flength(\%n))
  141.         assign \%f \findex(\%m, \m(\%1_dictionary), \%i)     ; value pos
  142.         assign \%l \feval(\%f - \%i)                         ; value length
  143.         assign \%v \fsubstring(\m(\%1_dictionary), \%i, \%l) ; value
  144.         echo \%v
  145.         assign \%i \feval(\%f + \flength(\%m))               ; next key pos
  146.     }
  147. }
  148.  
  149. define bag_service.every_value {
  150.     local \%f \%i \%k \%l \%m \%n \%v
  151.     assign \%m \x02        ; STX
  152.     assign \%n \x03        ; ETX
  153.     assign \%i \feval(1 + \flength(\%m))                     ; start dictionary
  154.     while < \%i \flength(\m(\%1_dictionary)) {
  155.         assign \%f \findex(\%n, \m(\%1_dictionary), \%i)     ; key pos
  156.         assign \%l \feval(\%f - \%i)                         ; key length
  157.         assign \%i \feval(\%f + \flength(\%n))
  158.         assign \%f \findex(\%m, \m(\%1_dictionary), \%i)     ; value pos
  159.         assign \%l \feval(\%f - \%i)                         ; value length
  160.         assign \%v \fsubstring(\m(\%1_dictionary), \%i, \%l) ; value
  161.         \%2 \%v                                              ; apply \%2 on val
  162.         assign \%i \feval(\%f + \flength(\%m))               ; next key pos
  163.     }
  164. }
  165. define bag_service.every_key {
  166.     local \%f \%i \%k \%l \%m \%n \%v
  167.     assign \%m \x02        ; STX
  168.     assign \%n \x03        ; ETX
  169.     assign \%i \feval(1 + \flength(\%m))                     ; start dictionary
  170.     while < \%i \flength(\m(\%1_dictionary)) {
  171.         assign \%f \findex(\%n, \m(\%1_dictionary), \%i)     ; key pos
  172.         assign \%l \feval(\%f - \%i)                         ; key length
  173.         assign \%k \fsubstring(\m(\%1_dictionary), \%i, \%l) ; key
  174.         assign \%i \feval(\%f + \flength(\%n))
  175.         assign \%f \findex(\%m, \m(\%1_dictionary), \%i)     ; value pos
  176.         assign \%l \feval(\%f - \%i)                         ; value length
  177.         \%2 \%k                                              ; apply \%2 on key
  178.         assign \%i \feval(\%f + \flength(\%m))               ; next key pos
  179.     }
  180. }
  181. define bag_service.drop {
  182.     local \%f \%i \%k \%l \%m \%n \%v \%z
  183.     assign \%m \x02        ; STX
  184.     assign \%n \x03        ; ETX
  185.     assign \%f \findex(\%m\%2\%n, \m(\%1_dictionary))         ; get key
  186.     xif = \%f 0 {                                             ; not yet used?
  187.         echo \%2 IS UNKNOWN KEY IN THIS OBJECT
  188.     } else {
  189.         assign \%i \feval(\%f + \flength(\%m\%2\%n))
  190.         assign \%f \findex(\%m, \m(\%1_dictionary), \%i)
  191.         assign \%l \feval(\%f - \%i)
  192.         assign \%v \fsubstring(\m(\%1_dictionary),\%i,\%l)
  193.         _assign \%1_dictionary -
  194.           \freplace(\m(\%1_dictionary),\%m\%2\%n\%v)
  195.         assign \%z \m(\%1_size)                               ; previous size
  196.         increment \%z -1                                      ; update size
  197.         _assign \%1_size \%z                                  ; save size
  198.     }
  199. }
  200.  
  201. define bag_service.destroy {
  202.     \%1.reset
  203.     _define \%1_size
  204.     _define \%1_dictionary
  205.     _define \%1.size
  206.     _define \%1.put
  207.     _define \%1.get
  208.     _define \%1.pairs
  209.     _define \%1.reset
  210.     _define \%1.keys
  211.     _define \%1.values
  212.     _define \%1.every_value
  213.     _define \%1.every_key
  214.     _define \%1.drop
  215.     _define \%1.destroy
  216. }
  217.  
  218. define bag {
  219.     _assign \%1_dictionary \x02    ; STX
  220.     _assign \%1_size 0
  221.     _define \%1.put {
  222.         if NOT define \%1 end 1 ... missing 1.param
  223.         if NOT define \%2 end 1 ... missing 2.param
  224.         bag_service.put \fbreak(\v(macro),.) \%1 \%2
  225.     }
  226.     _define \%1.get {
  227.         bag_service.get \fbreak(\v(macro),.) \%1
  228.     }
  229.     _define \%1.pairs {
  230.         bag_service.pairs \fbreak(\v(macro),.)
  231.     }
  232.     _define \%1.size {
  233.         local \%s
  234.         assign \%s \fbreak(\v(macro),.)
  235.         echo \m(\%s_size)
  236.         return \m(\%s_size)
  237.     }
  238.     _define \%1.reset {
  239.         bag_service.reset \fbreak(\v(macro),.)
  240.     }
  241.     _define \%1.keys {
  242.         bag_service.keys \fbreak(\v(macro),.)
  243.     }
  244.     _define \%1.values {
  245.         bag_service.values \fbreak(\v(macro),.)
  246.     }
  247.     _define \%1.every_value {
  248.         bag_service.every_value \fbreak(\v(macro),.) \%1
  249.     }
  250.     _define \%1.every_key {
  251.         bag_service.every_key \fbreak(\v(macro),.) \%1
  252.     }
  253.     _define \%1.drop {
  254.         bag_service.drop \fbreak(\v(macro),.) \%1
  255.     }
  256.     _define \%1.destroy {
  257.         bag_service.destroy \fbreak(\v(macro),.)
  258.     }
  259. }
  260.  
  261. ;************************************************************************
  262. ;*    USAGE EXAMPLE                                                     *
  263. ;************************************************************************
  264.  
  265. bag abc                   ; Buy a new bag abc
  266. abc.put a 1               ; Put the key a with the value 1 in the bag abc
  267. abc.put b 2               ; Put the key b with the value 2 in the bag abc
  268. abc.put c 3               ; Put the key c with the value 3 in the bag abc
  269. abc.pairs                 ; Show me all the pairs key => value
  270. abc.keys                  ; Show me all the keys
  271. abc.values                ; Show me all the value
  272.  
  273. define sum 0
  274. define sum_it {
  275.     assign sum \feval(\%1 + \m(sum)) ; sum ::= \%1 + \m(sum) <-- C-Kermit 7.0
  276. }
  277. abc.every_value sum_it    ; Add all the value in the bag abc
  278. echo \m(sum)              ; Show the sum
  279.  
  280. abc.put name Jordan       ; Put the key name with a value Jordan in the bag abc
  281. abc.pairs                 ; Show me all the pairs key => value
  282.  
  283. abc.put 1999 Year         ; Put the key 1999 with the value Year in the bag abc
  284. abc.pairs                 ; Show me all the pairs key => value
  285.  
  286. abc.put book Buch         ; English - German
  287. abc.put Buch book         ; German - English
  288.  
  289. abc.drop name             ; Jordan retires
  290. abc.pairs                 ; Show me all the pairs key => value
  291.  
  292. abc.reset                 ; empty the bag abc
  293.  
  294. for \%i 1 50 1 { abc.put a\%i b\%i }
  295. abc.pairs                 ; Show me all the pairs key => value
  296.  
  297. abc.destroy               ; Throw the bag abc into the gabbage
  298.  
  299. end
  300.