{"_id":"collection","_rev":"24-92c193813c79ed6127cbc7856ea85362","name":"collection","description":"Node.js cross-platform native collection library","dist-tags":{"latest":"0.1.6"},"versions":{"0.1.0":{"name":"collection","id":"collection","version":"0.1.0","description":"Node.js cross-platform native collection library","keywords":["collection","native","util","vector"],"author":{"name":"Thomas Feng","email":"tfeng@berkeley.edu"},"repository":{"type":"git","url":"https://github.com/tfeng/collection.git","web":"https://github.com/tfeng/collection"},"bugs":{"url":"https://github.com/tfeng/collection/issues"},"main":"./lib/collection.js","licenses":[{"type":"MIT","url":"http://opensource.org/licenses/MIT"}],"dependencies":{"bindings":"latest"},"scripts":{"install":"node-gyp rebuild"},"gypfile":true,"_id":"collection@0.1.0","dist":{"shasum":"b6fcd8c141dc11104e2e6a64211f39816a0285d4","tarball":"https://registry.npmjs.org/collection/-/collection-0.1.0.tgz","integrity":"sha512-nV7dazc/RahdAPrRMJ0ngSMlLMVPjv9KHe4YmK51pjWcB7X3mhSD5q9TGpcpseBLd4h2DvJ1nBa8oXt3PLb0fw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHNP3/L6u6Dc8VxBkY8sjML6TmF0a9DnDEH80qEx6cNVAiEAu1UI4gMjR+BkaxDxZYwtyKBvKDo4ml/yJvi1GoNpc1E="}]},"_from":"collection","_npmVersion":"1.3.0","_npmUser":{"name":"tfeng","email":"tfeng@berkeley.edu"},"maintainers":[{"name":"tfeng","email":"tfeng@berkeley.edu"}],"directories":{}},"0.1.1":{"name":"collection","id":"collection","version":"0.1.1","description":"Node.js cross-platform native collection library","keywords":["collection","native","util","vector"],"author":{"name":"Thomas Feng","email":"tfeng@berkeley.edu"},"repository":{"type":"git","url":"https://github.com/tfeng/collection.git","web":"https://github.com/tfeng/collection"},"bugs":{"url":"https://github.com/tfeng/collection/issues"},"main":"./lib/collection.js","licenses":[{"type":"MIT","url":"http://opensource.org/licenses/MIT"}],"dependencies":{"bindings":"latest"},"scripts":{"install":"node-gyp rebuild"},"gypfile":true,"_id":"collection@0.1.1","dist":{"shasum":"fe5fbcc29228e641f4ef0d3a855602b6f8dd512e","tarball":"https://registry.npmjs.org/collection/-/collection-0.1.1.tgz","integrity":"sha512-IMXyBZvBIudR+mzWeuUwBofeTMbFLwXmwst85oBX5MQyxxlx5Y7B9nKSEHczJY//CamLvvVnDuAUCXzqkjOovw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIAw1RQQO5jImXHr38CnIUNBoMZhPGdMiJcU2R26dwUqIAiA5wqhji7IoJCjIxIFraU9HXGD7aRnySDt638DNaCvejA=="}]},"_from":"collection/","_npmVersion":"1.3.0","_npmUser":{"name":"tfeng","email":"tfeng@berkeley.edu"},"maintainers":[{"name":"tfeng","email":"tfeng@berkeley.edu"}],"directories":{}},"0.1.2":{"name":"collection","id":"collection","version":"0.1.2","description":"Node.js cross-platform native collection library","keywords":["collection","native","util","vector"],"author":{"name":"Thomas Feng","email":"tfeng@berkeley.edu"},"repository":{"type":"git","url":"https://github.com/tfeng/collection.git","web":"https://github.com/tfeng/collection"},"bugs":{"url":"https://github.com/tfeng/collection/issues"},"main":"./lib/collection.js","licenses":[{"type":"MIT","url":"http://opensource.org/licenses/MIT"}],"dependencies":{"bindings":"latest"},"scripts":{"install":"node-gyp rebuild"},"gypfile":true,"_id":"collection@0.1.2","dist":{"shasum":"971ef0d5cc7bbc83b639a0d21e4cd8c35ecb504c","tarball":"https://registry.npmjs.org/collection/-/collection-0.1.2.tgz","integrity":"sha512-Hw+Oo40XhBL8F8zW274HdN/MJuEkyrGYeUHV04p5SycLmoeeNTIxwWBuXW59MHspi49pjFw33dwwCwYmsyXRaA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCDNkoBYxwF5NH2yRFX8oxtjlOIGSxafyqRTPBSsbi4ZgIhAOfvM3ghBscMFhZhqlyt7xkjyaJWD01xwC670PbOqnSB"}]},"_from":"collection/","_npmVersion":"1.3.0","_npmUser":{"name":"tfeng","email":"tfeng@berkeley.edu"},"maintainers":[{"name":"tfeng","email":"tfeng@berkeley.edu"}],"directories":{}},"0.1.3":{"name":"collection","id":"collection","version":"0.1.3","description":"Node.js cross-platform native collection library","keywords":["collection","native","util","vector","set","map"],"author":{"name":"Thomas Feng","email":"tfeng@berkeley.edu"},"homepage":"http://tfeng.github.io/collection","repository":{"type":"git","url":"https://github.com/tfeng/collection.git","web":"https://github.com/tfeng/collection"},"bugs":{"url":"https://github.com/tfeng/collection/issues"},"main":"./lib/collection.js","licenses":[{"type":"MIT","url":"http://opensource.org/licenses/MIT"}],"dependencies":{"bindings":"latest"},"scripts":{"install":"node-gyp rebuild"},"gypfile":true,"_id":"collection@0.1.3","dist":{"shasum":"be6ba416c9f211cb15b4c5b9e594b0b748787b08","tarball":"https://registry.npmjs.org/collection/-/collection-0.1.3.tgz","integrity":"sha512-LUiP9R4iO1E8IlPFWYCOImppYBOvaF39n51yLZOYePtzlinCe4EKMkXZ5hQJ32R/fxgvy5ntDMhqGOY/HiezIA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFL8hVbcpqBCIGgD6QuBmGO+kyPPwv3qfPicffroTLtUAiEAgrQdssM+jKc4LfKpnEgE8BECltRDKGd/LheKjIavQ7E="}]},"_from":"collection","_npmVersion":"1.3.0","_npmUser":{"name":"tfeng","email":"tfeng@berkeley.edu"},"maintainers":[{"name":"tfeng","email":"tfeng@berkeley.edu"}],"directories":{}},"0.1.4":{"name":"collection","id":"collection","version":"0.1.4","description":"Node.js cross-platform native collection library","keywords":["collection","native","util","vector","set","map"],"author":{"name":"Thomas Feng","email":"tfeng@berkeley.edu"},"homepage":"http://tfeng.github.io/collection","repository":{"type":"git","url":"https://github.com/tfeng/collection.git","web":"https://github.com/tfeng/collection"},"bugs":{"url":"https://github.com/tfeng/collection/issues"},"main":"./lib/collection.js","licenses":[{"type":"MIT","url":"http://opensource.org/licenses/MIT"}],"dependencies":{"bindings":"latest"},"scripts":{"install":"node-gyp rebuild"},"gypfile":true,"_id":"collection@0.1.4","dist":{"shasum":"00d16a912938e89d8e67232cb3f8ffde257d4dd6","tarball":"https://registry.npmjs.org/collection/-/collection-0.1.4.tgz","integrity":"sha512-FU1ZhrHWyMeYCQWS2SBnU0wS5Z/5RtBZkrPYaxic6EjVO50dJ60ymej88xbCn+sZPmiZcrAqGxnuUW2KTPGfdQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD7yygRkU2wAvwZVgbo/TTIcgfeLE8MKXXlAgY6Rb15fwIgQ5kkaeLuvG8U0Ks2KMF5XDtLIBiGQJ5zJGS5m9MxDiQ="}]},"_from":".","_npmVersion":"1.3.0","_npmUser":{"name":"tfeng","email":"tfeng@berkeley.edu"},"maintainers":[{"name":"tfeng","email":"tfeng@berkeley.edu"}],"directories":{}},"0.1.5":{"name":"collection","id":"collection","version":"0.1.5","description":"Node.js cross-platform native collection library","keywords":["collection","native","util","vector","set","map"],"author":{"name":"Thomas Feng","email":"tfeng@berkeley.edu"},"homepage":"http://tfeng.github.io/collection","repository":{"type":"git","url":"https://github.com/tfeng/collection.git","web":"https://github.com/tfeng/collection"},"bugs":{"url":"https://github.com/tfeng/collection/issues"},"main":"./lib/collection.js","licenses":[{"type":"MIT","url":"http://opensource.org/licenses/MIT"}],"dependencies":{"bindings":"latest"},"scripts":{"install":"node-gyp rebuild"},"gypfile":true,"gitHead":"e8e75ab11fbe141603f0db84dfb9c5efb5d5fe20","_id":"collection@0.1.5","_shasum":"908c43db1a29ca34b03d23dce807a50b490260bc","_from":"./","_npmVersion":"1.4.13","_npmUser":{"name":"tfeng","email":"tfeng@berkeley.edu"},"maintainers":[{"name":"tfeng","email":"tfeng@berkeley.edu"}],"dist":{"shasum":"908c43db1a29ca34b03d23dce807a50b490260bc","tarball":"https://registry.npmjs.org/collection/-/collection-0.1.5.tgz","integrity":"sha512-p06ZI74UVyAXbmWPtVoZzgYCUV/TcYbcSo7x43l0BfBjPHjgd/3Faz9oy/et29nuODGzWlMSq7eqPDnLfzfgtA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQD0P9QYfCC6Bq4Cx+BGGJrjpWmr42qcdH1ahO56PTr1UgIhAIkAAA2ZYH3Y1879Xr4ceL1RWuUoc5dwV3N2ZIZ/V/zw"}]},"directories":{}},"0.1.6":{"name":"collection","id":"collection","version":"0.1.6","description":"Node.js cross-platform native collection library","keywords":["collection","native","util","vector","set","map"],"author":{"name":"Thomas Feng","email":"tfeng@berkeley.edu"},"homepage":"http://tfeng.github.io/collection","repository":{"type":"git","url":"https://github.com/tfeng/collection.git","web":"https://github.com/tfeng/collection"},"bugs":{"url":"https://github.com/tfeng/collection/issues"},"main":"./lib/collection.js","licenses":[{"type":"MIT","url":"http://opensource.org/licenses/MIT"}],"dependencies":{"bindings":"latest"},"scripts":{"install":"node-gyp rebuild"},"gypfile":true,"gitHead":"0e7545cfd5f5b02d3fa5dba8e6b95d0a1f6724f9","_id":"collection@0.1.6","_shasum":"971cf4f80f598f5f170a7067d1ac2083099580fd","_from":".","_npmVersion":"1.4.28","_npmUser":{"name":"tfeng","email":"tfeng@berkeley.edu"},"maintainers":[{"name":"tfeng","email":"tfeng@berkeley.edu"}],"dist":{"shasum":"971cf4f80f598f5f170a7067d1ac2083099580fd","tarball":"https://registry.npmjs.org/collection/-/collection-0.1.6.tgz","integrity":"sha512-k2DYQSq4q22TVkx/myGyclUO1lcathv07eIzG0CwC+s0+cwwMuqtBKRxPnEYkEwi8CbUpywePk7Eq19BgOuZ2A==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDbhb26C7a2QuZUQA8Ftrn5Bmkym4Yi+B5U1sDrcGY1YAiAeWjjb9UcQTMiURFZO/nuUtKDcgVKSPUr0e2x16XECsw=="}]},"directories":{}}},"readme":"collection.js\n==========\n\n### Node.js cross-platform native collection library\n\nJavaScript has limited support for collections. Associate array and objects are normally used in case where lists, sets and maps are needed. Unfortunately, the performance is not always as desired, especially for websites built and run with Node.js that need to be highly efficient.\n\n`collection.js` is a native implementation of such data types as vector, set and map, based on their respective underlying implementation in C++. These data types are highly efficient due to the fact that operations on these data types take advantage of the underlying data structures that represent them, and that most of the heavy-lifting work is done at the C++ level, such as looking up a key in a map and modifying elements of a vector while iterating over them.\n\nExperimental result has shown that in many use cases, `collection.js` significantly improves not only the time it takes to run an algorithm with intensive data access or manipulation, but also the amount of data objects that can fit in Node.js' process memory.\n\n- [Overview](#overview)\n\t- [Supported Collection Types](#supported-collection-types)\n\t\t- [Vector](#vector)\n\t\t- [Set](#set)\n\t\t- [Map](#map)\n\t- [Setup](#setup)\n\t\t- [Prerequisite](#prerequisite)\n\t\t- [Installation](#installation)\n\t\t- [Testing](#testing)\n- [API Documentation](#api-documentation)\n\t- [Vector](#vector-1)\n\t\t- [add(value, ...)](#addvalue-)\n\t\t- [addAll(object)](#addallobject)\n\t\t- [clear()](#clear)\n\t\t- [each(callback)](#eachcallback)\n\t\t- [equals(object)](#equalsobject)\n\t\t- [filter(callback)](#filtercallback)\n\t\t- [find(callback)](#findcallback)\n\t\t- [get(index, ...)](#getindex-)\n\t\t- [has(value, ...)](#hasvalue-)\n\t\t- [index(value, ...)](#indexvalue-)\n\t\t- [isEmpty()](#isempty)\n\t\t- [map(callback)](#mapcallback)\n\t\t- [reduce(callback, memo)](#reducecallback-memo)\n\t\t- [reduceRight(callback, memo)](#reducerightcallback-memo)\n\t\t- [remove(value, ...)](#removevalue-)\n\t\t- [removeAt(index, ...)](#removeatindex-)\n\t\t- [removeLast()](#removelast)\n\t\t- [removeRange(start, end)](#removerangestart-end)\n\t\t- [reverse()](#reverse)\n\t\t- [set(index, value)](#setindex-value)\n\t\t- [size()](#size)\n\t\t- [toArray()](#toarray)\n\t\t- [toString()](#tostring)\n\t- [Set](#set-1)\n\t\t- [add(value, ...)](#addvalue--1)\n\t\t- [addAll(object)](#addallobject-1)\n\t\t- [clear()](#clear-1)\n\t\t- [each(callback)](#eachcallback-1)\n\t\t- [equals(object)](#equalsobject-1)\n\t\t- [filter(callback)](#filtercallback-1)\n\t\t- [find(callback)](#findcallback-1)\n\t\t- [get(index, ...)](#getindex--1)\n\t\t- [has(value, ...)](#hasvalue--1)\n\t\t- [index(value, ...)](#indexvalue--1)\n\t\t- [isEmpty()](#isempty-1)\n\t\t- [reduce(callback, memo)](#reducecallback-memo-1)\n\t\t- [reduceRight(callback, memo)](#reducerightcallback-memo-1)\n\t\t- [remove(value, ...)](#removevalue--1)\n\t\t- [removeAt(index, ...)](#removeatindex--1)\n\t\t- [removeLast()](#removelast-1)\n\t\t- [removeRange(start, end)](#removerangestart-end-1)\n\t\t- [size()](#size-1)\n\t\t- [toArray()](#toarray-1)\n\t\t- [toString()](#tostring-1)\n\t- [Map](#map-1)\n\t\t- [clear()](#clear-2)\n\t\t- [each(callback)](#eachcallback-2)\n\t\t- [equals(object)](#equalsobject-2)\n\t\t- [filter(callback)](#filtercallback-2)\n\t\t- [find(callback)](#findcallback-2)\n\t\t- [get(key, ...)](#getkey-)\n\t\t- [getAt(index, ...)](#getatindex-)\n\t\t- [has(key, ...)](#haskey-)\n\t\t- [isEmpty()](#isempty-2)\n\t\t- [reduce(callback, memo)](#reducecallback-memo-2)\n\t\t- [reduceRight(callback, memo)](#reducerightcallback-memo-2)\n\t\t- [remove(key, ...)](#removekey-)\n\t\t- [removeAt(index, ...)](#removeatindex--2)\n\t\t- [removeLast()](#removelast-2)\n\t\t- [removeRange(start, end)](#removerangestart-end-2)\n\t\t- [set(key, value)](#setkey-value)\n\t\t- [setAll(object)](#setallobject)\n\t\t- [size()](#size-2)\n\t\t- [toArray()](#toarray-2)\n\t\t- [toObject()](#toobject)\n\t\t- [toString()](#tostring-2)\n\nOverview\n----------\n\n### Supported Collection Types\n\n#### Vector\n\nWith resizable array as its underlying data structure, `vector` is an ideal choice for random access. Operations of the `vector` collection type allow users to access elements by index in constant time. The array may potentially grow while new elements are being added to the `vector`, whereas the amortized time is still constant.\n\nA set of handy functions are provided for processing elements in `vector`, such as `find`, `filter`, `map` and `reduce`. Among these, there is also a very useful function `each`, which not only allows one to iterate over elements in the `vector`, but also to modify its elements on the fly, in a way iterator in C++ and Java does.\n\n#### Set\n\nA `set` is a sorted tree in its data representation. Existence of an element can be tested efficiently by traversing the tree instead of going through all the contained elements. As opposed to `hash set`, such a data structure does not offer constant lookup time, but it is generally more compact, and does not require a good `hash function`.\n\n`Set` also provides an `each` function that can be used to traverse its elements in order (defined below). It, however, does not allows to modify elements on the fly because such modification may alter the traversal.\n\n#### Map\n\nA `map` can be viewed as a set of `entries`. Each entry is a (`key`, `value`) pair. No two entries with the same key exist in a `map`.\n\nInternally, `map` is represented by a tree sorted by its keys, in a way similar to `set` that keeps its elements sorted. A `map`, however, has the additional capability of associating values with keys, but values do not participate in sorting.\n\nThe `put` operation is to put an entry in to the map, so that the key of the entry are inserted into the proper location of the tree. If the same key already exists, its currently associated value will be replaced.\n\n`Each` method of a `map` can be used to iterate over entries.\n\n### Setup\n\n#### Prerequisite\n\nSee [node-gyp](https://github.com/TooTallNate/node-gyp) documentation (under Installation section) for software packages required to be installed before proceeding.\n\n#### Installation\n\nInstall `collection.js` using the following command:\n\n``` bash\n$ npm install collection\n```\n\n#### Testing\n\nAfter installing `collection.js`, start `node` to see whether it is working as expected.\n\n```node\n$ node\n> var Vector = require(\"collection\").Vector,\n      Set = require(\"collection\").Set,\n      Map = require(\"collection\").Map;\nundefined\n> var v = new Vector([1,2,3]),\n      s = new Set([\"a\",\"b\",\"c\"]),\n      m = new Map({\"1\":\"a\",\"2\":\"b\",\"3\":\"c\"});\nundefined\n> v.toString();\n'[1,2,3]'\n> s.toArray();\n[ 'a', 'b', 'c' ]\n> m.toObject()\n{ '1': 'a',\n  '2': 'b',\n  '3': 'c' }\n> v.each(function(v, m) { m.set(v+1); }).toArray();\n[ 2, 3, 4 ]\n>\n```\n\nAPI Documentation\n----------\n\n### Vector\n\nExamples below assume vector `v` is initialized with elements `1, 2, 3, 4`:\n\n```node\n> var v = new Vector([1,2,3,4]);\nundefined\n> v.toString();\n'[1,2,3,4]'\n```\n\n#### add(value, ...)\n\nAdd one or more values to the end of this vector. If a value is an array or a collection, the same instance of array or collection is added to the vector.\n\n*Return:* This vector.\n\n```node\n> v.add(5,6,7,8).toArray();\n[ 1, 2, 3, 4, 5, 6, 7, 8 ]\n```\n\n#### addAll(object)\n\nAdd all the elements in `object` to the end of this vector. Supported types of `object` parameter are array, vector and set.\n\n*Return:* This vector.\n\n```node\n> v.addAll(new Vector([5,6,7])).toArray();\n[ 1, 2, 3, 4, 5, 6, 7 ]\n> v.addAll([\"x\",\"y\",\"z\"]).toArray();\n[ 1, 2, 3, 4, 5, 6, 7, 'x', 'y', 'z' ]\n```\n\n#### clear()\n\nRemove all elements from this vector.\n\n*Return:* This vector.\n\n```node\n> v.clear().toArray();\n[]\n```\n\n#### each(callback)\n\nIterate over elements of this vector, and invoke the `callback` function for each element. The callback function should be of the form `function(v, m) { ... }`. `v` is the element. `m` is a `vector modifier`, which may be used to inspect properties of the iteration as well as modify the vector. The callback function may return `false` indicating the iteration should stop immediately. Any other returned value is ignored.\n\n*Vector modifier:* It is the object `m` passed into the callback function for `each`. It supports the following operations: `isFirst()` to return whether the current invocation is the first of the iteration. `isLast()` to return whether the current invocation is the last. `index()` to return the current index. `set(value)` to set the substitute the current element. `remove()` to remove the current element. `insertBefore(value, ...)` to insert new elements before the current element. `insertAfter(value, ...)` to insert new elements after the current element. All changes are recorded during the callback executes, and are made only after the callback returns. Newly inserted elements are not included in the iteration.\n\n*Return:* This vector.\n\n```node\n> v.each(function(v, m){if(v%2==0){console.log(v); return false;}});\n2\n> v.each(function(v, m){if(m.isFirst()) console.log(v);});\n1\n> v.each(function(v, m){console.log(m.index()); m.insertBefore(0);}).toArray();\n0\n2\n4\n6\n[ 0, 1, 0, 2, 0, 3, 0, 4 ]\n> v.each(function(v, m){m.set(0);}).toArray();\n[ 0, 0, 0, 0, 0, 0, 0, 0 ]\n> v.each(function(v, m){m.remove();}).toArray();\n[]\n```\n\n#### equals(object)\n\nTest whether this vector equals the given object.\n\n*Equality:* In `collection.js`, equality is defined as `deep equality` between two objects, including their types and values. For example, a vector never equals an array because their types are different, though a vector may be converted into an array using the `toArray` method. Similarly, a vector containing integer value `1` does not equal anther one containing string `\"1\"`. Two vectors equal if and only if the contain equal elements in the same order.\n\n*Return:* `true` or `false`.\n\n```node\n> v.equals(new Vector([1,2,3,4]));\ntrue\n```\n\n#### filter(callback)\n\nReturn in an array all elements that make the `callback` function return `true`. The callback function should be of the form `function(v) { ... }`.\n\n*Return:* An array of elements for which the callback function returns `true`.\n\n```node\n> v.filter(function(v){return v%2==0});\n[ 2, 4 ]\n```\n\n#### find(callback)\n\nReturn the first element of this vector that makes the `callback` function return `true`. The callback function should be of the form `function(v) { ... }`.\n\n*Return:* The first element of this vector for which the callback function returns `true`.\n\n```node\n> v.find(function(v){return v%2==0});\n2\n```\n\n#### get(index, ...)\n\nGet elements of this vector at the specified indexes.\n\n*Return:* If only 1 index is given as parameter, element of this vector at that index; if multiple indexes are given, an array of elements at those indexes.\n\n```node\n> v.get(0)\n1\n> v.get(0,1,5,3)\n[ 1, 2, , 4 ]\n```\n\n#### has(value, ...)\n\nCheck whether the given values exist in the vector.\n\n*Return:* If only 1 value is given as parameter, `true` or `false`; if multiple values are given, an array of boolean values, each identifying whether the corresponding value exists.\n\n```node\n> v.has(2,4,6)\n[ true, true, false ]\n```\n\n#### index(value, ...)\n\nReturn indexes of the specified elements in this vector.\n\n*Return:* If only 1 value is given as parameter, index of that element, or `undefined` if not found; if multiple values are given, an array of indexes.\n\n```node\n> v.index(2)\n1\n> v.index(1,3,5,7)\n[ 0, 2 ]\n> v.index(1,5,7,3)\n[ 0, , , 2 ]\n```\n\n#### isEmpty()\n\nTest whether the vector is empty.\n\n*Return:* `true` or `false`.\n\n```node\n> v.isEmpty();\nfalse\n```\n\n#### map(callback)\n\nIterates over all elements of this vector, invoke the `callback` function for each element, and when the callback function returns, replace the current element with a new value. The callback function should be of the form `function(v){ ... }`. Its returned value substitutes elements of this vector, one at a time.\n\n*Return:* This vector.\n\n```node\n> v.map(function(v){return v*2}).toArray();\n[ 2, 4, 6, 8 ]\n> v.map(function(v){}).toArray();\n[ undefined, undefined, undefined, undefined ]\n```\n\n#### reduce(callback, memo)\n\nIterates over all elements of this vector and invoke the `callback` function for each element. Each time a `memo` value is passed into the callback, and the return value of the callback becomes the memo value of the next iteration. The return value in the last iteration is the return value of the `reduce` function itself. The callback should be of the form `function(memo, v){ ... }`.\n\n*Return:* The return value of the callback function in the last iteration.\n\n```node\n> v.reduce(function(memo, v){return memo+v}, 0);\n10\n> v.reduce(function(memo, v){return memo*v}, 1);\n24\n```\n\n#### reduceRight(callback, memo)\n\nPerform the same function as `reduce`, but iterate elements of this vector in reverse order.\n\n*Return:* The return value of the callback function in the last iteration.\n\n```node\n> v.reduceRight(function(memo, v){return v}, 5);\n1\n```\n\n#### remove(value, ...)\n\nRemove elements from this vector.\n\n*Return:* This vector.\n\n```node\n> v.remove(1,3,5).toArray();\n[ 2, 4 ]\n```\n\n#### removeAt(index, ...)\n\nRemove elements at the specified indexes from this vector. For any index greater than size of this vector, there is no effect.\n\n*Return:* This vector.\n\n```node\n> v.removeAt(0,3,5).toArray();\n[ 2, 3 ]\n```\n\n#### removeLast()\n\nRemove last element of this vector. If this vector is empty, there is no effect.\n\n*Return:* This vector.\n\n```node\n> v.removeLast().removeLast().toArray();\n[ 1, 2 ]\n> v.removeLast().removeLast().removeLast().removeLast().toArray();\n[]\n```\n\n#### removeRange(start, end)\n\nRemove elements between the `start` index (inclusive) and the `end` index (exclusive). If `start` is greater than or equal to size of this vector, there is no effect. If `end` is greater than or equal to size of this vector, all elements from `start` index are removed.\n\n*Return:* This vector.\n\n```node\n> v.removeRange(9,20).toArray();\n[ 1, 2, 3, 4 ]\n> v.removeRange(0,0).toArray();\n[ 1, 2, 3, 4 ]\n> v.removeRange(0,1).toArray();\n[ 2, 3, 4 ]\n> v.removeRange(1,999).toArray();\n[ 2 ]\n```\n\n#### reverse()\n\nReverse this vector.\n\n*Return:* This vector.\n\n```node\n> v.reverse().toArray();\n[ 4, 3, 2, 1 ]\n> v.reverse().toArray();\n[ 1, 2, 3, 4 ]\n```\n\n#### set(index, value)\n\nSet a new value at a given index. If the index is less than size of this vector, existing value is substituted by the given new value. If the index equals size of this vector, the given value is added to the end of the vector. If the index is greater than size of this vector, an `Error` is thrown.\n\n*Return:* This vector.\n\n```node\n> v.set(2,5).toArray();\n[ 1, 2, 5, 4 ]\n> v.set(4,6).toArray();\n[ 1, 2, 5, 4, 6 ]\n```\n\n#### size()\n\nCheck the size of the vector.\n\n*Return:* Number of elements in this vector.\n\n```node\n> v.size();\n4\n```\n\n#### toArray()\n\nConvert this vector into an array that contains the same elements. If the vector contains an array or a collection as its element, the same instance of array or collection will also be an element of the resulting array.\n\n*Return:* An array.\n\n```node\n> v.toArray();\n[ 1, 2, 3, 4 ]\n> v.add([\"a\", \"b\"]).toArray();\n[ 1, 2, 3, 4, [ 'a', 'b' ] ]\n> v.toArray()[4].push(\"c\");\n3\n> v.toArray();\n[ 1, 2, 3, 4, [ 'a', 'b', 'c' ] ]\n```\n\n#### toString()\n\nConvert this vector into a string.\n\n*Return:* A string.\n\n```node\n> v.toString();\n'[1,2,3,4]'\n```\n\n### Set\n\nExamples below assume set `s` is initialized with elements `1, 2, 3, 4`:\n\n```node\n> var s = new Set([1,2,3,4]);\nundefined\n> s.toString();\n'[1,2,3,4]'\n```\n\n#### add(value, ...)\n\nAdd one or more values to this set, and keep the elements in sorted order. If a value is an array or a collection, the same instance of array or collection is added to the set.\n\n*Return:* This set.\n\n```node\n> s.add(5,6,7,8).toArray();\n[ 1, 2, 3, 4, 5, 6, 7, 8 ]\n> s.add(0,2,4,6,8).toArray()\n[ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]\n```\n\n#### addAll(object)\n\nAdd all the elements in `object` to this set, and keep the elements in sorted order. Supported types of `object` parameter are array, vector and set.\n\n*Return:* This set.\n\n```node\n> s.addAll(new Vector([3,4,5,6,7])).toArray();\n[ 1, 2, 3, 4, 5, 6, 7 ]\n> s.addAll([\"x\",\"y\",\"z\"]).toArray();\n[ 1, 2, 3, 4, 5, 6, 7, 'x', 'y', 'z' ]\n> s.addAll(new Vector([0,1,2,3])).toArray();\n[ 0, 1, 2, 3, 4, 5, 6, 7, 'x', 'y', 'z' ]\n```\n\n#### clear()\n\nRemove all elements from this set.\n\n*Return:* This set.\n\n```node\n> s.clear().toArray();\n[]\n```\n\n#### each(callback)\n\nIterate over elements of this set, and invoke the `callback` function for each element. The callback function should be of the form `function(v) { ... }`. `v` is the element. The callback function may return `false` indicating the iteration should stop immediately. Any other returned value is ignored.\n\n*Return:* This vector.\n\n```node\n> s.each(function(v){if(v%2==0){console.log(v); return false;}});\n2\n```\n\n#### equals(object)\n\nTest whether this set equals the given object. (See vector's `equals` function for definition of equality.)\n\n*Return:* `true` or `false`.\n\n```node\n> s.equals(new Set([4,3,2,1]));\ntrue\n```\n\n#### filter(callback)\n\nReturn in an array all elements that make the `callback` function return `true`. The callback function should be of the form `function(v) { ... }`.\n\n*Return:* An array of elements for which the callback function returns `true`.\n\n```node\n> s.filter(function(v){return v%2==0});\n[ 2, 4 ]\n```\n\n#### find(callback)\n\nReturn the first element of this set that makes the `callback` function return `true`. The callback function should be of the form `function(v) { ... }`.\n\n*Return:* The first element of this set for which the callback function returns `true`.\n\n```node\n> s.find(function(v){return v%2==0});\n2\n```\n\n#### get(index, ...)\n\nGet elements of this set at the specified indexes.\n\n*Return:* If only 1 index is given as parameter, element of this set at that index; if multiple indexes are given, an array of elements at those indexes.\n\n```node\n> s.get(0)\n1\n> s.get(0,1,5,3)\n[ 1, 2, , 4 ]\n```\n\n#### has(value, ...)\n\nCheck whether the given values exist in the set.\n\n*Return:* If only 1 value is given as parameter, `true` or `false`; if multiple values are given, an array of boolean values, each identifying whether the corresponding value exists.\n\n```node\n> s.has(2,4,6)\n[ true, true, false ]\n```\n\n#### index(value, ...)\n\nReturn indexes of the specified elements in this set.\n\n*Return:* If only 1 value is given as parameter, index of that element, or `undefined` if not found; if multiple values are given, an array of indexes.\n\n```node\n> s.index(2)\n1\n> s.index(1,3,5,7)\n[ 0, 2 ]\n> s.index(1,5,7,3)\n[ 0, , , 2 ]\n```\n\n#### isEmpty()\n\nTest whether the set is empty.\n\n*Return:* `true` or `false`.\n\n```node\n> s.isEmpty();\nfalse\n```\n\n#### reduce(callback, memo)\n\nIterates over all elements of this set and invoke the `callback` function for each element. Each time a `memo` value is passed into the callback, and the return value of the callback becomes the memo value of the next iteration. The return value in the last iteration is the return value of the `reduce` function itself. The callback should be of the form `function(memo, v){ ... }`.\n\n*Return:* The return value of the callback function in the last iteration.\n\n```node\n> s.reduce(function(memo, v){return memo+v}, 0);\n10\n> s.reduce(function(memo, v){return memo*v}, 1);\n24\n```\n\n#### reduceRight(callback, memo)\n\nPerform the same function as `reduce`, but iterate elements of this set in reverse order.\n\n*Return:* The return value of the callback function in the last iteration.\n\n```node\n> s.reduceRight(function(memo, v){return v}, 5);\n1\n```\n\n#### remove(value, ...)\n\nRemove elements from this set.\n\n*Return:* This set.\n\n```node\n> s.remove(1,3,5).toArray();\n[ 2, 4 ]\n```\n\n#### removeAt(index, ...)\n\nRemove elements at the specified indexes from this set. For any index greater than size of this set, there is no effect.\n\n*Return:* This set.\n\n```node\n> s.removeAt(0,3,5).toArray();\n[ 2, 3 ]\n```\n\n#### removeLast()\n\nRemove last element of this set. If this set is empty, there is no effect.\n\n*Return:* This set.\n\n```node\n> s.removeLast().removeLast().toArray();\n[ 1, 2 ]\n> s.removeLast().removeLast().removeLast().removeLast().toArray();\n[]\n```\n\n#### removeRange(start, end)\n\nRemove elements between the `start` index (inclusive) and the `end` index (exclusive). If `start` is greater than or equal to size of this set, there is no effect. If `end` is greater than or equal to size of this set, all elements from `start` index are removed.\n\n*Return:* This set.\n\n```node\n> s.removeRange(9,20).toArray();\n[ 1, 2, 3, 4 ]\n> s.removeRange(0,0).toArray();\n[ 1, 2, 3, 4 ]\n> s.removeRange(0,1).toArray();\n[ 2, 3, 4 ]\n> s.removeRange(1,999).toArray();\n[ 2 ]\n```\n\n#### size()\n\nCheck the size of the set.\n\n*Return:* Number of elements in this set.\n\n```node\n> s.size();\n4\n```\n\n#### toArray()\n\nConvert this set into an array that contains the same elements. If the set contains an array or a collection as its element, the same instance of array or collection will also be an element of the resulting array.\n\n*Return:* An array.\n\n```node\n> s.toArray();\n[ 1, 2, 3, 4 ]\n> s.add([\"a\", \"b\"]).toArray();\n[ 1, 2, 3, 4, [ 'a', 'b' ] ]\n> s.toArray()[4].push(\"c\");\n3\n> s.toArray();\n[ 1, 2, 3, 4, [ 'a', 'b', 'c' ] ]\n```\n\n#### toString()\n\nConvert this set into a string.\n\n*Return:* A string.\n\n```node\n> s.toString();\n'[1,2,3,4]'\n```\n\n### Map\n\nA map is structured as a tree sorted by the keys. It provides the capability of looking up a value associated with a key.\n\nA map can be converted into a JavaScript object, using the `toObject()` function. However, there is an important difference between the two, though `toObject()`, and subsequently `toString()` that depends to `toObject()`, obscure this difference. Keys in a map are allowed to be of any primitive data type, array, collection, or combination of these, wheres keys in a JavaScript object can only be strings. Therefore, if a value is associated with integer `1` in a map, one cannot retrieve this value back by invoking `get(\"1\")`, where string `\"1\"` is used as the key for lookup.\n\nDue to map's sorted nature, some functions below allow to access the map by an index, which points to the location of the sorted tree. A `entry` object is a common way of returning both the key and the value at that location.\n\n*Entry:* An `entry` is a (`key`, `value`) pair in the map. An entry object provides a `key()` function to return the key, a `value()` function to return the value, a `toObject()` function to return an equivalent JavaScript object, and a `toString()` function to return the string that describes it.\n\nExamples below assume set `m` is initialized with entries `{1:\"a\", 2:\"b\", 3:\"c\", 4:\"d\"}`:\n\n```node\n> var m = new Map({1:\"a\",2:\"b\",\"a\":\"b\",\"c\":\"d\"}).set(3,\"c\").set(4,\"d\");\nundefined\n> m.keys();\n[ 3, 4, '1', '2', 'a', 'c' ]\n> m.toObject();\n{ '1': 'a', '2': 'b', '3': 'c', '4': 'd', a: 'b', c: 'd' }\n> m.get(1);\nundefined\n> m.get(\"1\");\n'a'\n> m.get(3);\n'c'\n```\n\n#### clear()\n\nRemove all elements from this set.\n\n*Return:* This set.\n\n```node\n> m.clear().toObject();\n{}\n```\n\n#### each(callback)\n\nIterate over entries of this map, and invoke the `callback` function for each entry. The callback function should be of the form `function(v) { ... }`. `v` is the entry. The callback function may return `false` indicating the iteration should stop immediately. Any other returned value is ignored.\n\n*Return:* This map.\n\n```node\n> m.each(function(v){if(v.key()%2==0){console.log(v.toObject()); return false;}});\n{ key: 4, value: 'd' }\n```\n\n#### equals(object)\n\nTest whether this map equals the given object. (See vector's `equals` function for definition of equality.)\n\n*Return:* `true` or `false`.\n\n```node\n> m.equals(new Map(m.toObject())); // false, because toObject() converts all keys to strings.\nfalse\n> m.equals(new Map(m));\ntrue\n```\n\n#### filter(callback)\n\nReturn in an array all entries that make the `callback` function return `true`. The callback function should be of the form `function(v) { ... }`.\n\n*Return:* An array of entries for which the callback function returns `true`.\n\n```node\n> m.filter(function(v){return v.key()%2==0;}).toString();\n'{\"key\":4,\"value\":\"d\"},{\"key\":\"2\",\"value\":\"b\"}'\n```\n\n#### find(callback)\n\nReturn the first entry of this map that makes the `callback` function return `true`. The callback function should be of the form `function(v) { ... }`.\n\n*Return:* The first entry of this map for which the callback function returns `true`.\n\n```node\n> m.find(function(v){return v.key()%2==0;}).toObject();\n{ key: 4, value: 'd' }\n```\n\n#### get(key, ...)\n\nGet value of this map associated with the specified keys.\n\n*Return:* If only 1 key is given as parameter, value in this map associated with that key; if multiple keys are given, an array of values associated with those indexes.\n\n```node\n> m.get(\"1\");\n'a'\n> m.get(1);\nundefined\n> m.get(1,4,\"a\",3);\n[ , 'd', 'b', 'c' ]\n```\n\n#### getAt(index, ...)\n\nGet entry of this map at the specified indexes.\n\n*Return:* If only 1 index is given as parameter, entry of this map at that index; if multiple indexes are given, an array of entries at those indexes.\n\n```node\n> m.getAt(0).toObject();\n{ key: 3, value: 'c' }\n> m.getAt(1,4,8,3).toString();\n'{\"key\":4,\"value\":\"d\"},{\"key\":\"a\",\"value\":\"b\"},,{\"key\":\"2\",\"value\":\"b\"}'\n```\n\n#### has(key, ...)\n\nCheck whether the given keys exist in the map.\n\n*Return:* If only 1 key is given as parameter, `true` or `false`; if multiple keys are given, an array of boolean values, each identifying whether the corresponding key exists.\n\n```node\n> m.has(2,4,6)\n[ false, true, false ]\n> m.has(4)\ntrue\n```\n\n#### isEmpty()\n\nTest whether the map is empty.\n\n*Return:* `true` or `false`.\n\n```node\n> m.isEmpty();\nfalse\n```\n\n#### reduce(callback, memo)\n\nIterates over all entries of this map and invoke the `callback` function for each entry. Each time a `memo` value is passed into the callback, and the return value of the callback becomes the memo value of the next iteration. The return value in the last iteration is the return value of the `reduce` function itself. The callback should be of the form `function(memo, v){ ... }`.\n\n*Return:* The return value of the callback function in the last iteration.\n\n```node\n> m.reduce(function(memo, v){return memo+v.value()}, \"\");\n'cdabbd'\n```\n\n#### reduceRight(callback, memo)\n\nPerform the same function as `reduce`, but iterate entries of this map in reverse order.\n\n*Return:* The return value of the callback function in the last iteration.\n\n```node\n> m.reduceRight(function(memo, v){return memo+v.value()}, \"\");\n'dbbadc'\n```\n\n#### remove(key, ...)\n\nRemove entries identified by the given keys from this map.\n\n*Return:* This map.\n\n```node\n> m.remove(3,4).toObject();\n{ '1': 'a', '2': 'b', a: 'b', c: 'd' }\n```\n\n#### removeAt(index, ...)\n\nRemove entries at the specified indexes from this map. For any index greater than size of this map, there is no effect.\n\n*Return:* This set.\n\n```node\n> m.removeAt(0,3,5).toObject();\n{ '1': 'a', '4': 'd', a: 'b' }\n```\n\n#### removeLast()\n\nRemove last entry of this map. If this map is empty, there is no effect.\n\n*Return:* This map.\n\n```node\n> m.removeLast().removeLast().toObject();\n{ '1': 'a', '2': 'b', '3': 'c', '4': 'd' }\n> m.removeLast().removeLast().removeLast().removeLast().toObject();\n{}\n```\n\n#### removeRange(start, end)\n\nRemove entries between the `start` index (inclusive) and the `end` index (exclusive). If `start` is greater than or equal to size of this map, there is no effect. If `end` is greater than or equal to size of this map, all entries from `start` index are removed.\n\n*Return:* This map.\n\n```node\n> m.removeRange(9,20).toObject();\n{ '1': 'a', '2': 'b', '3': 'c', '4': 'd', a: 'b', c: 'd' }\n> m.removeRange(0,0).toObject();\n{ '1': 'a', '2': 'b', '3': 'c', '4': 'd', a: 'b', c: 'd' }\n> m.removeRange(0,1).toObject();\n{ '1': 'a', '2': 'b', '4': 'd', a: 'b', c: 'd' }\n> m.removeRange(1,999).toObject();\n{ '4': 'd' }\n```\n\n#### set(key, value)\n\nSet a new value associated with a key. If the key already exists in the map, existing value is substituted by the given new value. If the key does not exist, the given (key, value) entry is added to the map.\n\n*Return:* This map.\n\n```node\n> m.set(3,\"x\").set(4,\"y\").toObject();\n{ '1': 'a', '2': 'b', '3': 'x', '4': 'y', a: 'b', c: 'd' }\n```\n\n#### setAll(object)\n\nSet all the values in `object` in the map. The object can be another map or a JavaScript object.\n\n*Return:* This map.\n\n```node\n> m.setAll({3:\"x\",4:\"y\"}).toObject();\n{ '1': 'a', '2': 'b', '3': 'x', '4': 'y', a: 'b', c: 'd' }\n> m.setAll(new Map({null:undefined})).toObject();\n{ '1': 'a',\n  '2': 'b',\n  '3': 'c',\n  '4': 'd',\n  a: 'b',\n  c: 'd',\n  null: undefined }\n```\n\n#### size()\n\nCheck the size of the map.\n\n*Return:* Number of entries in this map.\n\n```node\n> m.size();\n6\n```\n\n#### toArray()\n\nConvert this map into an array that contains the same entries. If the map contains an entry with an array or a collection as its key or value, the same instance of array or collection will also be the key or value of the corresponding entry in the resulting array.\n\n*Return:* An array.\n\n```node\n> m.toArray().toString();\n'{\"key\":3,\"value\":\"c\"},{\"key\":4,\"value\":\"d\"},{\"key\":\"1\",\"value\":\"a\"},{\"key\":\"2\",\"value\":\"b\"},{\"key\":\"a\",\"value\":\"b\"},{\"key\":\"c\",\"value\":\"d\"}'\n```\n\n#### toObject()\n\nConvert this map into a JavaScript object. All keys in the map are converted into strings using their `toString()` functions before storing into the object. As a result, values of some originally distinct keys, such as `1` and `\"1\"`, are stored into the same field of the object. In such a case, value of the greater key according to keys' sorting order overwrites value of the smaller key. Because of this conversion, `new Map(m.toObject())` does not always produce an equivalent map as `m`.\n\n```node\n> m.toObject();\n{ '1': 'a', '2': 'b', '3': 'c', '4': 'd', a: 'b', c: 'd' }\n> new Map().set(m, 1).toObject();\n{ '{\"1\":\"a\",\"2\":\"b\",\"3\":\"c\",\"4\":\"d\",\"a\":\"b\",\"c\":\"d\"}': 1 }\n```\n\n#### toString()\n\nConvert this map into a string. This is equivalent to calling `toObject()` and then calling `toString()` on the returned object. Because of this, the returned string may not contain all the keys and values of the original map. (See `toObject()` for explanation.)\n\n*Return:* A string.\n\n```node\n> m.toString();\n'{\"1\":\"a\",\"2\":\"b\",\"3\":\"c\",\"4\":\"d\",\"a\":\"b\",\"c\":\"d\"}'\n```\n","maintainers":[{"name":"tfeng","email":"tfeng@berkeley.edu"}],"time":{"modified":"2022-06-13T06:30:22.480Z","created":"2013-06-23T22:47:55.328Z","0.1.0":"2013-06-23T22:47:57.483Z","0.1.1":"2013-06-23T22:49:02.517Z","0.1.2":"2013-06-26T07:36:48.268Z","0.1.3":"2013-06-26T07:58:59.723Z","0.1.4":"2013-06-27T07:34:03.966Z","0.1.5":"2014-06-07T06:35:37.233Z","0.1.6":"2014-10-06T17:19:56.298Z"},"author":{"name":"Thomas Feng","email":"tfeng@berkeley.edu"},"repository":{"type":"git","url":"https://github.com/tfeng/collection.git","web":"https://github.com/tfeng/collection"},"homepage":"http://tfeng.github.io/collection","keywords":["collection","native","util","vector","set","map"],"bugs":{"url":"https://github.com/tfeng/collection/issues"},"readmeFilename":"README.md","users":{"pgilad":true,"alexpearly":true,"rocket0191":true,"systelab-technologies":true}}