ht. IntIntMap
Source: htdoc.
Used for storing key/data pairs, wraps an efficient C++ implementation.
Property
new IntIntMap()
Integer-Integer hashmap.
Example
// create a new hashtable
ht = require('qminer').ht;
var h = new ht.IntIntMap();
// Adding two key/dat pairs
h.put(5, 10);
h.put(15, 20);
// Getting data
h.hasKey(5); // returns true
h.get(15); // returns 20
h.key(1); // returns 15
h.dat(1); // returns 20
h.length; // returns 2
// Saving and loading:
var fs = require('qminer').fs;
fout = fs.openWrite('map.dat'); // open write stream
h.save(fout).close(); // save and close write stream
var h2 = new ht.IntIntMap(); // new empty table
var fin = fs.openRead('map.dat'); // open read stream
h2.load(fin); // load
Property
length
Number of key/dat pairs. Type number
.
Example
// create a new hashtable
ht = require('qminer').ht;
var h = new ht.IntIntMap();
// Adding two key/dat pairs
h.put(5, 10);
// get the number of key/dat pairs
var length = h.length; // returns 1
Methods
dat(n) → number
Returns n-th dat.
Example
// import modules
var qm = require('qminer');
var ht = qm.ht;
// create a new hashtable
var h = new ht.IntIntMap();
// add a key/dat pair
h.put(5, 10);
// get the first dat
var key = h.key(0); // returns 10
Parameter
Name | Type | Optional | Description |
---|---|---|---|
n |
number |
|
Hashmap dat index number. Should be between 0 and length-1. |
- Returns
-
number
B The n-th data value.
get(key) → number
Returns dat given key.
Example
// import modules
var qm = require('qminer');
var ht = qm.ht;
// create a new hashtable
var h = new ht.IntIntMap();
// add a key/dat pair
h.put(5, 10);
// get the newly added data
var val = h.get(5); // returns 10
Parameter
Name | Type | Optional | Description |
---|---|---|---|
key |
number |
|
Hashmap key. |
- Returns
-
number
B Hashmap data.
hasKey(key) → boolean
Returns true if the map has a given key.
Example
// import modules
var qm = require('qminer');
var ht = qm.ht;
// create a new hashtable
var h = new ht.IntIntMap();
// add a key/dat pair
h.put(5, 10);
// check if the hashtable has the key
h.hasKey(5); // returns true
Parameter
Name | Type | Optional | Description |
---|---|---|---|
key |
number |
|
Hashmap key. |
- Returns
-
boolean
B True if the map contains key. Otherwise, false.
key(n) → number
Returns n-th key.
Example
// import modules
var qm = require('qminer');
var ht = qm.ht;
// create a new hashtable
var h = new ht.IntIntMap();
// add a key/dat pair
h.put(5, 10);
// get the first key
var key = h.key(0); // returns 5
Parameter
Name | Type | Optional | Description |
---|---|---|---|
n |
number |
|
Hashmap key index number. Should be between 0 and length-1. |
- Returns
-
number
B The n-th key.
keyId(key) → number
Returns the ID of the key provided as parameter.
Example
// import modules
var qm = require('qminer');
var ht = qm.ht;
// create a new hashtable
var h = new ht.IntIntMap();
// add a key/dat pair
h.put(5, 10);
// get key id of 5
var key = h.keyId(5); // returns 0
Parameter
Name | Type | Optional | Description |
---|---|---|---|
key |
number |
|
Hashmap key. |
- Returns
-
number
B n - Hashmap index number of the key.
load(fin) → module:ht.IntIntMap
Loads the hashtable from input stream.
Example
// import modules
var qm = require('qminer');
var ht = qm.ht;
var fs = qm.fs;
// create a new hashtable
var h = new ht.IntIntMap();
fout = fs.openWrite('map.dat'); // open write stream
h.save(fout).close(); // save and close write stream
var fin = fs.openRead('map.dat'); // open read stream
var h2 = new ht.IntIntMap();
h2.load(fin); // load
Parameter
Name | Type | Optional | Description |
---|---|---|---|
fin |
|
Input stream. |
- Returns
-
module:ht.IntIntMap
B Self.
put(key, data) → module:ht.IntIntMap
Add/update key-value pair.
Example
// import modules
var qm = require('qminer');
var ht = qm.ht;
// create a new hashtable
var h = new ht.IntIntMap();
// add a key/dat pair
h.put(5, 10);
Parameters
Name | Type | Optional | Description |
---|---|---|---|
key |
number |
|
Hashmap key. |
data |
number |
|
Hashmap data. |
- Returns
-
module:ht.IntIntMap
B Self.
save(fout) → module:fs.FOut
Saves the hashtable to output stream.
Example
// import modules
var qm = require('qminer');
var ht = qm.ht;
var fs = qm.fs;
// create a new hashtable
var h = new ht.IntIntMap();
fout = fs.openWrite('map.dat'); // open write stream
h.save(fout).close(); // save and close write stream
Parameter
Name | Type | Optional | Description |
---|---|---|---|
fout |
|
Output stream. |
- Returns
-
module:fs.FOut
B fout.
sortDat([asc]) → module:ht.IntIntMap
Sorts by dat.
Example
// import modules
var qm = require('qminer');
var ht = qm.ht;
// create a new hashtable
var h = new ht.IntIntMap();
h.put(5, 10);
h.put(15, 20);
// sort the hashtable by dat
h.sortDat();
Parameter
Name | Type | Optional | Description |
---|---|---|---|
asc |
boolean |
Yes |
If true, sorts in ascending order. Defaults to |
- Returns
-
module:ht.IntIntMap
B Self.
sortKey([asc]) → module:ht.IntIntMap
Sorts by keys.
Example
// import modules
var qm = require('qminer');
var ht = qm.ht;
// create a new hashtable
var h = new ht.IntIntMap();
h.put(5, 10);
h.put(15, 20);
// sort the hashtable by keys
h.sortKey();
Parameter
Name | Type | Optional | Description |
---|---|---|---|
asc |
boolean |
Yes |
If true, sorts in ascending order. Defaults to |
- Returns
-
module:ht.IntIntMap
B Self.