Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed May 24, 2023
2 parents 57e7288 + 2da7456 commit 548fa9c
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/main/eo/org/eolang/collections/hash-code-of.eo
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# The MIT License (MIT)
#
# Copyright (c) 2016-2022 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

+alias org.eolang.collections.bytes-as-array
+home https://github.com/objectionary/eo-collections
+package org.eolang.collections
+rt jvm org.eolang:eo-collections:0.0.0
+version 0.0.0

# Hash code - the pseudo-unique integer representation of an object.
#
# Here "h" must be an object that can be converted to bytes via "as-bytes"
# object.
[h] > hash-code-of
switch > @
*
eq.
h.as-bytes > h-as-bytes!
01-
1232
*
h-as-bytes.eq 00-
1237
*
h-as-bytes.size.eq 8
as-int.
xor.
h-as-bytes
h-as-bytes.right 32
*
TRUE
[]
[acc index bytes zero-as-bytes] > rec-hash-code
if. > @
index.eq bytes.length
acc
rec-hash-code
plus.
times.
31
acc
as-int.
right.
zero-as-bytes.and
bytes.at index
54
index.plus 1
bytes
zero-as-bytes

0.as-bytes > zero-as-bytes!

rec-hash-code > @
1
0
bytes-as-array
h-as-bytes
zero-as-bytes
3 changes: 3 additions & 0 deletions src/main/eo/org/eolang/collections/map.eo
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@

# Returns the new map with added object
# Replaces if there was one before
# @todo #156:30min. Replace all "as-hash" object usings with "hash-code-of".
# In order to make "eo-runtime" independent of "eo-collections" need to
# replace all "as-hash" objects with new "hash-code-of" object.
[key value] > with
multimap * > mmp!
* key value > new-pair!
Expand Down
71 changes: 71 additions & 0 deletions src/test/eo/org/eolang/collections/hash-code-of-tests.eo
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# The MIT License (MIT)
#
# Copyright (c) 2021-2022 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

+alias org.eolang.collections.hash-code-of
+alias org.eolang.hamcrest.assert-that
+alias org.eolang.math.number
+home https://github.com/objectionary/eo-collections
+junit
+package org.eolang.collections
+version 0.0.0

# @todo #156:30min Add more tests for hash-code-of object.
# It would be interesting to test floating numbers and, maybe, big numbers
# (or some corner cases)
[] > hash-code-bools-is-integer
assert-that > @
and.
is-int.
number
hash-code-of TRUE
is-int.
number
hash-code-of FALSE
$.equal-to TRUE

[] > hash-of-int-is-int
assert-that > @
is-int.
number
hash-code-of 42
$.equal-to TRUE

[] > hash-codes-of-the-same-numbers-are-equal
42 > forty-two!
assert-that > @
hash-code-of forty-two
$.equal-to
hash-code-of forty-two

[] > hash-code-of-the-same-strings-are-equal
"hello" > str!
assert-that > @
hash-code-of str
$.equal-to
hash-code-of str

[] > hash-code-of-different-numbers-are-different
assert-that > @
hash-code-of 42
$.not
$.equal-to
hash-code-of 24

2 comments on commit 548fa9c

@0pdd
Copy link

@0pdd 0pdd commented on 548fa9c May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 156-e1f116f6 discovered in src/main/eo/org/eolang/collections/map.eo) and submitted as #175. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 548fa9c May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 156-50c73486 discovered in src/test/eo/org/eolang/collections/hash-code-of-tests.eo) and submitted as #176. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.