Arena is an implementation of the generational arena data structure in Swift.

Overview

Arena

This package is very much work in progress.

Arena is an implementation of the generational arena data structure in Swift.

An Arena is useful for example for building data models with highly interdependent data (e.g. graphs).

Another example is data with the parent-child relationship. With the OOP approach, one would have to resort to using the weak which gets messy pretty fast.

So what's a generational arena? Think of it as an array which, unlike an array, has stable indices and which recycles indices.

What do we mean by stable indices? If you create an array with some elements, and then remove some elements, if you wanted to reference these elements by their indices, the indices you kept around are invalidated after you remove elements from the array.

With an Arena you can do the following:

var arena = Arena<Int>()
let idx0 = arena.insert(10)
let idx1 = arena.insert(20)

arena.remove(idx0)

assert(arena[idx1] == 20)

If we were using an array, we could theoretically only ever append new elements and if we remove elements, leave those slots empty but this would make the array grow very large in size very quickly.

Arena recycles indices so that the empty slots can be reused but if you have a dangling index (an index referencing a removed element) and then try to get the elements, you get a nil.

Arena performs lookups in O(1).

Here's an example of dealing with the parent child relationship.

import Arena

struct Node {
    var parent: Index
   ?
    
   var children: [
   Index
   
    ]
}


    var nodes 
    = Arena
    <Node
    >()


    let parentIndex 
    = nodes.
    insert(
    Node(
    parent: 
    nil, 
    children: []))


    let childIdx 
    = nodes.
    insert(
    Node(
    parent: parentIndex, 
    children: []))


    // the closure has mutable access to the element of the Arena

    nodes.
    transform(
    at: parentIndex) { 
    
    $0.
    children.
    append(childIdx)
}

   
  

When using an Arena, you keep around the indices which you then use to lookup the actual data in the arena.

I invite you to give this approach to programming a try, if your data is highly interdependent, OOP just doesn't cut it.

Arena is heavily inspired by the generational-arena Rust crate https://github.com/fitzgen/generational-arena.

You might also like...
Simple diff library in pure Swift

Diff Simple diffing library in pure Swift. Installing You can use Carthage or Swift Package Manager to install Diff. Usage Start by importing the pack

A functional tool-belt for Swift Language similar to Lo-Dash or Underscore.js in Javascript

Dollar Dollar is a Swift library that provides useful functional programming helper methods without extending any built in objects. It is similar to L

Swift type modelling the success/failure of arbitrary operations.

Result This is a Swift µframework providing ResultValue, Error. ResultValue, Error values are either successful (wrapping Value) or failed (wrappi

Swift μ-framework for efficient array diffs and datasource adapters.
Swift μ-framework for efficient array diffs and datasource adapters.

Buffer Swift μ-framework for efficient array diffs, collection observation and data source implementation. C++11 port here Installation cd {PROJECT_RO

A Generic Priority Queue in Pure Swift

SwiftPriorityQueue SwiftPriorityQueue is a pure Swift (no Cocoa) implementation of a generic priority queue data structure, appropriate for use on all

Super lightweight DB written in Swift.
Super lightweight DB written in Swift.

Use of value types is recommended and we define standard values, simple structured data, application state and etc. as struct or enum. Pencil makes us

A fast Swift diffing library.

HeckelDiff Pure Swift implementation of Paul Heckel's A Technique for Isolating Differences Between Files Features This is a simple diff algorithm tha

NSCoding's counterpart for Swift structs.
NSCoding's counterpart for Swift structs.

Dekoter Why You Might Be Interested How Much Familiar It Feels One More Example What We've Learned from It Features Save an Object to UserDefaults Arc

A Distributed Value Store in Swift.
A Distributed Value Store in Swift.

Impeller is a Distributed Value Store (DVS) written in Swift. It was inspired by successful Distributed Version Control Systems (DVCSes) like Git and

Owner
ngrid.io, femtovg core team member, AudioKit core team member, nalgebra contributor
null
Unidirectional, transactional, operation-based Store implementation.

Unidirectional, transactional, operation-based Store implementation for Swift and SwiftUI Overview Store eschews MVC in favour of a unidirectional dat

Alex Usbergo 502 Dec 9, 2022
Commonly used data structures for Swift

Swift Collections is an open-source package of data structure implementations for the Swift programming language.

Apple 2.7k Jan 5, 2023
Examples of commonly used data structures and algorithms in Swift.

Swift Structures This project provides a framework for commonly used data structures and algorithms written in a new iOS development language called S

Wayne Bishop 2.1k Dec 28, 2022
Algorithms and data structures in Swift, with explanations!

Welcome to the Swift Algorithm Club! Here you'll find implementations of popular algorithms and data structures in everyone's favorite new language Sw

raywenderlich 27.3k Jan 8, 2023
The simplest abstraction to synchronize local data with remote source. For iOS, wirtten in swift.

Purpose The simplest abstraction to synchronize local data with remote source. For iOS, written in swift. Overview Many applications uses remote serve

Siarhei Ladzeika 7 Mar 17, 2022
Simple implementations of various dynamic data structures in Swift.

SwiftDataStructures Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installatio

Hector Delgado 0 Oct 21, 2021
EKAlgorithms contains some well known CS algorithms & data structures.

EKAlgorithms EKAlgorithms is a set of computer exercises implemented in Objective-C. Data structures, well known algorithms, CS curiosities, you name

Evgeny Karkan 2.4k Jan 4, 2023
KeyPathKit is a library that provides the standard functions to manipulate data along with a call-syntax that relies on typed keypaths to make the call sites as short and clean as possible.

KeyPathKit Context Swift 4 has introduced a new type called KeyPath, with allows to access the properties of an object with a very nice syntax. For in

Vincent Pradeilles 406 Dec 25, 2022
Swift-extensions - Swift package extending the Swift programming language.

swift-extensions A package containing extensions for the Swift programming language. Contribution Reporting a bug If you find a bug, please open a bug

Alexandre H. Saad 2 Jun 12, 2022
Fast sorted collections for Swift using in-memory B-trees

Fast Sorted Collections for Swift Using In-Memory B-Trees Overview Reference Documentation Optimizing Collections: The Book What Are B-Trees? Why In-M

null 1.3k Dec 20, 2022