<commit_before>//  Copyright (c) 2015 <NAME>. All rights reserved.

import Dumpster

/// A singly-linked list of values.
public struct List<T> {
    // MARK: Constructors

    /// Initializes an empty `List`.
    public init() {
    }

    /// Initializes a `List` with a single `value`.
    public init(value: T) {
        self.init(Node(value))
    }

    /// Initializes a `List` with a collection of `values`.
    public init<S: SequenceType where S.Generator.Element == T>(_ values: S) {
        // TODO This should return the tail of the list as well so we don't rely on `head.last`
        self.init(Node.create(values))
    }

    // MARK: Properties

    /// Returns true iff `List` is empty.
    public var isEmpty: Bool {
        return head == nil
    }

    /// Returns the value at the head of `List`, `nil` if empty.
    public var first: T? {
        return head?.value
    }

    /// Returns the value at the tail of `List`, `nil` if empty.
    public var last: T? {
        return tail?.value
    }

    /// MARK: Operations

    /// Inserts a new `value` before the `first` value.
    public mutating func prepend(value: T) {
        head = head?.insertBefore(value)
        if head == nil {
            head = Node(value)
            tail = head
        }
    }

    /// Inserts a new `value` after the `last` value.
    public mutating func append(value: T) {
        tail = tail?.insertAfter(value)
        if tail == nil {
            tail = Node(value)
            head = tail
        }
    }

    // MARK: Private

    /// Initializes `List` with `head`.
    private init(_ head: Node?) {
        self.init(head, head?.last)
    }

    /// Initializes `List` with `head` and `tail`
    private init(_ head: Node?, _ tail: Node?) {
        self.head = head
        self.tail = tail
    }

    /// Replace the nodes at `subRange` with the given replacements.
    private mutating func spliceList(subRange: Range<Index>, _ replacementHead: ListNode<T>?, _ replacementTail: ListNode<T>?) {
        var prefixTail = subRange.startIndex.previous
        var suffixHead = subRange.endIndex.node

        if prefixTail == nil {
            head = replacementHead ?? suffixHead
        } else {
            prefixTail?.next = replacementHead ?? suffixHead
        }

        if suffixHead == nil {
            tail = replacementTail
        } else {
            replacementTail?.next = suffixHead
        }
    }
    
    /// The type of nodes in `List`.
    private typealias Node = ListNode<T>

    /// The first node of `List`.
    private var head: Node?

    /// The last node of `List`.
    private var tail: Node?
}

// MARK: Queue/Stack

extension List : QueueType, StackType {
    public typealias Element = T

    /// Removes the `first` value at the head of `List` and returns it, `nil` if empty.
    public mutating func popFirst() -> T? {
        let value = head?.value
        if head == tail {
            head = nil
            tail = nil
        } else {
            head = head?.next
        }
        return value
    }

    /// Inserts a new `value` before the `first` value.
    public mutating func pushFirst(value: T) {
        prepend(value)
    }

    /// Inserts a new `value` after the `last` value.
    public mutating func pushLast(value: T) {
        append(value)
    }
}

// MARK: ArrayLiteralConvertible

extension List : ArrayLiteralConvertible {
    /// Initializes a `List` with the `elements` from array.
    public init(arrayLiteral elements: T...) {
        self.init(elements)
    }
}<commit_msg>Make spliceList the core mutation operation<commit_after>//  Copyright (c) 2015 <NAME>. All rights reserved.

import Dumpster

/// A singly-linked list of values.
public struct List<T> {
    // MARK: Constructors

    /// Initializes an empty `List`.
    public init() {
    }

    /// Initializes a `List` with a single `value`.
    public init(value: T) {
        self.init(Node(value))
    }

    /// Initializes a `List` with a collection of `values`.
    public init<S: SequenceType where S.Generator.Element == T>(_ values: S) {
        // TODO This should return the tail of the list as well so we don't rely on `head.last`
        self.init(Node.create(values))
    }

    // MARK: Properties

    /// Returns true iff `List` is empty.
    public var isEmpty: Bool {
        return head == nil
    }

    /// Returns the value at the head of `List`, `nil` if empty.
    public var first: T? {
        return head?.value
    }

    /// Returns the value at the tail of `List`, `nil` if empty.
    public var last: T? {
        return tail?.value
    }

    /// MARK: Operations

    /// Inserts a new `value` before the `first` value.
    public mutating func prepend(value: T) {
        insert(value, atIndex: startIndex)
    }

    /// Inserts a new `value` after the `last` value.
    public mutating func append(value: T) {
        insert(value, atIndex: endIndex)
    }

    // MARK: Private

    /// Initializes `List` with `head`.
    private init(_ head: Node?) {
        self.init(head, head?.last)
    }

    /// Initializes `List` with `head` and `tail`
    private init(_ head: Node?, _ tail: Node?) {
        self.head = head
        self.tail = tail
    }

    /// Replace the nodes at `subRange` with the given replacements.
    private mutating func spliceList(subRange: Range<Index>, _ replacementHead: ListNode<T>?, _ replacementTail: ListNode<T>?) {
        var prefixTail = subRange.startIndex.previous
        var suffixHead = subRange.endIndex.node

        if prefixTail == nil {
            head = replacementHead ?? suffixHead
        } else {
            prefixTail?.next = replacementHead ?? suffixHead
        }

        if suffixHead == nil {
            tail = replacementTail
        } else {
            replacementTail?.next = suffixHead
        }
    }
    
    /// The type of nodes in `List`.
    private typealias Node = ListNode<T>

    /// The first node of `List`.
    private var head: Node?

    /// The last node of `List`.
    private var tail: Node?
}

// MARK: Queue/Stack

extension List : QueueType, StackType {
    public typealias Element = T

    /// Removes the `first` value at the head of `List` and returns it, `nil` if empty.
    public mutating func popFirst() -> T? {
        let value = head?.value
        self.removeAtIndex(startIndex)
        return value
    }

    /// Inserts a new `value` before the `first` value.
    public mutating func pushFirst(value: T) {
        prepend(value)
    }

    /// Inserts a new `value` after the `last` value.
    public mutating func pushLast(value: T) {
        append(value)
    }
}

// MARK: ArrayLiteralConvertible

extension List : ArrayLiteralConvertible {
    /// Initializes a `List` with the `elements` from array.
    public init(arrayLiteral elements: T...) {
        self.init(elements)
    }
}
{'fraction_non_alphanumeric': 0.11326248362207017, 'fraction_numerical': 0.001164652787887611, 'mean_word_length': 2.0065645514223194, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1947777', 'n_tokens_mistral': 2048, 'n_tokens_neox': 1967, 'n_words': 694}