la. StrVector
Source: ladoc.
The string vector representation. Wraps a C++ array.
Property
new StrVector([arg])
Vector - array of strings.
Example
var la = require('qminer').la;
// create a new empty vector
var vec = new la.StrVector();
// create a new vector
var vec2 = new la.StrVector(['a', 'b', 'c']);
Parameter
Name | Type | Optional | Description |
---|---|---|---|
arg |
(Array of string or module:la.StrVector) |
Yes |
Constructor arguments. There are two ways of constructing:
|
Property
length
Gives the length of vector. Type number
.
Example
var la = require('qminer').la;
// create a new vector
var x = new la.StrVector(['a', 'b', 'c']);
// get the length of the vector
var len = x.length; // returns 3
Methods
at(index) → string
Returns element at index.
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.StrVector(['a', 'b', 'c']);
// get the element at index 1
var el = vec[1];
Parameter
Name | Type | Optional | Description |
---|---|---|---|
index |
number |
|
Element index (zero-based). |
- Returns
-
string
B Vector element.
load(fin) → module:la.StrVector
Loads the vector from input stream (binary deserialization).
Example
// import fs module
var fs = require('qminer').fs;
var la = require('qminer').la;
// create an empty vector
var vec = new la.StrVector();
// open a read stream
var fin = fs.openRead('vec.dat');
// load the vector
vec.load(fin);
Parameter
Name | Type | Optional | Description |
---|---|---|---|
fin |
|
Input stream. |
- Returns
-
module:la.StrVector
B Self. The vector is filled using the input streamfin
.
loadascii(fin) → module:la.StrVector
Loads the vector from input stream (ascii deserialization).
Example
// import fs module
var fs = require('qminer').fs;
var la = require('qminer').la;
// create an empty vector
var vec = new la.StrVector();
// open a read stream
var fin = fs.openRead('vec.dat');
// load the matrix
vec.loadascii(fin);
Parameter
Name | Type | Optional | Description |
---|---|---|---|
fin |
|
Input stream. |
- Returns
-
module:la.StrVector
B Self. The vector is filled using the input streamfin
.
push(val) → number
Adds an element to the end of the vector.
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.StrVector(['a', 'b', 'c']);
// push an element to the vector
vec.push('xyz');
Parameter
Name | Type | Optional | Description |
---|---|---|---|
val |
string |
|
The element added to the vector. |
- Returns
-
number
B The new length property of the object upon which the method was called.
pushV(vec) → number
Appends a second vector to the first one.
Example
// import la module
var la = require('qminer').la;
// create two new vectors
var vec = new la.StrVector(['a', 'b', 'c']);
var vec2 = new la.StrVector(['d', 'e']);
// append the two vectors
vec.pushV(vec2);
Parameter
Name | Type | Optional | Description |
---|---|---|---|
vec |
|
The appended vector. |
- Returns
-
number
B The new length property of the vectors.
put(idx, val) → module:la.StrVector
Sets an element in vector.
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.StrVector(['a', 'b', 'c']);
// set the first element to 'xyz'
vec.put(0, 'xyz');
Parameters
Name | Type | Optional | Description |
---|---|---|---|
idx |
number |
|
Index (zero based). |
val |
string |
|
Element value. |
- Returns
-
module:la.StrVector
B Self. The values at indexidx
has been changed toval
.
save(fout) → module:fs.FOut
Saves the vector as output stream (binary serialization).
Example
// import fs module
var fs = require('qminer').fs;
var la = require('qminer').la;
// create a new vector
var vec = new la.StrVector(['a', 'b', 'c']);
// open write stream
var fout = fs.openWrite('vec.dat');
// save vector and close write stream
vec.save(fout).close();
Parameter
Name | Type | Optional | Description |
---|---|---|---|
fout |
|
Output stream. |
- Returns
-
module:fs.FOut
B The output streamfout
.
saveascii(fout) → module:fs.FOut
Saves the vector as output stream (ascii serialization).
Example
// import fs module
var fs = require('qminer').fs;
var la = require('qminer').la;
// create a new vector
var vec = new la.StrVector(['a', 'b', 'c']);
// open write stream
var fout = fs.openWrite('vec.dat');
// save matrix and close write stream
vec.saveascii(fout).close();
Parameter
Name | Type | Optional | Description |
---|---|---|---|
fout |
|
Output stream. |
- Returns
-
module:fs.FOut
B The output streamfout
.
shuffle() → module:la.StrVector
Randomly reorders the elements of the vector (inplace).
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.StrVector(['asd', 'z', 'kkkk']);
// shuffle the elements
vec.shuffle();
- Returns
-
module:la.StrVector
B Self. The elements are randomly reordered.
splice(start, deleteCount[, ...itemN]) → module:la.StrVector
Changes the vector by removing and adding elements.
Example
var la = require('qminer').la;
// create a new vector
var vec = new la.StrVector(['a', 'b', 'c']);
// splice the vector by removing the last two elements and adding 'd', 'e'
vec.splice(1, 2, 'd', 'e')// returns vector ['a', 'd', 'e']
Parameters
Name | Type | Optional | Description |
---|---|---|---|
start |
number |
|
Index at which to start changing the array. |
deleteCount |
number |
|
Number of elements to be removed. |
itemN |
number |
Yes |
The element(s) to be add to the array. If no elements are given, splice() will only remove elements from the array. Value can be repeated. |
- Returns
-
module:la.StrVector
B Self. The selected elements are removed/replaced.
subVec(arg) → module:la.StrVector
Returns a subvector.
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.StrVector(['a', 'b', 'c']);
// get the subvector of the first two elements
var subvec = vec.subVec([0, 1]);
Parameter
Name | Type | Optional | Description |
---|---|---|---|
arg |
(Array of number or module:la.IntVector) |
|
Index array or vector. Indices can repeat (zero based). |
- Returns
-
module:la.StrVector
B Subvector, where the i-th element is thearg[i]
-th element of the instance.
toArray() → Array of string
Copies the vector into a JavaScript array of strings.
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.StrVector(["one", "two", "three"]);
// create a JavaScript array out of vec
var arr = vec.toArray(); // returns an array ["one", "two", "three"]
- Returns
-
Array of string
B A JavaScript array of strings.
toString() → string
Returns the vector as string.
Example
var la = require('qminer').la;
// create a new vector
var vec = new la.StrVector(['a', 'b', 'c']);
// create vector as string
vec.toString(); // returns 'a, b, c'
- Returns
-
string
B String representation.
trunc(idx) → module:la.StrVector
Deletes elements with sprecific index or more.
Example
var la = require('qminer').la;
// create a new vector
var vec = new la.StrVector(['a', 'b', 'c']);
// trunc all elements with index 1 or more
vec.trunc(1); // returns vector ['a']
Parameter
Name | Type | Optional | Description |
---|---|---|---|
idx |
string |
|
Index (zero based). |
- Returns
-
module:la.StrVector
B Self after truncating.
unshift(...args) → number
Adds elements to the beginning of the vector.
Example
// import la module
var la = require('qminer').la;
// create a new vector
var vec = new la.StrVector(['a', 'b', 'c']);
// add two elements to the beggining of the vector
var len = vec.unshift('d', 'e'); // returns 5
Parameter
Name | Type | Optional | Description |
---|---|---|---|
args |
string |
|
One or more elements to be added to the vector. Value can be repeated. |
- Returns
-
number
B The new length of vector.