Skip to content

Conversation

@atta-ullah01
Copy link
Contributor

Summary of Changes

This PR optimizes CGAL::Concatenate_iterator to provide $O(1)$ time complexity for arithmetic operations (operator+, operator-, operator+=, operator-=) and distance calculations when the underlying iterators are Random Access Iterators (e.g., std::vector, pointers).

Previously, these operations relied on $O(n)$ loops (checking ++ or -- sequentially). This change utilizes C++17 if constexpr to conditionally enable the optimized arithmetic logic while preserving the original $O(n)$ fallback for Forward or Bidirectional iterators (e.g., std::list).

Key Changes:

  • $O(1)$ Arithmetic: implemented operator+ and operator- (offset) to calculate positions immediately by checking range boundaries.
  • $O(1)$ Distance: Implemented operator- (difference) to calculate the distance between two iterators across the concatenated ranges without traversal.
  • Iterator Category Deduction: updated iterator_category to use std::common_type_t. This correctly degrades the category to the "lowest common denominator" (e.g., mixing a Random Access iterator with a Bidirectional iterator results in a Bidirectional Concatenate_iterator).
  • Compound Assignment: Added operator+= and operator-=.
  • Negative Offsets: Fixed operator+ and operator- to correctly handle negative arguments by delegating to their inverse counterparts.
  • Tests: Added comprehensive tests for $O(1)$ behavior using std::vector and regression tests for mixed iterator categories.

This resolves the TODO comments previously located at lines 100 and 116 regarding complexity optimizations.

Release Management

  • Affected package(s): STL_Extension
  • Issue(s) solved (if any): Resolves internal TODOs regarding $O(1)$ complexity.
  • Feature/Small Feature (if any): Optimization for Concatenate_iterator
  • Link to compiled documentation (obligatory for small feature):
  • License and copyright ownership: CGAL

Use if constexpr to provide O(1) implementations of operator+, operator-,
operator+=, and operator-= when underlying iterators are random access.
Falls back to the original O(n) loop for forward/bidirectional iterators.

Changes:
- Correctly deduce iterator_category using std::common_type to support mixed iterator types.
- Add operator-(difference_type) for backwards traversal.
- Add operator-= for compound subtraction.
- Fix operator+ to handle negative offsets by delegating to operator-.
- Fix operator- to handle negative offsets by delegating to operator+.
- Add comprehensive tests for O(1) arithmetic operators and mixed iterator categories.

This resolves the TODO comments at lines 100 and 116.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes CGAL::Concatenate_iterator to provide O(1) time complexity for arithmetic operations when the underlying iterators are Random Access Iterators, replacing the previous O(n) loop-based implementations.

Key changes:

  • Implements O(1) arithmetic operations (operator+, operator-, operator+=, operator-=) and distance calculations for random access iterators using if constexpr
  • Updates iterator category deduction to use std::common_type_t to handle mixed iterator types
  • Adds comprehensive tests for O(1) behavior and mixed iterator categories

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 9 comments.

File Description
STL_Extension/include/CGAL/Concatenate_iterator.h Implements O(1) arithmetic operations with conditional compilation based on iterator category; adds Traits2 typedef and updates iterator_category deduction
STL_Extension/test/STL_Extension/test_Concatenate_iterator.cpp Adds tests for O(1) operations with vectors and regression tests for mixed iterator categories

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@atta-ullah01 atta-ullah01 requested a review from lrineau January 10, 2026 10:30
@MaelRL MaelRL added Enhancement CLA/CAA The contributor must sign a CLA or a CAA so that the contribution can be merged into CGAL Pkg::STL_Extension labels Jan 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA/CAA The contributor must sign a CLA or a CAA so that the contribution can be merged into CGAL Enhancement Pkg::STL_Extension Under Testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants