collections/internal/ArrayEdits.protos

Repository
guillermomolina/protos
Commit
2723a55555e7beecee0816bf99d16e3c8c1ae635
Tree
c36054e4e5abd7fc29a039b4e096cac2e8706492
Blob
aecb3cae77ce12df818492347c77c1d8f34ffa05
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

//! Private Array edits algorithms.

append: (array, value) => {
    Array.recognizes(array).ifFalse(() => {
        Error().signal()
    })
    Array(...array, value)
}

concat: (left, right) => {
    Array.recognizes(left).ifFalse(() => {
        Error().signal()
    })
    Array.recognizes(right).ifFalse(() => {
        Error().signal()
    })
    Array(...left, ...right)
}

slice: (array, start, end) => {
    Array.recognizes(array).ifFalse(() => {
        Error().signal()
    })
    Integer.recognizes(start).ifFalse(() => {
        Error().signal()
    })
    Integer.recognizes(end).ifFalse(() => {
        Error().signal()
    })

    snapshot: Array(...array)
    size: snapshot.size()

    (((0 <= start) && (start <= end)) && (end <= size)).ifFalse(() => {
        Error().signal()
    })

    // Answers a fresh Array of `count` null placeholders in O(count).
    sized: null
    sized = (count) => {
        result: Array()
        if(count > 0) {
            half: sized(count.div(2))
            result = Array(...half, ...half)
            if((count % 2) == 1) {
                result = Array(...result, null)
            }
        }
        result
    }

    count: end - start
    result: sized(count)
    index: 0
    while(index < count) {
        result[index] = snapshot[start + index]
        index = index + 1
    }
    result
}

insertAt: (array, index, value) => {
    Array.recognizes(array).ifFalse(() => {
        Error().signal()
    })
    Integer.recognizes(index).ifFalse(() => {
        Error().signal()
    })

    result: Array(...array, value)
    n: result.size() - 1

    ((0 <= index) && (index <= n)).ifFalse(() => {
        Error().signal()
    })

    // Shift the suffix from the end so no pending element is overwritten.
    position: n
    while(position > index) {
        result[position] = result[position - 1]
        position = position - 1
    }
    result[index] = value
    result
}

removeAt: (array, index) => {
    Array.recognizes(array).ifFalse(() => {
        Error().signal()
    })
    Integer.recognizes(index).ifFalse(() => {
        Error().signal()
    })

    snapshot: Array(...array)
    n: snapshot.size()

    ((0 <= index) && (index < n)).ifFalse(() => {
        Error().signal()
    })

    // Answers a fresh Array of `count` null placeholders in O(count).
    sized: null
    sized = (count) => {
        result: Array()
        if(count > 0) {
            half: sized(count.div(2))
            result = Array(...half, ...half)
            if((count % 2) == 1) {
                result = Array(...result, null)
            }
        }
        result
    }

    result: sized(n - 1)
    target: 0
    while(target < (n - 1)) {
        source: target
        if(target >= index) {
            source = target + 1
        }
        result[target] = snapshot[source]
        target = target + 1
    }
    result
}

reverse: (array) => {
    Array.recognizes(array).ifFalse(() => {
        Error().signal()
    })

    result: Array(...array)
    left: 0
    right: result.size() - 1
    while(left < right) {
        swap: result[left]
        result[left] = result[right]
        result[right] = swap
        left = left + 1
        right = right - 1
    }
    result
}

take: (slice, array, count) => {
    Array.recognizes(array).ifFalse(() => {
        Error().signal()
    })
    Integer.recognizes(count).ifFalse(() => {
        Error().signal()
    })
    (count >= 0).ifFalse(() => {
        Error().signal()
    })

    snapshot: Array(...array)
    n: snapshot.size()
    if(count >= n) {
        ^snapshot
    }
    slice(snapshot, 0, count)
}

drop: (slice, array, count) => {
    Array.recognizes(array).ifFalse(() => {
        Error().signal()
    })
    Integer.recognizes(count).ifFalse(() => {
        Error().signal()
    })
    (count >= 0).ifFalse(() => {
        Error().signal()
    })

    snapshot: Array(...array)
    n: snapshot.size()
    if(count == 0) {
        ^snapshot
    }
    if(count >= n) {
        ^Array()
    }
    slice(snapshot, count, n)
}