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

  1. "======================================================================
  2. |
  3. |   SortedCollection 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     19 Sep 89      Changed to use real method categories.
  36. |
  37. | sbyrne     25 Apr 89      created.
  38. |
  39. "
  40.  
  41. OrderedCollection variableSubclass: #SortedCollection
  42.           instanceVariableNames: 'sortBlock'
  43.           classVariableNames: ''
  44.           poolDictionaries: ''
  45.           category: nil
  46. !
  47.  
  48. SortedCollection comment:
  49. 'I am a collection of objects, stored and accessed according to some
  50. sorting criteria.  I store things using a bubble sort.  My instances have a
  51. comparison block associated with them; this block takes two arguments and
  52. is a predicate which returns true if the first argument should be sorted
  53. earlier than the second.  The default block is [ :a :b | a <= b ], but I
  54. will accept any block that conforms to the above criteria.' !
  55.  
  56. !SortedCollection class methodsFor: 'instance creation'!
  57.  
  58. new
  59.     ^self sortBlock: [ :a :b | a <= b ]
  60. !
  61.  
  62. sortBlock: aSortBlock
  63.     ^super new setSortBlock: aSortBlock
  64.  
  65. !!
  66.  
  67.  
  68.  
  69. !SortedCollection methodsFor: 'basic'!
  70.  
  71. addFirst: anObject
  72.     self shouldNotImplement
  73. !
  74.  
  75. addLast: anObject
  76.     self shouldNotImplement
  77. !
  78.  
  79. at: index put: anObject
  80.     self shouldNotImplement
  81. !
  82.  
  83. add: newObject after: oldObject
  84.     self shouldNotImplement
  85. !
  86.  
  87. add: newObject before: oldObject
  88.     self shouldNotImplement
  89. !
  90.  
  91. add: anObject
  92.     "Add anObject into the collection at the proper place using bubble sort."
  93.     "### not real happy with the way this is coded"
  94.     super addFirst: anObject.
  95.     firstIndex + 1 to: lastIndex do:
  96.         [ :i | (sortBlock value: (self basicAt: i)
  97.                       value: anObject)
  98.                   ifTrue: [ self basicAt: i - 1 put: (self basicAt: i) ]
  99.           ifFalse: [ self basicAt: i - 1 put: anObject.
  100.                      ^anObject ] ].
  101.     self basicAt: lastIndex put: anObject.
  102.     ^anObject
  103. !!
  104.  
  105.  
  106.  
  107. !SortedCollection methodsFor: 'instance protocol'!
  108. sortBlock
  109.     ^sortBlock
  110. !
  111.  
  112. sortBlock: aSortBlock
  113.     "Change the sort criteria for a sorted collection, resort the elements of 
  114.     the collection, and return it."
  115.     | newSortedCollection |
  116.     newSortedCollection _ SortedCollection sortBlock: aSortBlock.
  117.     self do: [ :element | newSortedCollection add: element ].
  118.     ^self become: newSortedCollection
  119. ! !
  120.  
  121.  
  122.  
  123. !SortedCollection methodsFor: 'copying'!
  124.  
  125. copyEmpty
  126.     ^(super copyEmpty) setSortBlock: sortBlock
  127. !!
  128.  
  129.  
  130.  
  131. !SortedCollection methodsFor: 'private methods'!
  132. setSortBlock: aSortBlock
  133.     sortBlock _ aSortBlock
  134. !
  135.  
  136. growTo: anInteger
  137.     | newCollection |
  138.     newCollection _ super growTo: anInteger.
  139.     newCollection setSortBlock: sortBlock.
  140.     ^newCollection
  141. ! !
  142.