Squeak Class Documentation category index | class index  
 
PluggableSet
  category: Collections-Unordered
  superclass: Set
  subclasses:

PluggableSets allow the redefinition of hashing and equality by clients. This is in particular useful if the clients know about specific properties of the objects stored in the set which in turn can heavily improve the performance of sets and dictionaries.

Instance variables:
hashBlock <BlockContext> A one argument block used for hashing the elements.
equalBlock <BlockContext> A two argument block used for comparing the elements.

Example: Adding 1000 integer points in the range (0@0) to: (100@100) to a set.

| rnd set max pt |
set _ Set new: 1000.
rnd _ Random new.
max _ 100.
Time millisecondsToRun:[
1 to: 1000 do:[:i|
pt _ (rnd next * max) truncated @ (rnd next * max) truncated.
set add: pt.
].
].

The above is way slow since the default hashing function of points leads to an awful lot of collisions in the set. And now the same, with a somewhat different hash function:

| rnd set max pt |
set _ PluggableSet new: 1000.
set hashBlock:[:item| (item x bitShift: 16) + item y].
rnd _ Random new.
max _ 100.
Time millisecondsToRun:[
1 to: 1000 do:[:i|
pt _ (rnd next * max) truncated @ (rnd next * max) truncated.
set add: pt.
].
].

instance methods
  accessing
  equalBlock
equalBlock:
hashBlock
hashBlock:

  copying
  copy
postCopyBlocks

  private
  scanFor:

class methods
  as yet unclassified
  integerSet

instance methods
  accessing top  
 

equalBlock

Return the block used for comparing the elements in the receiver.


 

equalBlock:

Set a new equality block. The block must accept two arguments and return true if the argumets are considered equal, false otherwise


 

hashBlock

Return the block used for hashing the elements in the receiver.


 

hashBlock:

Set a new hash block. The block must accept one argument and return the hash value of the given argument.


  copying top  
 

copy

Answer another instance just like the receiver. Subclasses typically
override this method; they typically do not override shallowCopy.


 

postCopyBlocks


  private top  
 

scanFor:

Scan the key array for the first slot containing either a nil
(indicating
an empty slot) or an element that matches anObject. Answer the index

of that slot or zero if no slot is found. This method will be
overridden
in various subclasses that have different interpretations for matching

elements.


class methods
  as yet unclassified top  
 

integerSet