2017-01-06 09:53:40 +00:00
|
|
|
|
|
|
|
class Collection(Object)
|
2015-10-08 14:26:04 +00:00
|
|
|
{
|
2017-03-08 13:53:41 +00:00
|
|
|
method isEmpty
|
|
|
|
{
|
|
|
|
^self size <= 0
|
|
|
|
}
|
|
|
|
|
|
|
|
method notEmpty
|
|
|
|
{
|
|
|
|
^self size > 0
|
|
|
|
}
|
2017-09-21 07:56:51 +00:00
|
|
|
|
2017-03-08 13:53:41 +00:00
|
|
|
method size
|
|
|
|
{
|
|
|
|
(* Each subclass must override this method because
|
|
|
|
* it interates over the all elements for counting *)
|
|
|
|
| count |
|
|
|
|
count := 0.
|
|
|
|
self do: [ :el | count := count + 1 ].
|
|
|
|
^count
|
|
|
|
}
|
2017-09-21 07:56:51 +00:00
|
|
|
|
2017-03-08 13:53:41 +00:00
|
|
|
method do: block
|
|
|
|
{
|
|
|
|
^self subclassResponsibility: #do
|
|
|
|
}
|
2017-09-21 07:56:51 +00:00
|
|
|
|
2017-03-08 13:53:41 +00:00
|
|
|
method detect: block
|
|
|
|
{
|
|
|
|
self do: [ :el | if (block value: el) { ^el } ].
|
2017-03-08 14:48:12 +00:00
|
|
|
^Error.Code.ENOENT
|
2017-03-08 13:53:41 +00:00
|
|
|
}
|
2017-09-21 07:56:51 +00:00
|
|
|
|
2017-03-08 13:53:41 +00:00
|
|
|
method detect: block ifNone: exception_block
|
|
|
|
{
|
|
|
|
self do: [ :el | if (block value: el) { ^el } ].
|
|
|
|
^exception_block value.
|
|
|
|
}
|
2015-10-08 14:26:04 +00:00
|
|
|
}
|
2016-05-13 15:10:34 +00:00
|
|
|
|
|
|
|
## -------------------------------------------------------------------------------
|
2017-01-06 09:53:40 +00:00
|
|
|
class(#pointer) Array(Collection)
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
2017-01-06 09:53:40 +00:00
|
|
|
method size
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
2017-01-06 09:53:40 +00:00
|
|
|
^self basicSize
|
2016-05-13 15:10:34 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method at: anInteger
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
|
|
|
^self basicAt: anInteger.
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method at: anInteger put: aValue
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
|
|
|
^self basicAt: anInteger put: aValue.
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method first
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
|
|
|
^self at: 0.
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method last
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
2017-10-08 15:40:32 +00:00
|
|
|
^self at: (self size - 1).
|
2016-05-13 15:10:34 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method do: aBlock
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
2017-10-08 15:40:32 +00:00
|
|
|
0 priorTo: (self size) do: [:i | aBlock value: (self at: i)].
|
2016-05-13 15:10:34 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method copy: anArray
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
2017-10-08 15:40:32 +00:00
|
|
|
0 priorTo: (anArray size) do: [:i | self at: i put: (anArray at: i) ].
|
|
|
|
}
|
|
|
|
|
|
|
|
method copy: anArray from: start to: end
|
|
|
|
{
|
|
|
|
## copy elements from an array 'anArray' starting from
|
|
|
|
## the index 'start' to the index 'end'.
|
|
|
|
|
|
|
|
| s i ss |
|
|
|
|
|
|
|
|
(*
|
|
|
|
s := anArray size.
|
|
|
|
|
|
|
|
if (start < 0) { start := 0 }
|
|
|
|
elsif (start >= s) { start := s - 1 }.
|
|
|
|
|
|
|
|
if (end < 0) { end := 0 }
|
|
|
|
elsif (end >= s) { end := s - 1 }.
|
|
|
|
*)
|
|
|
|
i := 0.
|
|
|
|
ss := self size.
|
|
|
|
while (start <= end)
|
|
|
|
{
|
|
|
|
if (i >= ss) { break }.
|
|
|
|
self at: i put: (anArray at: start).
|
|
|
|
i := i + 1.
|
|
|
|
start := start + 1.
|
|
|
|
}.
|
|
|
|
}
|
|
|
|
|
|
|
|
method copyFrom: start to: end
|
|
|
|
{
|
|
|
|
## returns a copy of the receiver starting from the element
|
|
|
|
## at index 'start' to the element at index 'end'.
|
|
|
|
|
|
|
|
| newsz |
|
|
|
|
newsz := end - start + 1.
|
|
|
|
^(self class new: newsz) copy: self from: start to: end
|
2016-05-13 15:10:34 +00:00
|
|
|
}
|
2017-10-02 01:22:49 +00:00
|
|
|
|
|
|
|
method = anArray
|
|
|
|
{
|
|
|
|
| size i |
|
|
|
|
if (self class ~~ anArray class) { ^false }.
|
|
|
|
|
|
|
|
size := self size.
|
|
|
|
if (size ~~ anArray size) { ^false }.
|
|
|
|
|
|
|
|
i := 0.
|
|
|
|
while (i < size)
|
|
|
|
{
|
|
|
|
if ((self at: i) ~= (anArray at: i)) { ^false }.
|
|
|
|
i := i + 1.
|
|
|
|
}.
|
|
|
|
|
|
|
|
^true.
|
|
|
|
}
|
|
|
|
|
|
|
|
method ~= anArray
|
|
|
|
{
|
|
|
|
^(self = anArray) not
|
|
|
|
}
|
2016-05-13 15:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
## -------------------------------------------------------------------------------
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
class(#character) String(Array)
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
2017-01-06 09:53:40 +00:00
|
|
|
method & string
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
2017-01-05 10:16:04 +00:00
|
|
|
(* TOOD: make this a primitive for performance. *)
|
|
|
|
|
|
|
|
(* concatenate two strings. *)
|
|
|
|
| newsize newstr cursize appsize |
|
2016-06-22 03:23:14 +00:00
|
|
|
|
2017-01-05 10:16:04 +00:00
|
|
|
cursize := self basicSize.
|
|
|
|
appsize := string basicSize.
|
|
|
|
newsize := cursize + appsize.
|
|
|
|
(*newstr := self class basicNew: newsize.*)
|
|
|
|
newstr := String basicNew: newsize.
|
2016-06-22 03:23:14 +00:00
|
|
|
|
2017-01-05 10:16:04 +00:00
|
|
|
0 priorTo: cursize do: [:i | newstr at: i put: (self at: i) ].
|
|
|
|
0 priorTo: appsize do: [:i | newstr at: (i + cursize) put: (string at: i) ].
|
2016-06-22 03:23:14 +00:00
|
|
|
|
2016-05-13 15:10:34 +00:00
|
|
|
^newstr
|
|
|
|
}
|
2017-09-21 07:56:51 +00:00
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method asString
|
2017-01-05 10:16:04 +00:00
|
|
|
{
|
|
|
|
^self
|
|
|
|
}
|
2017-09-21 07:56:51 +00:00
|
|
|
|
2017-05-01 12:54:41 +00:00
|
|
|
(* TODO: Symbol is a #final class. Symbol new is not allowed. To create a symbol programatically, you should
|
|
|
|
* build a string and send asSymbol to the string............
|
|
|
|
method asSymbol
|
|
|
|
{
|
|
|
|
}
|
|
|
|
*)
|
2017-05-16 02:04:18 +00:00
|
|
|
|
|
|
|
(* The strlen method returns the number of characters before a terminating null.
|
|
|
|
* if no terminating null character exists, it returns the same value as the size method *)
|
|
|
|
method(#primitive,#lenient) _strlen.
|
|
|
|
method(#primitive) strlen.
|
2016-05-13 15:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
## -------------------------------------------------------------------------------
|
|
|
|
|
2017-05-07 05:18:21 +00:00
|
|
|
class(#character,#final,#limited,#immutable) Symbol(String)
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
2017-01-06 09:53:40 +00:00
|
|
|
method asString
|
2017-01-05 10:16:04 +00:00
|
|
|
{
|
|
|
|
(* TODO: make this a primitive for performance *)
|
|
|
|
|
|
|
|
(* convert a symbol to a string *)
|
2017-02-07 18:09:07 +00:00
|
|
|
| size str i |
|
2017-01-05 10:16:04 +00:00
|
|
|
size := self basicSize.
|
|
|
|
str := String basicNew: size.
|
2017-02-07 18:09:07 +00:00
|
|
|
|
|
|
|
##0 priorTo: size do: [:i | str at: i put: (self at: i) ].
|
|
|
|
i := 0.
|
|
|
|
while (i < size)
|
|
|
|
{
|
|
|
|
str at: i put: (self at: i).
|
|
|
|
i := i + 1
|
|
|
|
}.
|
2017-01-05 10:16:04 +00:00
|
|
|
^str.
|
|
|
|
}
|
2017-01-06 13:27:49 +00:00
|
|
|
|
|
|
|
method = anObject
|
|
|
|
{
|
|
|
|
(* for a symbol, equality check is the same as the identity check *)
|
2017-12-03 17:08:04 +00:00
|
|
|
<primitive: #'Apex_=='>
|
2017-01-06 13:27:49 +00:00
|
|
|
self primitiveFailed.
|
|
|
|
}
|
|
|
|
|
|
|
|
method ~= anObject
|
|
|
|
{
|
|
|
|
(* for a symbol, equality check is the same as the identity check *)
|
2017-12-03 17:08:04 +00:00
|
|
|
<primitive: #'Apex_~~'>
|
2017-01-06 13:27:49 +00:00
|
|
|
^(self == anObject) not.
|
|
|
|
}
|
2016-05-13 15:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
## -------------------------------------------------------------------------------
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
class(#byte) ByteArray(Collection)
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
2017-01-06 09:53:40 +00:00
|
|
|
method at: anInteger
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
|
|
|
^self basicAt: anInteger.
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method at: anInteger put: aValue
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
|
|
|
^self basicAt: anInteger put: aValue.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
## -------------------------------------------------------------------------------
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
class Set(Collection)
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
2017-04-19 16:46:44 +00:00
|
|
|
var tally, bucket.
|
2017-01-09 13:20:46 +00:00
|
|
|
|
2017-02-10 07:38:29 +00:00
|
|
|
method(#class) new: size
|
2017-01-09 13:20:46 +00:00
|
|
|
{
|
|
|
|
^self new initialize: size.
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method initialize
|
2017-01-05 10:16:04 +00:00
|
|
|
{
|
2017-01-09 13:20:46 +00:00
|
|
|
^self initialize: 128. (* TODO: default initial size *)
|
|
|
|
}
|
|
|
|
|
|
|
|
method initialize: size
|
|
|
|
{
|
2017-02-07 18:09:07 +00:00
|
|
|
if (size <= 0) { size := 2 }.
|
2017-01-05 10:16:04 +00:00
|
|
|
self.tally := 0.
|
2017-01-09 13:20:46 +00:00
|
|
|
self.bucket := Array new: size.
|
2017-01-05 10:16:04 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method size
|
2017-01-05 10:16:04 +00:00
|
|
|
{
|
|
|
|
^self.tally
|
|
|
|
}
|
|
|
|
|
2017-02-07 17:40:34 +00:00
|
|
|
method __make_expanded_bucket: bs
|
|
|
|
{
|
|
|
|
| newbuc newsz ass index |
|
|
|
|
|
|
|
|
(* expand the bucket *)
|
|
|
|
newsz := bs + 128. (* TODO: keep this growth policy in sync with VM(dic.c) *)
|
|
|
|
newbuc := Array new: newsz.
|
|
|
|
0 priorTo: bs do: [:i |
|
|
|
|
ass := self.bucket at: i.
|
2017-02-07 18:09:07 +00:00
|
|
|
if (ass notNil)
|
|
|
|
{
|
2017-02-07 17:40:34 +00:00
|
|
|
index := (ass key hash) rem: newsz.
|
2017-02-07 18:09:07 +00:00
|
|
|
while ((newbuc at: index) notNil) { index := (index + 1) rem: newsz }.
|
2017-02-07 17:40:34 +00:00
|
|
|
newbuc at: index put: ass
|
2017-02-07 18:09:07 +00:00
|
|
|
}.
|
2017-02-07 17:40:34 +00:00
|
|
|
].
|
|
|
|
|
|
|
|
^newbuc.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method __find: key or_upsert: upsert with: value
|
2017-01-05 10:16:04 +00:00
|
|
|
{
|
|
|
|
| hv ass bs index ntally |
|
|
|
|
|
|
|
|
bs := self.bucket size.
|
|
|
|
hv := key hash.
|
|
|
|
index := hv rem: bs.
|
|
|
|
|
2017-02-07 18:09:07 +00:00
|
|
|
while ((ass := self.bucket at: index) notNil)
|
|
|
|
{
|
|
|
|
if (key = ass key)
|
|
|
|
{
|
|
|
|
(* found *)
|
|
|
|
if (upsert) { ass value: value }.
|
|
|
|
^ass
|
|
|
|
}.
|
|
|
|
index := (index + 1) rem: bs.
|
|
|
|
}.
|
2017-01-05 10:16:04 +00:00
|
|
|
|
2017-02-07 17:40:34 +00:00
|
|
|
##upsert ifFalse: [^ErrorCode.NOENT].
|
2017-03-08 14:48:12 +00:00
|
|
|
if (upsert) {} else { ^Error.Code.ENOENT }.
|
2017-01-05 10:16:04 +00:00
|
|
|
|
|
|
|
ntally := self.tally + 1.
|
2017-02-07 17:40:34 +00:00
|
|
|
if (ntally >= bs)
|
|
|
|
{
|
|
|
|
self.bucket := self __make_expanded_bucket: bs.
|
2017-01-06 14:49:42 +00:00
|
|
|
bs := self.bucket size.
|
|
|
|
index := hv rem: bs.
|
2017-02-07 18:09:07 +00:00
|
|
|
while ((self.bucket at: index) notNil) { index := (index + 1) rem: bs }.
|
2017-02-07 17:40:34 +00:00
|
|
|
}.
|
2017-09-21 07:56:51 +00:00
|
|
|
|
2017-01-05 10:16:04 +00:00
|
|
|
ass := Association key: key value: value.
|
|
|
|
self.tally := ntally.
|
|
|
|
self.bucket at: index put: ass.
|
|
|
|
|
|
|
|
^ass
|
|
|
|
}
|
|
|
|
|
2017-01-06 14:49:42 +00:00
|
|
|
method at: key
|
|
|
|
{
|
|
|
|
| ass |
|
|
|
|
ass := self __find: key or_upsert: false with: nil.
|
2017-02-07 18:09:07 +00:00
|
|
|
if (ass isError) { ^ass }.
|
2017-01-09 09:11:36 +00:00
|
|
|
^ass value
|
2017-01-06 14:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
method at: key ifAbsent: error_block
|
|
|
|
{
|
|
|
|
| ass |
|
|
|
|
ass := self __find: key or_upsert: false with: nil.
|
2017-02-07 18:09:07 +00:00
|
|
|
if (ass isError) { ^error_block value }.
|
2017-01-09 09:11:36 +00:00
|
|
|
^ass value
|
|
|
|
}
|
|
|
|
|
2017-02-07 17:40:34 +00:00
|
|
|
method associationAt: assoc
|
2017-01-09 09:11:36 +00:00
|
|
|
{
|
2017-02-07 17:40:34 +00:00
|
|
|
^self __find: (assoc key) or_upsert: false with: nil.
|
2017-01-09 09:11:36 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 17:40:34 +00:00
|
|
|
method associationAt: assoc ifAbsent: error_block
|
2017-01-09 09:11:36 +00:00
|
|
|
{
|
|
|
|
| ass |
|
2017-02-07 17:40:34 +00:00
|
|
|
ass := self __find: (assoc key) or_upsert: false with: nil.
|
2017-02-07 18:09:07 +00:00
|
|
|
if (ass isError) { ^error_block value }.
|
2017-01-09 09:11:36 +00:00
|
|
|
^ass
|
2017-01-06 14:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
method at: key put: value
|
|
|
|
{
|
2017-01-09 09:11:36 +00:00
|
|
|
(* returns the affected/inserted association *)
|
|
|
|
^self __find: key or_upsert: true with: value.
|
2017-01-06 14:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
method includesKey: key
|
|
|
|
{
|
|
|
|
| ass |
|
|
|
|
ass := self __find: key or_upsert: false with: nil.
|
2017-01-09 09:11:36 +00:00
|
|
|
^ass notError
|
2017-01-06 14:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
method includesAssociation: assoc
|
|
|
|
{
|
|
|
|
| ass |
|
|
|
|
ass := self __find: (assoc key) or_upsert: false with: nil.
|
|
|
|
^ass = assoc.
|
|
|
|
}
|
|
|
|
|
|
|
|
method includesKey: key value: value
|
|
|
|
{
|
|
|
|
| ass |
|
|
|
|
ass := self __find: key or_upsert: false with: nil.
|
|
|
|
^ass key = key and: [ass value = value]
|
|
|
|
}
|
2017-01-09 09:11:36 +00:00
|
|
|
|
|
|
|
method __find_index: key
|
|
|
|
{
|
|
|
|
| bs ass index |
|
|
|
|
|
|
|
|
bs := self.bucket size.
|
|
|
|
index := (key hash) rem: bs.
|
|
|
|
|
2017-02-07 18:09:07 +00:00
|
|
|
while ((ass := self.bucket at: index) notNil)
|
|
|
|
{
|
|
|
|
if (key = ass key) { ^index }.
|
|
|
|
index := (index + 1) rem: bs.
|
|
|
|
}.
|
2017-01-09 09:11:36 +00:00
|
|
|
|
2017-03-08 14:48:12 +00:00
|
|
|
^Error.Code.ENOENT.
|
2017-01-09 09:11:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
method __remove_at: index
|
|
|
|
{
|
2017-03-19 14:18:37 +00:00
|
|
|
| bs x y i v ass z |
|
2017-01-09 09:11:36 +00:00
|
|
|
|
|
|
|
bs := self.bucket size.
|
2017-03-19 14:18:37 +00:00
|
|
|
v := self.bucket at: index.
|
2017-01-09 09:11:36 +00:00
|
|
|
|
|
|
|
x := index.
|
|
|
|
y := index.
|
|
|
|
i := 0.
|
2017-03-19 14:18:37 +00:00
|
|
|
while (i < self.tally)
|
|
|
|
{
|
2017-01-09 09:11:36 +00:00
|
|
|
y := (y + 1) rem: bs.
|
|
|
|
|
2017-03-19 14:18:37 +00:00
|
|
|
ass := self.bucket at: y.
|
|
|
|
if (ass isNil) { (* done. the slot at the current index is nil *) break }.
|
|
|
|
|
|
|
|
(* get the natural hash index *)
|
|
|
|
z := (ass key hash) rem: bs.
|
2017-02-07 18:09:07 +00:00
|
|
|
|
2017-03-19 14:18:37 +00:00
|
|
|
(* move an element if necessary *)
|
|
|
|
if ((y > x and: [(z <= x) or: [z > y]]) or: [(y < x) and: [(z <= x) and: [z > y]]])
|
|
|
|
{
|
|
|
|
self.bucket at: x put: (self.bucket at: y).
|
|
|
|
x := y.
|
2017-02-07 18:09:07 +00:00
|
|
|
}.
|
2017-03-19 14:18:37 +00:00
|
|
|
|
|
|
|
i := i + 1.
|
|
|
|
}.
|
2017-01-09 09:11:36 +00:00
|
|
|
|
|
|
|
self.bucket at: x put: nil.
|
|
|
|
self.tally := self.tally - 1.
|
|
|
|
|
|
|
|
(* return the affected association *)
|
|
|
|
^v
|
|
|
|
}
|
|
|
|
|
2017-01-06 14:49:42 +00:00
|
|
|
method removeKey: key
|
|
|
|
{
|
2017-01-09 09:11:36 +00:00
|
|
|
| index |
|
|
|
|
index := self __find_index: key.
|
2017-02-07 18:09:07 +00:00
|
|
|
if (index isError) { ^index }.
|
2017-01-09 09:11:36 +00:00
|
|
|
^self __remove_at: index.
|
2017-01-06 14:49:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
method removeKey: key ifAbsent: error_block
|
|
|
|
{
|
2017-01-09 09:11:36 +00:00
|
|
|
| index |
|
|
|
|
index := self __find_index: key.
|
2017-02-07 18:09:07 +00:00
|
|
|
if (index isError) { ^error_block value }.
|
2017-01-09 09:11:36 +00:00
|
|
|
^self __remove_at: index.
|
2017-01-06 14:49:42 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:11:36 +00:00
|
|
|
method removeAllKeys
|
|
|
|
{
|
|
|
|
(* remove all items from a dictionary *)
|
|
|
|
| bs |
|
|
|
|
bs := self.bucket size.
|
|
|
|
0 priorTo: bs do: [:i | self.bucket at: i put: nil ].
|
|
|
|
self.tally := 0
|
|
|
|
}
|
|
|
|
|
|
|
|
(* TODO: ... keys is an array of keys.
|
|
|
|
method removeAllKeys: keys
|
|
|
|
{
|
|
|
|
self notImplemented: #removeAllKeys:
|
|
|
|
}
|
|
|
|
*)
|
|
|
|
|
2017-01-06 14:49:42 +00:00
|
|
|
method remove: assoc
|
|
|
|
{
|
|
|
|
^self removeKey: (assoc key)
|
|
|
|
}
|
|
|
|
|
|
|
|
method remove: assoc ifAbsent: error_block
|
|
|
|
{
|
|
|
|
^self removeKey: (assoc key) ifAbsent: error_block
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:11:36 +00:00
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method do: block
|
2017-01-05 10:16:04 +00:00
|
|
|
{
|
|
|
|
| bs |
|
|
|
|
bs := self.bucket size.
|
|
|
|
0 priorTo: bs by: 1 do: [:i |
|
|
|
|
| ass |
|
2017-02-07 18:09:07 +00:00
|
|
|
if ((ass := self.bucket at: i) notNil) { block value: ass value }.
|
2017-01-05 10:16:04 +00:00
|
|
|
].
|
|
|
|
}
|
|
|
|
|
2017-01-06 13:27:49 +00:00
|
|
|
method keysDo: block
|
2017-01-05 10:16:04 +00:00
|
|
|
{
|
|
|
|
| bs |
|
2017-01-06 13:27:49 +00:00
|
|
|
bs := self.bucket size.
|
|
|
|
0 priorTo: bs by: 1 do: [:i |
|
|
|
|
| ass |
|
2017-02-07 18:09:07 +00:00
|
|
|
if ((ass := self.bucket at: i) notNil) { block value: ass key }.
|
2017-01-06 13:27:49 +00:00
|
|
|
].
|
|
|
|
}
|
2017-01-05 10:16:04 +00:00
|
|
|
|
2017-01-06 13:27:49 +00:00
|
|
|
method keysAndValuesDo: block
|
|
|
|
{
|
|
|
|
| bs |
|
2017-01-05 10:16:04 +00:00
|
|
|
bs := self.bucket size.
|
|
|
|
0 priorTo: bs by: 1 do: [:i |
|
|
|
|
| ass |
|
2017-02-07 18:09:07 +00:00
|
|
|
if ((ass := self.bucket at: i) notNil) { block value: ass key value: ass value }.
|
2017-01-05 10:16:04 +00:00
|
|
|
].
|
|
|
|
}
|
2016-05-13 15:10:34 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
class SymbolSet(Set)
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
class Dictionary(Set)
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
2017-02-10 07:38:29 +00:00
|
|
|
(* [NOTE]
|
|
|
|
* VM require Dictionary to implement new: and __put_assoc
|
2017-06-16 09:45:22 +00:00
|
|
|
* for the dictionary expression notation - %{ }
|
2017-02-10 07:38:29 +00:00
|
|
|
*)
|
|
|
|
|
|
|
|
## TODO: implement Dictionary as a Hashed List/Table or Red-Black Tree
|
|
|
|
## Do not inherit Set upon reimplementation
|
|
|
|
##
|
|
|
|
method(#class) new: size
|
|
|
|
{
|
|
|
|
^super new: (size + 10).
|
|
|
|
}
|
|
|
|
|
|
|
|
(* put_assoc: is called internally by VM to add an association
|
|
|
|
* to a dictionary with the dictionary/association expression notation
|
|
|
|
* like this:
|
|
|
|
*
|
2017-06-16 09:45:22 +00:00
|
|
|
* %{ 1 -> 20, #moo -> 999 }
|
2017-02-10 07:38:29 +00:00
|
|
|
*
|
|
|
|
* it must return self for the way VM works.
|
|
|
|
*)
|
|
|
|
method put_assoc: assoc
|
|
|
|
{
|
|
|
|
| hv ass bs index ntally key |
|
|
|
|
|
|
|
|
key := assoc key.
|
|
|
|
bs := self.bucket size.
|
|
|
|
hv := key hash.
|
|
|
|
index := hv rem: bs.
|
|
|
|
|
2017-03-09 07:26:43 +00:00
|
|
|
(* as long as 'assoc' supports the message 'key' and 'value'
|
|
|
|
* this dictionary should work. there is no explicit check
|
|
|
|
* on this protocol of key and value. *)
|
|
|
|
|
2017-02-10 07:38:29 +00:00
|
|
|
while ((ass := self.bucket at: index) notNil)
|
|
|
|
{
|
|
|
|
if (key = ass key)
|
|
|
|
{
|
|
|
|
(* found *)
|
|
|
|
self.bucket at: index put: assoc.
|
|
|
|
^self. ## it must return self for the instructions generated by the compiler.
|
|
|
|
}.
|
|
|
|
index := (index + 1) rem: bs.
|
|
|
|
}.
|
|
|
|
|
|
|
|
(* not found *)
|
|
|
|
ntally := self.tally + 1.
|
|
|
|
if (ntally >= bs)
|
|
|
|
{
|
|
|
|
self.bucket := self __make_expanded_bucket: bs.
|
|
|
|
bs := self.bucket size.
|
|
|
|
index := hv rem: bs.
|
|
|
|
while ((self.bucket at: index) notNil) { index := (index + 1) rem: bs }.
|
|
|
|
}.
|
|
|
|
|
|
|
|
self.tally := ntally.
|
|
|
|
self.bucket at: index put: assoc.
|
|
|
|
|
2017-03-09 07:26:43 +00:00
|
|
|
(* it must return self for the instructions generated by the compiler.
|
|
|
|
* otherwise, VM will break. *)
|
|
|
|
^self.
|
2017-02-10 07:38:29 +00:00
|
|
|
}
|
2016-05-13 15:10:34 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:27:48 +00:00
|
|
|
(* Namespace is marked with #limited. If a compiler is writeen in moo itself, it must
|
|
|
|
* call a primitive to instantiate a new namespace rather than sending the new message
|
|
|
|
* to Namespace *)
|
|
|
|
class(#limited) Namespace(Set)
|
2016-06-30 13:44:37 +00:00
|
|
|
{
|
2017-05-20 02:27:48 +00:00
|
|
|
var name, nsup.
|
|
|
|
|
|
|
|
method name { ^self.name }
|
|
|
|
## method name: name { self.name := name }
|
|
|
|
|
|
|
|
(* nsup points to either the class associated with this namespace or directly
|
|
|
|
* the upper namespace placed above this namespace. when it points to a class,
|
|
|
|
* you should inspect the nsup field of the class to reach the actual upper
|
|
|
|
* namespace *)
|
|
|
|
method nsup { ^self.nsup }
|
|
|
|
## method nsup: nsup { self.nsup := nsup }
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method at: key
|
|
|
|
{
|
2017-02-07 18:09:07 +00:00
|
|
|
if (key class ~= Symbol) { InvalidArgumentException signal: 'key is not a symbol' }.
|
2017-01-06 09:53:40 +00:00
|
|
|
^super at: key.
|
|
|
|
}
|
2017-01-06 13:27:49 +00:00
|
|
|
|
|
|
|
method at: key put: value
|
|
|
|
{
|
2017-02-07 18:09:07 +00:00
|
|
|
if (key class ~= Symbol) { InvalidArgumentException signal: 'key is not a symbol' }.
|
2017-01-06 13:27:49 +00:00
|
|
|
^super at: key put: value
|
|
|
|
}
|
2016-06-30 13:44:37 +00:00
|
|
|
}
|
2016-05-13 15:10:34 +00:00
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
class PoolDictionary(Set)
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
class MethodDictionary(Dictionary)
|
2016-05-13 15:10:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
extend Apex
|
2016-07-01 16:31:47 +00:00
|
|
|
{
|
|
|
|
## -------------------------------------------------------
|
|
|
|
## Association has been defined now. let's add association
|
|
|
|
## creating methods
|
|
|
|
## -------------------------------------------------------
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method(#class) -> object
|
2016-07-01 16:31:47 +00:00
|
|
|
{
|
|
|
|
^Association new key: self value: object
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method -> object
|
2016-07-01 16:31:47 +00:00
|
|
|
{
|
|
|
|
^Association new key: self value: object
|
|
|
|
}
|
2017-01-18 17:17:05 +00:00
|
|
|
}
|
2017-03-04 05:48:23 +00:00
|
|
|
|
|
|
|
## -------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class Link(Object)
|
|
|
|
{
|
2017-04-19 16:46:44 +00:00
|
|
|
var prev, next, value.
|
2017-03-04 05:48:23 +00:00
|
|
|
|
|
|
|
method(#class) new: value
|
|
|
|
{
|
|
|
|
| x |
|
|
|
|
x := self new.
|
|
|
|
x value: value.
|
|
|
|
^x
|
|
|
|
}
|
|
|
|
|
|
|
|
method prev { ^self.prev }
|
|
|
|
method next { ^self.next }
|
|
|
|
method value { ^self.value }
|
|
|
|
method prev: link { self.prev := link }
|
|
|
|
method next: link { self.next := link }
|
|
|
|
method value: value { self.value := value }
|
|
|
|
}
|
|
|
|
|
|
|
|
class LinkedList(Collection)
|
|
|
|
{
|
2017-04-19 16:46:44 +00:00
|
|
|
var first, last, tally.
|
2017-03-04 05:48:23 +00:00
|
|
|
|
|
|
|
method initialize
|
|
|
|
{
|
|
|
|
self.tally := 0.
|
|
|
|
}
|
|
|
|
|
|
|
|
method size
|
|
|
|
{
|
|
|
|
^self.tally
|
|
|
|
}
|
|
|
|
|
|
|
|
method first
|
|
|
|
{
|
|
|
|
^self.first
|
|
|
|
}
|
|
|
|
|
|
|
|
method last
|
|
|
|
{
|
|
|
|
^self.last
|
|
|
|
}
|
|
|
|
|
|
|
|
method insertLink: link at: pos
|
|
|
|
{
|
|
|
|
if (pos isNil)
|
|
|
|
{
|
|
|
|
(* add link at the back *)
|
|
|
|
if (self.tally == 0)
|
|
|
|
{
|
|
|
|
self.first := link.
|
|
|
|
self.last := link.
|
|
|
|
link prev: nil.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
link prev: self.last.
|
|
|
|
self.last next: link.
|
|
|
|
self.last := link.
|
|
|
|
}.
|
|
|
|
link next: nil.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
(* insert the link before pos *)
|
|
|
|
link next: pos.
|
|
|
|
link prev: pos prev.
|
|
|
|
if (pos prev notNil) { pos prev next: link }
|
|
|
|
else { self.first := link }.
|
|
|
|
pos prev: link
|
|
|
|
}.
|
|
|
|
|
|
|
|
self.tally := self.tally + 1.
|
|
|
|
^link
|
|
|
|
}
|
|
|
|
|
|
|
|
method insert: value at: pos
|
|
|
|
{
|
|
|
|
^self insertLink: (Link new: value) at: pos
|
|
|
|
}
|
|
|
|
|
|
|
|
method addFirst: value
|
|
|
|
{
|
|
|
|
^self insert: value at: self.first
|
|
|
|
}
|
|
|
|
|
|
|
|
method addLast: value
|
|
|
|
{
|
|
|
|
^self insert: value at: nil
|
|
|
|
}
|
|
|
|
|
|
|
|
method addFirstLink: link
|
|
|
|
{
|
|
|
|
^self insertLink: link at: self.first
|
|
|
|
}
|
|
|
|
|
|
|
|
method addLastLink: link
|
|
|
|
{
|
|
|
|
^self insertLink: link at: nil
|
|
|
|
}
|
|
|
|
|
|
|
|
method removeLink: link
|
|
|
|
{
|
|
|
|
if (link next notNil) { link next prev: link prev }
|
|
|
|
else { self.last := link prev }.
|
|
|
|
|
|
|
|
if (link prev notNil) { link prev next: link next }
|
|
|
|
else { self.first := link next }.
|
|
|
|
|
|
|
|
self.tally := self.tally - 1.
|
2017-03-19 14:18:37 +00:00
|
|
|
^link.
|
2017-03-04 05:48:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-19 14:18:37 +00:00
|
|
|
method removeFirstLink
|
2017-03-04 05:48:23 +00:00
|
|
|
{
|
|
|
|
^self removeLink: self.first
|
|
|
|
}
|
|
|
|
|
2017-03-19 14:18:37 +00:00
|
|
|
method removeLastLink
|
2017-03-04 05:48:23 +00:00
|
|
|
{
|
|
|
|
^self removeLink: self.last
|
|
|
|
}
|
|
|
|
|
|
|
|
method do: block
|
|
|
|
{
|
|
|
|
| link next |
|
|
|
|
link := self.first.
|
|
|
|
while (link notNil)
|
|
|
|
{
|
|
|
|
next := link next.
|
|
|
|
block value: link value.
|
|
|
|
link := next.
|
|
|
|
}
|
|
|
|
}
|
2017-03-19 14:18:37 +00:00
|
|
|
|
|
|
|
method doOverLink: block
|
|
|
|
{
|
|
|
|
| link next |
|
|
|
|
link := self.first.
|
|
|
|
while (link notNil)
|
|
|
|
{
|
|
|
|
next := link next.
|
|
|
|
block value: link.
|
|
|
|
link := next.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 16:03:29 +00:00
|
|
|
method findEqualLink: value
|
2017-03-19 14:18:37 +00:00
|
|
|
{
|
|
|
|
self doOverLink: [:el | if (el value = value) { ^el }].
|
|
|
|
^Error.Code.ENOENT
|
|
|
|
}
|
2017-06-27 16:03:29 +00:00
|
|
|
|
|
|
|
method findIdenticalLink: value
|
|
|
|
{
|
|
|
|
self doOverLink: [:el | if (el value == value) { ^el }].
|
|
|
|
^Error.Code.ENOENT
|
|
|
|
}
|
2017-03-04 05:48:23 +00:00
|
|
|
}
|