KeyedCollection.st
changeset 23354 bcd17ecee2c6
parent 23341 1fdd8b7e859c
child 23363 3d39badc34e9
--- a/KeyedCollection.st	Mon Sep 17 11:14:24 2018 +0200
+++ b/KeyedCollection.st	Mon Sep 17 14:53:53 2018 +0200
@@ -61,6 +61,32 @@
 
 !KeyedCollection class methodsFor:'instance creation'!
 
+decodeFromLiteralArray:anArray
+    "create & return a new instance from information encoded in anArray."
+
+    |dictionary
+     sz "{ Class: SmallInteger }"|
+
+    sz := anArray size.
+    dictionary := self new:sz//2.
+    2 to:sz by:2 do:[:idx | |key val|
+        key := (anArray at:idx) decodeAsLiteralArray.
+        val := (anArray at:idx+1) decodeAsLiteralArray.
+        dictionary at:key put:val
+    ].
+    ^ dictionary
+
+    "
+     (SmallDictionary new
+         at:1 put:'one';
+         at:2 put:'two';
+         yourself
+     ) literalArrayEncoding decodeAsLiteralArray
+    "
+
+    "Created: / 17-09-2018 / 14:51:59 / Stefan Vogel"
+!
+
 withAll:aCollection
     "create a KeyedCollection from another Collection with keys"
 
@@ -499,6 +525,34 @@
     "Created: / 14-09-2018 / 16:56:36 / Stefan Vogel"
 ! !
 
+!KeyedCollection methodsFor:'converting'!
+
+literalArrayEncoding
+    |literalArray idx|
+
+    literalArray := Array new:(self size * 2)+1.
+    literalArray at:1 put:self class name.
+    idx := 2.
+    self keysAndValuesDo:[:eachKey :eachValue |
+        literalArray
+            at:idx   put:eachKey literalArrayEncoding;
+            at:idx+1 put:eachValue literalArrayEncoding.
+        idx := idx + 2.
+    ].
+    ^ literalArray
+
+    "
+      |dict|
+
+      dict := SmallDictionary new.
+      dict at:1 put:'bla'.
+      dict at:'fasel' put:#[1 2 3 4].
+      dict literalArrayEncoding
+    "
+
+    "Created: / 17-09-2018 / 14:49:52 / Stefan Vogel"
+! !
+
 !KeyedCollection methodsFor:'enumerating'!
 
 associationsDo:aBlock