-
Notifications
You must be signed in to change notification settings - Fork 139
[DON'T MERGE] feat: Add Map::map and Map::copy methods to LinkedHashMap #2308
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
Conversation
Implement two new methods for the LinkedHashMap: 1. `Map::map[K, V, V2](self: Self[K, V], f: (K, V) -> V2) -> Map[K, V2]` - Transforms all values in the map using the provided function - Preserves the original structure, order, and hash table layout - Creates a new map without rehashing 2. `Map::copy[K, V](self: Self[K, V]) -> Map[K, V]` - Creates a deep copy of the map with identical structure and contents - Returned map is completely independent of the original - Preserves insertion order and hash table structure Both methods avoid using `Map::new()` or `Map::add()` as required, instead directly creating maps from existing entries while preserving the internal structure. Features: - Full documentation with doctests showing proper usage - Comprehensive black-box tests covering various scenarios - Preservation of insertion order and independence for copies - Support for type transformation in map method - Memory-efficient implementation using pointer comparisons Tests included: - Basic functionality tests for both methods - Empty map handling - Type transformation verification - Independence verification (modifications to original don't affect copies) - Complex transformation scenarios - Chaining operations verification
Inefficient next entry lookup in map/copy methodsCategory type Entry[K, V] = {
next_idx: Int, // Store index instead of Entry reference
// ... other fields
} Reasoning Duplicated code between map and copy methodsCategory fn rebuild_links[K, V](old_entries, new_entries, capacity) -> Entry[K,V]? { ... } Reasoning Tail reference is copied without rebuildingCategory let mut new_tail = None
// Find tail by traversing from head
let mut current = new_head
while current.next != None {
current = current.next
}
new_tail = current Reasoning |
Pull Request Test Coverage Report for Build 7405Details
💛 - Coveralls |
// Find which slot contains the next entry by comparing addresses | ||
for j in 0..<self.capacity { | ||
match self.entries[j] { | ||
Some(candidate_entry) => { | ||
// Use pointer comparison instead of value comparison | ||
if physical_equal(candidate_entry, old_next_entry) { | ||
new_entry.next = new_entries[j] | ||
break | ||
} | ||
} | ||
None => continue | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need to scan self.entries
. This would lead to an O(capacity²) time complexity.
The index of old_next_entry
= (old_next_entry.hash + old_next_entry.psl) % capacity
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@FlyCloudC Thank you for your review! However, as the original commit message already points out, this PR is actually not written by me but an LLM agent to test out its abilities. I'll make it clearer in the original post.
Note
This PR was made by an LLM agent without further modifications, and it is not intended to be merged.
Implement two new methods for the LinkedHashMap:
Map::map[K, V, V2](self: Self[K, V], f: (K, V) -> V2) -> Map[K, V2]
Map::copy[K, V](self: Self[K, V]) -> Map[K, V]
Both methods avoid using
Map::new()
orMap::add()
as required, instead directly creating maps from existing entries while preserving the internal structure.Features:
Tests included: