Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit array size in swift

Tags:

arrays

swift

Is there a way to limit array size in swift so that when you append elements to it when it's full, it doesn't append?

I know this can be done programmatically. Wondering if swift has inbuilt handling for this.

eg:

array -> Array with size 10

array.append(1)

.

.

.

array.append(10)

array.append(11) // Doesn't do anything?

array.insert(0, pos: 0) 

Use Case: Pushes the last element out of the array to make space for the new element?

Edit - last line is the main use case.

like image 229
Rakesha Shastri Avatar asked Mar 07 '18 05:03

Rakesha Shastri


2 Answers

Nope, Swift doesn't come with this kind of array - which is similar to a View from a database - allowing you to peek the first N elements. Though for this both the view and it target should be reference types, which is not the case in Swift for arrays.

But enough blabbing around, you could quickly write a wrapper over an Array for fulfilling your needs:

/// an array-like struct that has a fixed maximum capacity
/// any element over the maximum allowed size gets discarded
struct LimitedArray<T> {
    private(set) var storage: [T] = []
    public let maxSize: Int

    /// creates an empty array
    public init(maxSize: Int) {
        self.maxSize = maxSize
    }

    /// takes the max N elements from the given collection
    public init<S: Sequence>(from other: S, maxSize: Int) where S.Element == T {
        self.maxSize = maxSize
        storage = Array(other.prefix(maxSize))
    }

    /// adds a new item to the array, does nothing if the array has reached its maximum capacity
    /// returns a bool indicated the operation success
    @discardableResult public mutating func append(_ item: T) -> Bool {
        if storage.count < maxSize {
            storage.append(item)
            return true
        } else {
            return false
        }
    }

    /// inserts an item at the specified position. if this would result in
    /// the array exceeding its maxSize, the extra element are dropped
    public mutating func insert(_ item: T, at index: Int) {
        storage.insert(item, at: index)
        if storage.count > maxSize {
            storage.remove(at: maxSize)
        }
    }

    // add here other methods you might need
}

// let's benefit all the awesome operations like map, flatMap, reduce, filter, etc
extension LimitedArray: MutableCollection {
    public var startIndex: Int { return storage.startIndex }
    public var endIndex: Int { return storage.endIndex }

    public subscript(_ index: Int) -> T {
        get { return storage[index] }
        set { storage[index] = newValue }
    }

    public func index(after i: Int) -> Int {
        return storage.index(after: i)
    }
}

Since the struct conforms to Collection, you can easily pass it to code that knows only to work with arrays by transforming its contents into an array: Array(myLimitedArray).

like image 140
Cristik Avatar answered Sep 22 '22 21:09

Cristik


simply count the elements of array before appending new element.

var array :[Int] = [1,2,3,4,5,6,7,8,9,10]

func insertIntoArray(_ value: Int, array: [Int]) -> [Int] {
    var arr = array
    if arr.count == 10 {
        arr.removeLast()
    }
    arr.append(value)
    return arr
}

array = insertIntoArray(11, array: array)
like image 28
Uday Babariya Avatar answered Sep 26 '22 21:09

Uday Babariya