Latest 0.2.3
Homepage https://github.com/QuqqU/SplayDict
License MIT
Platforms ios 9.0
Authors

CI Status
Version
License
Platform

SplayDict written in Swift, is a collection type like special ordered set.
It’s based on Splay tree which reflects tendency of search input data.

What is SplayDict?

SplayDict is Dictionary using splay tree, not red-black tree.
Due to attribution of splay tree, it is very fast to get data searched frequently.

In real world, so many search are requested. And many people requset a same(or similar) data. Every request, every new search. It’s very consumptively.

Using SplayDict, it’s not waste of time. It takes a very short time to get data searched frequently. (But, It takes a little more time to find data searched occasionally.)
According to Pareto’s law, using SplayDict makes a program be productive, and even faster.

TODO

  • travis (I don’t know how to do…)
  • SplayTree
  • flatMap
  • SplayDict to Json
  • Subscript
  • More methods
  • Adopt protocols such as CustomStringConvertible, Sequence, etc.

Requirement

  • iOS 10.0+
  • Xcode 9+


SplayDict is available through CocoaPods. To install
it, simply add the following line to your Podfile:

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it using the following command.

$ sudo gem install cocoapods --pre

Cocoapod 1.5.3+ is required.

To use SplayDict in your project using CocoaPod, add the following code in your Podfile.

platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'SplayDict', '~> 0.2.3'
end

After you edit Podfile, then run the command below.

$ pod install

Usage

/////////////////////////////////////////////////////////////
///* Declaration *///
/////////////////////////////////////////////////////////////

var a: SplayDict<Int, Double> = SplayDict() // make a empty SplayDict.
var b = SplayDict([1, 3, 2]) // make a SplayDict which contains 1, 2, 3.

// and more constructor is building...

/////////////////////////////////////////////////////////////
///* Insert *///
/////////////////////////////////////////////////////////////

var a: SplayDict<Int, Int> = SplayDict()
a.insert(key: 3, value: 7)
// Insert [3 : 7] into SplayDict.

a[5] = 12
// Insert [5 : 12] into SplayDict.

// If you want to insert key which is already exists,
// then being value is replaced with newValue.

/////////////////////////////////////////////////////////////
///* Top *///
/////////////////////////////////////////////////////////////

// nil if SplayDict is empty.
let (a, b) = a.top!
// Return a top element key & value of SplayDict.
// It's a latest searched data.
// Read-only property

_ = a.top?.key
_ = a.topKey
// Return top element key.

_ = a.top?.value
_ = a.topValue
// Return top element value

/////////////////////////////////////////////////////////////
///* Find *///
/////////////////////////////////////////////////////////////

var a: SplayDict<Int, String> = SplayDict()
let value: String? = a.find(key: 7) 
// Return value if search value exists, else return nil.
// And this make searched key(or nearest key) be on a top of SplayDict.

/////////////////////////////////////////////////////////////
///* Delete *///
/////////////////////////////////////////////////////////////

a.delete(key: 5)
// Delete element corresponded with key in SplayDict.
// Make a biggest key under input be a top element.

/////////////////////////////////////////////////////////////
///* Read *///
/////////////////////////////////////////////////////////////

var a: SplayDict<Int, Double> = SplayDict()
let value: Double? = a.indexOf(3)
// Return n-th value if SplayDict.
// If index is out of range, returns nil.

_ = a[4]
// You can use subscript to read value related to key.
// If there is no key corresponding to input, return value as nil.

- = a[12]{50}
// You can set default value rather than nil.
// If there is key corresponding to input, return value related to input key.
// Otherwise, return default value.

_ = a.elements
// Return all [key : value] elements orderly according to comparable key.

_ = a.keys
// Return all keys orderly.

_ = a.values
// Return all values unorderly.

/////////////////////////////////////////////////////////////
///* Custom String *///
/////////////////////////////////////////////////////////////

//- description
> [ (3 : 10), (4 : 7), (5 : 6) ]

//- debugDescription
>   ----- SplayDict -----
      key: 3, value: 10
      key: 4, value: 7
      key: 5, value: 6 
|||---/ SplayDict /---/

/////////////////////////////////////////////////////////////
///* Other Operations *///
/////////////////////////////////////////////////////////////

a.reverse(from: 3, to: 6)
// Reverse elements. (from 3 to 6)
// Implement using Lazy Propagtion.

// example
// Let splay set consists of [0, 1, 2, 3, 4, 5, 6].
// reverse(from: 3, to : 6).
// The result is [0, 1, 2, 6, 5, 4, 3].

Contact

QuqqU(KiUng Jung)

License

Copyright QuqqU(KiUng Jung) 2018 ~
SplayDict is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "SplayDict",
    "version": "0.2.3",
    "summary": "It is a collection type based on law of Pareto, splay tree.",
    "description": "SplayDict written in Swift, is a collection type like special ordered set. It is based on Splay tree which reflects tendency of search input data.",
    "homepage": "https://github.com/QuqqU/SplayDict",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "QuqqU": "[email protected]"
    },
    "source": {
        "git": "https://github.com/QuqqU/SplayDict.git",
        "tag": "0.2.3"
    },
    "platforms": {
        "ios": "9.0"
    },
    "source_files": "SplayDict/Classes/**/*"
}

Pin It on Pinterest

Share This