Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1264 Add .range() method to sorted_map and sorted_set #1359

Merged
merged 6 commits into from
Jan 12, 2025

Conversation

E-M-T-F
Copy link
Contributor

@E-M-T-F E-M-T-F commented Dec 24, 2024

Added .range() method to sorted_map and sorted_set, and privode the test cases

Copy link

peter-jerry-ye-code-review bot commented Dec 24, 2024

‼️ This code review is generated by a bot. Please verify the content before trusting it.

Here are three observations from the provided git diff output:

  1. Inconsistent Range Semantics in Documentation:

    • In the map.mbt file, the documentation for the range function states that it returns key-value pairs within the range [low, high), which is a half-open interval (includes low but excludes high).
    • However, in the set.mbt file, the range function's documentation does not explicitly specify whether the range is half-open or closed. This inconsistency in documentation could lead to confusion about the expected behavior of the range function in different contexts.
  2. Potential Logic Error in Range Function:

    • In the range_aux function in both map.mbt and set.mbt, the condition if cmp_key_low >= 0 && cmp_key_high <= 0 is used to determine whether to yield a node. This condition implies that the range is inclusive on both ends ([low, high]), which contradicts the documentation in map.mbt that suggests a half-open interval ([low, high)).
    • This discrepancy could lead to unexpected behavior where the high value is included in the results when it should not be, according to the documentation.
  3. Redundant Guard Statements:

    • In both range_aux functions, there are redundant guard statements that check for IterContinue after recursive calls or yield operations. These guards are followed by an else clause that immediately returns on any other value. However, since IterContinue is the only possible return value in these contexts, the else clause is unnecessary and could be removed to simplify the code.
    • For example:
      guard let IterContinue = range_aux(node.left, low, high).run(yield_) else {
        x => return x
      }
      could be simplified to:
      range_aux(node.left, low, high).run(yield_)

These issues should be addressed to ensure clarity, correctness, and maintainability of the code.

@coveralls
Copy link
Collaborator

coveralls commented Dec 24, 2024

Pull Request Test Coverage Report for Build 4712

Details

  • 15 of 21 (71.43%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.04%) to 82.88%

Changes Missing Coverage Covered Lines Changed/Added Lines %
sorted_map/map.mbt 8 11 72.73%
sorted_set/set.mbt 7 10 70.0%
Totals Coverage Status
Change from base Build 4709: -0.04%
Covered Lines: 4783
Relevant Lines: 5771

💛 - Coveralls

@bobzhang
Copy link
Contributor

the implementation looks good to me, but this is not commonly used operation, shall we leave it in the user land?

@E-M-T-F
Copy link
Contributor Author

E-M-T-F commented Dec 25, 2024

Related: #1264

@E-M-T-F
Copy link
Contributor Author

E-M-T-F commented Dec 25, 2024

This PR is the solution of issue #1264, so I think it is an useful solution for the user land

@bobzhang
Copy link
Contributor

@E-M-T-F that looks good to me, can you change the API to return Iter2 instead?

range[K : Compare, V](Self[K, V], K, K) -> Iter2[K,V]

@E-M-T-F
Copy link
Contributor Author

E-M-T-F commented Dec 26, 2024

@bobzhang OK,I will change the API to return Iter2 instead

@Lampese Lampese requested a review from bobzhang January 8, 2025 08:41
@bobzhang bobzhang force-pushed the range_sorted_map_sorted_set branch from a0461cd to 2e7fa56 Compare January 11, 2025 14:30
@bobzhang bobzhang force-pushed the range_sorted_map_sorted_set branch from 2e7fa56 to d129a06 Compare January 12, 2025 01:48
…ove key comparisons and yield logic for better performance and clarity.
@bobzhang
Copy link
Contributor

@E-M-T-F I added some tweaks, thank you

@bobzhang bobzhang enabled auto-merge (rebase) January 12, 2025 02:19
@bobzhang bobzhang merged commit 147e136 into moonbitlang:main Jan 12, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants