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

Implement MutatingSlice #1561

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions StandardLibrary/Sources/Array.hylo
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public type Array<Element: SemiRegular>: SemiRegular {
let c = count()
var i = 0
while i < c {
&pointer_to_element(at: i).unsafe_pointee().deinit()
pointer_to_element(at: i).unsafe_pointee().deinit()
&i += 1
}
&storage.header = 0
Expand Down Expand Up @@ -176,7 +176,7 @@ public type Array<Element: SemiRegular>: SemiRegular {
var i = count() - 1
var j = 0
while i > j {
swap_at(i, j)
&swap_at(i, j)
&i -= 1
&j += 1
}
Expand Down
2 changes: 1 addition & 1 deletion StandardLibrary/Sources/Core/MutableCollection.hylo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ trait MutableCollection: Collection {
/// Accesses the element at position `i`.
///
/// - Requires: `i` is a valid position in `self` different from `end_position()`.
subscript(_ i: Position): Element { inout }
subscript(_ i: Position): Element { let inout }

/// Exchanges the values at the given positions in `self`.
///
Expand Down
74 changes: 74 additions & 0 deletions StandardLibrary/Sources/Core/MutatingSlice.hylo
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/// A view presenting elements between two positions in a collection.
public type MutatingSlice<Base: MutableCollection>: SemiRegular {

/// A pointer to the collection of which `self` is a slice.
private let base: PointerToMutable<Base>

/// The first position in `self`.
private let lower: Base.Position

/// The "past-the-end" position in `self`.
private let upper: Base.Position

/// Creates an instance with the given source.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/// Creates an instance with the given source.
/// Creates an instance projecting the elements at `lower..<upper` in `base`.

FWIW, "the given" is usually a mistake.

memberwise init

/// Returns `true` iff `self` is equal to `other`.
public fun infix== (_ other: Self) -> Bool {
(self.base == other.base) && (self.lower == other.lower) && (self.upper == other.upper)
}

/// Projects a slice of `base` presenting its elements from `lower` to `upper`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

A little ambiguous as to whether the range is half-open.

public static subscript of(
_ base: inout Base, from lower: sink Base.Position, to upper: sink Base.Position
): Self {
let {
let instance = MutatingSlice<Base>(
base: mutable_pointer[to: &base].copy(),
lower: lower, upper: upper)
yield instance
}
inout {
var instance = MutatingSlice<Base>(
base: mutable_pointer[to: &base].copy(),
lower: lower, upper: upper)
yield &instance
}
}

}

public conformance MutatingSlice: MutableCollection {

public typealias Position = Base.Position

public typealias Element = Base.Element

public fun start_position() -> Base.Position {
lower.copy()
}

public fun end_position() -> Base.Position {
upper.copy()
}

public fun position(after p: Base.Position) -> Base.Position {
base.unsafe[].position(after: p)
}

public fun swap_at(_ p: Base.Position, _ q: Base.Position) inout {
&(base.unsafe[]).swap_at(p, q)
}

public subscript(_ p: Base.Position): Base.Element {
let {
let b = base.unsafe[]
yield b[p]
}
inout {
inout b = &(base.unsafe[])
yield &b[p]
}
}

}