home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnusmalltalk / mappedcollection.st < prev    next >
Text File  |  1992-02-15  |  3KB  |  129 lines

  1. "======================================================================
  2. |
  3. |   MappedCollection Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         16 Mar 91      Class creation now separate statement.
  34. |
  35. | sbyrne     25 Apr 89      created.
  36. |
  37. "
  38.  
  39. Collection subclass: #MappedCollection
  40.        instanceVariableNames: 'domain map'
  41.        classVariableNames: ''
  42.        poolDictionaries: ''
  43.        category: nil
  44. !
  45.  
  46. MappedCollection comment: 
  47. 'I represent collections of objects that are indirectly indexed by names.
  48. There are really two collections involved: domain and a map.  The map maps
  49. between external names and indices into domain, which contains the
  50. real association.  In order to work properly, the domain and map objects must
  51. be instances of a subclass of SequenceableCollection or Dictionary. ' !
  52.  
  53. !MappedCollection class methodsFor: 'basic'!
  54.  
  55. collection: aCollection map: aMap
  56.     ^self new setCollection: aCollection andMap: aMap
  57. !
  58.  
  59. new
  60.     self error: 'new not available for MappedCollections; use collection:map:'
  61. !!
  62.  
  63.  
  64. !MappedCollection methodsFor: 'basic'!
  65.  
  66. at: key
  67.     ^domain at: (map at: key)
  68. !
  69.  
  70. at: key put: value
  71.     ^domain at: (map at: key) put: value
  72. !
  73.  
  74. contents
  75.     | contents |
  76.     contents _ Bag new.
  77.     map do: [ :domainKey | contents add: domain at: domainKey ].
  78.     ^contents
  79. !
  80.  
  81. size
  82.     ^domain size
  83. !
  84.  
  85. add: anObject
  86.     self shouldNotImplement
  87. !
  88.  
  89. contents
  90.     | aBag |
  91.     aBag _ Bag new.
  92.     map do: [ :value | aBag add: (domain at: value) ].
  93.     ^aBag
  94. !
  95.  
  96. do: aBlock
  97.     map do: [ :value | aBlock value: (domain at: value) ]
  98. !
  99.  
  100. collect: aBlock
  101.     | aStream |
  102.     aStream _ WriteStream on: (self species new: self size).
  103.     self do: [ :value | aStream nextPut: (aBlock value: value) ].
  104.     ^aStream contents
  105. !
  106.  
  107. select: aBlock
  108.     | aStream |
  109.     aStream _ WriteStream on: (self species new: self size).
  110.     self do: [ :value | (aBlock value: value) ifTrue:
  111.                 [ aStream nextPut: value ] ].
  112.     ^aStream contents
  113. !!
  114.  
  115.  
  116.  
  117. !MappedCollection methodsFor: 'private'!
  118.  
  119. setCollection: aCollection andMap: aMap
  120.     domain _ aCollection.
  121.     map _ aMap
  122. !
  123.  
  124. species
  125.     ^domain species
  126. !!
  127.