Skip to content

Fix MyLine priority system implementation in comparison methods#1

Closed
Copilot wants to merge 2 commits into
masterfrom
copilot/vscode1758506656298
Closed

Fix MyLine priority system implementation in comparison methods#1
Copilot wants to merge 2 commits into
masterfrom
copilot/vscode1758506656298

Conversation

Copy link
Copy Markdown

Copilot AI commented Sep 22, 2025

The MyLine class had a priority_level attribute but the comparison methods (__lt__ and __eq__) were only considering line length, completely ignoring the priority system. This caused sorting operations to work incorrectly when lines had different priority levels.

Problem

from CADAlgo.parser import MyLine

# These lines have different priorities but same length
line1 = MyLine((0, 0), (1, 0), priority_level=1)  # Highest priority
line3 = MyLine((0, 0), (1, 0), priority_level=3)  # Lowest priority

# Before fix: comparison only looked at length
print(line1 < line3)  # False (same length, so not less than)
print(line1 == line3)  # True (same length, considered equal)

# This broke priority-based sorting
lines = [line3, line1]
lines.sort()
# Result: [line3, line1] - wrong order!

Solution

Updated __lt__ and __eq__ methods to:

  1. Compare by priority first (lower number = higher priority)
  2. Use length as tiebreaker when priorities are equal
  3. Maintain backward compatibility for existing code using default priority
def __lt__(self, other: "MyLine") -> bool:
    # Compare by priority first (lower number = higher priority)
    if self.priority_level != other.priority_level:
        return self.priority_level < other.priority_level
    # If priorities are equal, compare by length (shorter is better)
    return self.geometry.length < other.geometry.length

After Fix

# Now priority is correctly considered
print(line1 < line3)  # True (priority 1 < priority 3)
print(line1 == line3)  # False (different priorities)

lines = [line3, line1]
lines.sort()
# Result: [line1, line3] - correct priority order!

The fix ensures that:

  • Lines with higher priority (lower priority_level numbers) are processed first
  • Within the same priority level, shorter lines are preferred
  • All existing code continues to work without changes (backward compatible)
  • Both sort() and heapq operations now respect the priority system

Created from VS Code via the GitHub Pull Request extension.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@jihaohaaaa jihaohaaaa closed this Sep 22, 2025
Copilot AI changed the title [WIP] 请问我的这个优先级的实现可以吗? Fix MyLine priority system implementation in comparison methods Sep 22, 2025
Copilot AI requested a review from jihaohaaaa September 22, 2025 02:11
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.

2 participants