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

WIP: exploring workaround for slice first design #21

Open
wants to merge 6 commits into
base: slice_first_design
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 68 additions & 131 deletions src/algo.rs
Original file line number Diff line number Diff line change
@@ -1,153 +1,90 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2024-2025 Rishabh Dwivedi (rishabhdwivedi17@gmail.com)

//! # Algorithms module
//!
//! The `algo` module provides STL algorithms.
// Copyright (c) 2025 Rishabh Dwivedi (rishabhdwivedi17@gmail.com)

use crate::{
view::{LazyMapView, MapView},
Collection, LazyCollection, Range,
Collection, MutableCollection, MutableRange, RandomAccessRange, Range,
RangeLifetime,
};

mod find;

pub trait RangeExtension: Range {
/// Finds position of first element satisfying predicate.
///
/// # Precondition
///
/// # Postcondition
/// - Returns position of first element in self satisfying pred.
/// - Returns end position if no such element exists.
/// - Complexity: O(n). Maximum `n` applications of `pred`.
///
/// where n is number of elements in self.
///
/// # Example
/// ```rust
/// use stl::*;
/// use algo::*;
///
/// let arr = [1, 2, 3];
/// let i = arr.find_if(|x| *x == 3);
/// assert_eq!(i, 2);
/// ```
fn find_if<Pred>(&self, pred: Pred) -> Self::Position
pub trait MutableRangeExtension: MutableRange
where
for<'a> <Self as RangeLifetime<'a>>::Slice: Range,
for<'a> <Self as RangeLifetime<'a>>::MutableSlice: MutableRange,
{
/// Partitions range with partition_fn which accepts second argument as reference to element at i.
fn partition_on<F>(
&mut self,
_i: &Self::Position,
_partition_fn: F,
) -> Self::Position
where
Pred: Fn(&Self::Element) -> bool,
F: Fn(&Self::Element, &Self::Element) -> bool,
{
find::find_if(self, pred)
todo!()
}
}

/// Finds position of first element not satisfying predicate.
///
/// # Precondition
///
/// # Postcondition
/// - Returns position of first element in self not satisfying pred.
/// - Returns end position if no such element exists.
/// - Complexity: O(n). Maximum `n` applications of `pred`.
///
/// where n is number of elements in self.
///
/// # Example
/// ```rust
/// use stl::*;
/// use algo::*;
///
/// let arr = [1, 2, 3];
///
/// let i = arr.find_if_not(|x| *x == 3);
/// assert_eq!(i, 0);
/// ```
fn find_if_not<Pred>(&self, pred: Pred) -> Self::Position
pub trait RandomAccessRangeExtension: RandomAccessRange
where
for<'a> <Self as RangeLifetime<'a>>::Slice: RandomAccessRange,
for<'a> <Self as RangeLifetime<'a>>::MutableSlice: RandomAccessRange,
{
fn quick_sort<Comparator>(&mut self, is_less: Comparator)
where
Pred: Fn(&Self::Element) -> bool,
Self: MutableRange,
for<'a> <Self as RangeLifetime<'a>>::MutableSlice: MutableRange,
Comparator: Fn(&Self::Element, &Self::Element) -> bool,
{
find::find_if_not(self, pred)
let start = self.start();
let mut end = self.end();
if start == end {
return;
}
end = self.before(end);
let p = self.partition_on(&end, is_less);
self.slice_mut(start, p.clone()).quick_sort(is_less);
self.slice_mut(p, end).quick_sort(is_less);
}
}

/// Finds position of first element equals given element.
///
/// # Precondition
///
/// # Postcondition
/// - Returns position of first element in self equals e.
/// - Returns end if no such element exists.
/// - Complexity: O(n). Maximum `n` equality comparisions,
///
/// where n is number of elements in self.
///
/// # Example
/// ```rust
/// use stl::*;
/// use algo::*;
///
/// let arr = [1, 2, 3];
/// let i = arr.find(&3);
/// assert_eq!(i, 2);
/// ```
fn find(&self, e: &Self::Element) -> Self::Position
pub trait MutableCollectionExtension: MutableCollection
where
for<'a> <Self as RangeLifetime<'a>>::Slice: Collection,
for<'a> <Self as RangeLifetime<'a>>::MutableSlice: MutableCollection,
{
fn for_each_mut<F>(&mut self, mut f: F)
where
Self::Element: Eq,
F: FnMut(&mut Self::Element),
{
find::find(self, e)
let mut start = self.start();
let end = self.end();
while start != end {
f(self.at_mut(&start));
start = self.after(start);
}
}
}

pub trait CollectionExtension: Collection {
/// Returns a lazy collection over given collection where ith element is f(ith_element).
///
/// # Precondition
///
/// # Postcondition
/// - Position of mapped view would be same as positions of underlying range.
/// - Traits satisfied:
/// - Range: YES
/// - Collection: NO
/// - LazyCollection: YES
/// - BidirectionalRange: If self is BidirectionalRange
/// - RandomAccessRange: If self is RandomAccessRange
/// - MutableRange: If self is MutableRange
/// - ReorderableRange: If self is ReorderableRange
/// - MutableCollection: NO
/// - MutabeLazyCollection: NO
fn map<F, OutputElement>(self, f: F) -> MapView<Self, F, OutputElement>
where
F: Fn(&Self::Element) -> OutputElement,
Self: Sized,
{
MapView::new(self, f)
}
impl<R> MutableCollectionExtension for R
where
R: MutableCollection + ?Sized,
for<'a> <R as RangeLifetime<'a>>::Slice: Collection,
for<'a> <R as RangeLifetime<'a>>::MutableSlice: MutableCollection,
{
}

pub trait LazyCollectionExtension: LazyCollection {
/// Returns a lazy collection over given collection where ith element is f(ith_element).
///
/// # Precondition
///
/// # Postcondition
/// - Position of mapped view would be same as positions of underlying range.
/// - Traits satisfied:
/// - Range: YES
/// - Collection: NO
/// - LazyCollection: YES
/// - BidirectionalRange: If self is BidirectionalRange
/// - RandomAccessRange: If self is RandomAccessRange
/// - MutableRange: If self is MutableRange
/// - ReorderableRange: If self is ReorderableRange
/// - MutableCollection: NO
/// - MutabeLazyCollection: NO
fn map<F, OutputElement>(self, f: F) -> LazyMapView<Self, F, OutputElement>
where
F: Fn(Self::Element) -> OutputElement,
Self: Sized,
{
LazyMapView::new(self, f)
}
impl<R> MutableRangeExtension for R
where
R: MutableRange + ?Sized,
for<'a> <R as RangeLifetime<'a>>::Slice: Range,
for<'a> <R as RangeLifetime<'a>>::MutableSlice: MutableRange,
{
}

impl<R> RangeExtension for R where R: Range {}
impl<R> CollectionExtension for R where R: Collection {}
impl<R> LazyCollectionExtension for R where R: LazyCollection {}
impl<R> RandomAccessRangeExtension for R
where
R: RandomAccessRange + ?Sized,
for<'a> <R as RangeLifetime<'a>>::Slice: RandomAccessRange,
for<'a> <R as RangeLifetime<'a>>::MutableSlice: RandomAccessRange,
{
}
36 changes: 0 additions & 36 deletions src/algo/find.rs

This file was deleted.

79 changes: 0 additions & 79 deletions src/array.rs

This file was deleted.

Loading