Skip to content

Instantly share code, notes, and snippets.

@briantrice
Created April 12, 2011 22:44
Show Gist options
  • Save briantrice/916612 to your computer and use it in GitHub Desktop.
Save briantrice/916612 to your computer and use it in GitHub Desktop.
A version of Rubinius' CompactLookupTable for Slate
define: #CompactMapping &parents: {Collection. Mapping} &slots: {
#contents -> #{}
}.
m@(CompactMapping traits) new*
[| *rest |
m new `>> [contents := rest. ]
].
m@(CompactMapping traits) size
[m contents size // 2].
m@(CompactMapping traits) capacity
[m size].
m@(CompactMapping traits) keysDo: block
[
0 below: m contents size by: 2 do:
[| :index | block apply*, (m contents at: index)].
].
m@(CompactMapping traits) valuesDo: block
[
1 below: m contents size by: 2 do:
[| :index | block apply*, (m contents at: index)].
].
m@(CompactMapping traits) at: key ifAbsent: block
[
0 below: m contents size do:
[| :index | (m contents at: index) = key ifTrue:
[^ (m contents at: index + 1)]].
block do
].
m@(CompactMapping traits) at: key put: obj
[
0 below: m contents size do:
[| :index | (m contents at: index) = key ifTrue:
[^ (m contents at: index + 1 put: obj)]].
0 below: m contents size do:
[| :index | (m contents at: index) ifNil:
[m contents at: index put: key.
^ (m contents at: index + 1 put: obj)]].
error: 'This mapping cannot accept new key-value pairs.'
].
m@(Mapping traits) removeKey: key ifAbsent: block
[
0 below: m contents size do:
[| :index | (m contents at: index) = key ifTrue:
[m contents at: index put: Nil. m contents at: index + 1 put: Nil]].
block do
].
m@(Mapping traits) keyAtValue: obj ifAbsent: block
[
1 below: m contents size by: 2 do:
[| :index | (m contents at: index) = obj ifTrue:
[^ (m contents at: index - 1)]].
block do
].
m@(Mapping traits) keysAndValuesDo: block
[
0 below: m contents size by: 2 do:
[| :index | block apply*, (m contents at: index), (m contents at: index + 1)].
].
m@(Mapping traits) allSatisfy: block
[
m valuesDo: [| :value | (block apply*, value) ifFalse: [^ False]].
True
].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment