Skip to content

Instantly share code, notes, and snippets.

View CTMacUser's full-sized avatar

Daryle Walker CTMacUser

View GitHub Profile
@CTMacUser
CTMacUser / StridingSequence.swift
Created February 1, 2019 05:26
A sequence that strides over most of its wrapped sequence's elements.
//
// StridingSequence.swift
// CLITest
//
// Created by Daryle Walker on 1/31/19.
// Copyright © 2019 Daryle Walker. All rights reserved.
//
// WARNING: I think this requires Swift 5, since it needs Sequence to no longer have an SubSequence member.
@CTMacUser
CTMacUser / BasicSetSubtraction.swift
Last active February 20, 2019 05:23
A sorted sequence with elements of another sorted sequence filtered out. Based on <https://forums.swift.org/t/how-to-effectively-filter-array-by-another-array-in-functional-paradigm/20442>.
// BasicSetSubtraction.swift by Daryle Walker
/// An iterator that vends elements from a sorted wrapped iterator, subtracting the elements from in another wrapped sorted filter iterator.
public struct SetSubtractionIterator<Base: IteratorProtocol, Filter: IteratorProtocol> where Base.Element == Filter.Element, Base.Element: Comparable {
/// The iterator where the vended elements come from.
var base: Base
/// The iterator where matches to be filtered come from.
var filters: Filter
@CTMacUser
CTMacUser / RotateSwapMove.swift
Last active March 2, 2019 06:11
Extensions to MutableCollection to swap multiple elements at a time, plus support methods to rotate, and example methods to move elements.
// RotateSwapMove.swift by Daryle Walker
extension MutableCollection {
/**
Rotates the elements such that the value at the given index is now at `startIndex`.
Passing `startIndex` as `i` has no effect.
The method applies a left-rotation, bringing the target element's value to `startIndex`.
@CTMacUser
CTMacUser / ForEachIndex.swift
Last active March 2, 2019 07:07
Extension to loop over each Collection Index without the retention issues indices has. Plus, example usage for MutableCollection, and an extra related method for RangeReplaceableCollection.
// ForEachIndex.swift by Daryle Walker
extension Collection {
/**
Calls the given closure on each element index in the collection in the
same order as a `for`-`in` loop.
The two loops in the following example produce the same output:
@CTMacUser
CTMacUser / NNNN-multipass-sequences.md
Created March 26, 2019 03:48
An improvement to the Swift library cancelled due to ABI stability.

Multiple-Pass Sequences and Cycle Detection

During the review process, add the following fields as needed:

@CTMacUser
CTMacUser / ArrayManifesto.md
Last active July 2, 2019 23:06
A manifesto to outline how to add fixed-size scoped-storage arrays to Swift.

Fixed-Size, Scoped-Storage Array Manifesto

Introduction

This manifesto is for collecting the ideas needed to introduce the classic fixed-size array family of types to Swift.

@CTMacUser
CTMacUser / BinarySearch.swift
Last active September 23, 2019 21:57
A set type, in Swift 5(.1), that stores its elements in strictly increasing order, requiring Comparable instead of Hashable. Also includes: binary search, sort detection, streaming set operations, and (space-wise) debouncing.
//
// BinarySearch.swift
// SortedSet
//
// Created by Daryle Walker on 8/22/19.
// Copyright © 2019 Daryle Walker. All rights reserved.
//
extension Collection {
@CTMacUser
CTMacUser / IntegerNull.swift
Last active August 30, 2019 08:31
Another zero-width signed and unsigned integer type pair for Swift
//
// IntegerNull.swift
// ZeroInteger
//
// Created by Daryle Walker on 8/27/19.
// Copyright © 2019 Daryle Walker. All rights reserved.
//
// MARK: Zero-bit Integer Primary Definitions
@CTMacUser
CTMacUser / Heap.swift
Last active July 18, 2020 04:28
Heap operation methods and Priority Queues in Swift, inspired by the C++ Standard Library. See <https://forums.swift.org/t/heaps-structure-not-storage-and-priority-queues/31135> for more information.
extension Collection {
/// Computes the binary-tree children indices for the given starting index,
/// if the collection is long enough.
///
/// Child elements of a flattened tree are consecutive, so if two indices
/// are returned, `distance(from: left!, to: right!) == 1`.
///
/// - Parameter source: The index of the parent element.
/// - Returns: A tuple with the indices of the left and right binary-tree
@CTMacUser
CTMacUser / MergedSortedSequence.swift
Last active November 26, 2019 18:09
Merging, Combining, and Testing of Sorted Sequences in Swift, inspired by the C++ Standard Library. See <https://forums.swift.org/t/sorted-sequence-operations-merge-sort-set-operations-inclusion-testing/31145> for more information.
// MARK: Per-Element Sharing in Merged Sorted Sequences
/// Primary source categories when merging two sets together.
public enum MergedSetElementSource<Element> {
/// The given element is only in the first set.
case exclusivelyFirst(Element)
/// The element is in both sets. Both versions are given.
case shared(Element, Element)
/// The given element is only in the second set.