Skip to content

Commit

Permalink
bug(objectionary#3481): map
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Jan 19, 2025
1 parent 5d5ec55 commit 856a4f1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 73 deletions.
8 changes: 4 additions & 4 deletions eo-runtime/src/main/eo/org/eolang/structs/hash-code-of.eo
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
if. > @
index.eq ^.size
acc.as-number
^.rec-hash-code
rec-hash-code
plus.
^.magic-number.times acc
magic-number.times acc
as-i64.
concat.
00-00-00-00-00-00-00
^.input-as-bytes.slice index 1
index.plus 1
input-as-bytes.slice index 1
(index.plus 1).as-number
137 changes: 68 additions & 69 deletions eo-runtime/src/main/eo/org/eolang/structs/map.eo
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,39 @@
+unlint broken-ref

# Hash-map.
# Here `pairs` must be a `tuple` of `tuple`s where each sub-tuple consists of 2
# elements - key and value.
# Key must be a dataizable object, value may be any object.
# Here `pairs` must be a `tuple` of `map.entry` object.
# The `map.entry.key` must be a dataizable object, `map.entry.value` may be any object.
[pairs] > map
initialized. > @
this. > @
[] >>
^.pairs.length > pairs-size!
^.initialized > @
if.
pairs-size.eq 0
*
rec-rebuild
initialized > @
as-tuple.
if.
pairs.length.eq 0
*
0
list *
entries.
rec-rebuild
pairs

[accum index hashes] > rec-rebuild
^.^.pairs.at index > entry
hash-code-of entry.key > hash!
[entries hashes] > couple
$ > this

[tup] > rec-rebuild
hash-code-of tup.value.key > hash!
(rec-rebuild tup.prev).this > prev
if. > @
^.pairs-size.eq index
accum
^.rec-rebuild
if.
hashes.contains hash
accum
accum.with
tup.length.eq 0
couple * (list *)
if.
prev.hashes.contains hash
prev
couple
prev.entries.with
[] >>
^.entry.key > key
^.entry.value > value
tup.value.key > key
tup.value.value > value
^.hash > hash
index.plus 1
hashes.with hash
prev.hashes.with hash

# Hash map entry.
# Here 'key' is an object which is used to find `value` is hash map.
Expand All @@ -73,26 +73,22 @@

# Initialized hash map with rebuilt entries.
[entries] > initialized
$ > initialized
$ > this
entries.length > size

# Returns `list` of all keys in hash map.
# Keys order is not guaranteed.
[] > keys
mapped. > @
list ^.entries
entry.key > [entry]

mapped. > keys
list entries
entry.key > [entry]
# Returns `list` of all values in hash map.
# Values order is not guaranteed.
[] > values
mapped. > @
list ^.entries
entry.value > [entry]
mapped. > values
list entries
entry.value > [entry]

# Returns `true` if hash map has object by given `key`.
# Here `key` must be dataizable.
(^.found key).exists > [key] > has
(found key).exists > [key] > has

# Tries to find object in hash map by given `key`.
# Here `key` must be dataizable.
Expand All @@ -115,54 +111,57 @@
[key] > found
hash-code-of key > hash!
if. > @
^.size.eq 0
size.eq 0
not-found
rec-key-search
not-found
0
rec-key-search entries

[found index] > rec-key-search
^.^.entries.at index > entry
[tup] > rec-key-search
(rec-key-search tup.prev).this > prev
if. > @
or.
found.exists
^.^.size.eq index
found
^.rec-key-search
tup.length.eq 0
not-found
if.
prev.exists
prev
if.
^.hash.eq entry.hash
[] (true > exists) (^.entry.value > get) >>
found
index.plus 1
tup.value.hash.eq hash
[] >>
$ > this
true > exists
tup.value.value > get
not-found

[] > not-found
$ > this
false > exists
error > get
sprintf
"Object by hash code %d from given key does not exists"
* ^.hash
* hash

# Returns the new `map` with added object
# Replaces if there was one before.
[key value] > with
hash-code-of key > hash!
map.initialized > @
with.
origin.
filtered.
list ^.entries
(^.hash.eq entry.hash).not > [entry] >>
[] >>
^.key > key
^.value > value
^.hash > hash
this. > @
map.initialized
with.
origin.
filtered.
list entries
(hash.eq entry.hash).not > [entry] >>
[] >>
^.key > key
^.value > value
^.hash > hash

# Returns a new `map`, without element with the given `key`
# Returns the `map` itself, if there was no item with this `key`.
[key] > without
hash-code-of key > hash!
map.initialized > @
origin.
filtered.
list ^.entries
(^.hash.eq entry.hash).not > [entry] >>
this. > @
map.initialized
origin.
filtered.
list entries
(hash.eq entry.hash).not > [entry] >>

0 comments on commit 856a4f1

Please sign in to comment.