OrderedDict is a data structure that preservers order of inserted keys. And is a sub-class of a regular object/dictionary. In addition it provides couple of convenient methods to push elements to start or end of a dict.
As an example: this data structure can be used to implement LRU-cache.
let OrderedDict = require("adict");
let dict = new OrderedDict();
dict.set(1, 2);
- Small 593b min and gzip
- Zero dependencies
- O(1) runtime complexity for all operations
OrderedDict is a data structure that preservers order of inserted keys.
And is a sub-class of a regular object/dictionary.
Implementation is inspired by python's OrderedDict
and this particular gist:
https://gist.github.com/joequery/12332f410a05e6c7c949
Click to expand!
Kind: Class
- OrderedDict()
- instance
- .set(key, value) ⇒
OrderedDict
- .delete(key) ⇒
boolean
- .clear() ⇒
undefined
- .get(key) ⇒
OrderedDictValue
|undefined
- .has(key) ⇒
boolean
- .pop() ⇒
undefined
|OrderedDictKeyValue
- .shift() ⇒
undefined
|OrderedDictKeyValue
- .toStart(key) ⇒
boolean
- .toEnd(key) ⇒
boolean
- .keys() ⇒
Iterator
- .values() ⇒
Iterator
- .entries() ⇒
Iterator
- .set(key, value) ⇒
- static
- instance
orderedDict.set(key, value) ⇒ OrderedDict
Add a new key-value pair to an ordered dict.
Kind: instance method of OrderedDict
Param | Type |
---|---|
key | OrderedDictKey |
value | any |
Delete a key from an ordered dict.
Kind: instance method of OrderedDict
Param | Type |
---|---|
key | OrderedDictKey |
Clear ordered dict.
Kind: instance method of OrderedDict
orderedDict.get(key) ⇒ OrderedDictValue
| undefined
Retrieve a key from an ordered dict.
Kind: instance method of OrderedDict
Param | Type |
---|---|
key | OrderedDictKey |
Check if key exists in an ordered dict.
Kind: instance method of OrderedDict
Param | Type |
---|---|
key | OrderedDictKey |
orderedDict.pop() ⇒ undefined
| OrderedDictKeyValue
Remove and return last element from an ordered dict.
Kind: instance method of OrderedDict
orderedDict.shift() ⇒ undefined
| OrderedDictKeyValue
Remove and return first element from an ordered dict.
Kind: instance method of OrderedDict
Move an existing element to the start of an orederd dict.
Kind: instance method of OrderedDict
Param | Type |
---|---|
key | OrderedDictKey |
Move an existing element to the end of an ordered dict.
Kind: instance method of OrderedDict
Param | Type |
---|---|
key | OrderedDictKey |
Returns new Iterator object that contains all keys of an ordered dict.
Kind: instance method of OrderedDict
Returns new Iterator object that contains all values of an ordered dict.
Kind: instance method of OrderedDict
Returns new Iterator object that contains all key-value pairs of an ordered dict.
Kind: instance method of OrderedDict
OrderedDict.from(data) ⇒ OrderedDict
Create an ordered dict from an array or object.
Kind: static method of OrderedDict
Param | Type |
---|---|
data | Array<OrderedDictKeyValue> | Object |