Skip to content

✨ (slice): Introduce SliceExt trait with has_dup method#29

Merged
czy-29 merged 3 commits into
mainfrom
openhands/implement-slice-has-dup
Jul 8, 2025
Merged

✨ (slice): Introduce SliceExt trait with has_dup method#29
czy-29 merged 3 commits into
mainfrom
openhands/implement-slice-has-dup

Conversation

@czy-29

@czy-29 czy-29 commented Jul 8, 2025

Copy link
Copy Markdown
Member

This PR implements a new SliceExt trait with a has_dup method as requested in issue #28.

Changes

  • New slice module: Added src/slice.rs with the SliceExt trait
  • has_dup method: Efficiently checks for duplicate elements in O(n) average time complexity
  • Feature flag: Added slice feature to Cargo.toml following project conventions
  • Comprehensive tests: 14 unit tests covering various scenarios including edge cases
  • Documentation: Detailed rustdoc with examples and complexity analysis

Implementation Details

The has_dup method uses a HashSet to track seen elements, providing:

  • Time Complexity: O(n) average case, O(n²) worst case (rare with good hash function)
  • Space Complexity: O(n) for storing seen elements
  • Generic Support: Works with any type implementing Hash + Eq

Test Coverage

The implementation includes comprehensive tests for:

  • ✅ Slices with duplicates
  • ✅ Slices without duplicates
  • ✅ Empty slices
  • ✅ Single element slices
  • ✅ All same elements
  • ✅ Different data types (integers, strings, chars)
  • ✅ Vec and slice references
  • ✅ Large slices (performance verification)
  • ✅ Edge cases (duplicates at beginning/end)

Usage Example

use est::slice::SliceExt;

let slice_with_dups = [1, 2, 3, 2, 4];
assert!(slice_with_dups.has_dup());

let slice_without_dups = [1, 2, 3, 4, 5];
assert!(!slice_without_dups.has_dup());

Fixes #28

@czy-29 can click here to continue refining the PR

- Add new slice module with SliceExt trait
- Implement has_dup method for checking duplicates in O(n) time
- Add comprehensive unit tests covering various scenarios
- Add slice feature flag to Cargo.toml
- Update lib.rs to include slice module with feature gate
- Add detailed documentation with examples

Fixes #28
@czy-29

czy-29 commented Jul 8, 2025

Copy link
Copy Markdown
Member Author

@OpenHands 两个问题:

  1. 把pub mod slice的文档注释改成:
/// Extensions to the [`std::slice`] module.
  1. 两个语言的README里补充上:slice::SliceExt::has_dup()

@openhands-ai

openhands-ai Bot commented Jul 8, 2025

Copy link
Copy Markdown

I'm on it! czy-29 can track my progress at all-hands.dev

openhands-agent and others added 2 commits July 8, 2025 03:50
…DMEs

- Change slice module doc comment from 'Extensions to the [slice] type' to 'Extensions to the [std::slice] module'
- Add slice::SliceExt::has_dup() to both English and Chinese README files
@czy-29 czy-29 changed the title feat: implement SliceExt trait with has_dup method ✨ (slice): Introduce SliceExt trait with has_dup method Jul 8, 2025
@czy-29 czy-29 merged commit fa37a11 into main Jul 8, 2025
1 check passed
@czy-29 czy-29 deleted the openhands/implement-slice-has-dup branch July 8, 2025 04:31
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.

实现一个SliceExt::has_dup

2 participants