Data Objects Creation

This section helps you understand how to create objects in Swordfish with a few simple examples. For more information, refer to C++ Data Types and Forms.

Creating Scalars

The following examples show how to create scalars of different types.
# include "Swordfish.h"

int main() {
    DolphinDBLib::initializeRuntime();
    // create an INT scalar
    Int *b = new Int(12);
    std::cout << b->getString() << std::endl;

    // create a STRING scalar
    String *str = new String("hello");

    std::cout << str->getString() << std::endl;

    // create a DOUBLE scalar
    Double *d = new Double(10);

    std::cout << d->getString() << std::endl;

    // create a DECIMAL32 scalar
    Decimal<int> decimal1(2, 12345);  // with 12345 as input value and 2 as decimal places
        // create a DECIMAL64 scalar
    Decimal<long long> decimal2(5, 88);  // with 88 as input value and 5 as decimal places

    // create a DATE scalar
    Date *date1 = new Date(2024, 8, 14);

    std::cout << date1->getString() << std::endl;
    DolphinDBLib::finalizeRuntime();
    return 0;
}

Creating Vectors

The following examples illustrate vector creation for different types and methods to append values to them.
# include "Swordfish.h"

int main() {
    DolphinDBLib::initializeRuntime();
    // create a DOUBLE vector v1
    VectorSP v1 = Util::createVector(DT_DOUBLE, 0, 100);
 
    // append values to v1
    std::vector<double> newData = {1.1, 2.2, 3.3};
    v1->appendDouble(newData.data(), newData.size());

    std::cout << v1->getDouble(1) << std::endl;

    // create a STRING vector v2
    VectorSP v2 = Util::createVector(DT_STRING, 0);

    // append values to v2
    std::vector<string> newData2 = {"A", "B", "C"};
    v2->appendString(newData2.data(), newData2.size());

    std::cout << v2->getString() << std::endl;

    DolphinDBLib::finalizeRuntime();
    return 0;
}

Creating Tuples

The following examples illustrate how to create a tuple and append values to it.
# include "Swordfish.h"

int main() {
    DolphinDBLib::initializeRuntime();

    // create a tuple tp
    VectorSP tp = Util::createVector(DT_ANY, 0);
    ConstantSP d1{new Double(1.1)};
    ConstantSP d2{new String("A")};
    // append 2 elements
    tp->append(d1);
    tp->append(d2);
    // check whether tp is a tuple
    std::cout << tp->isTuple() << std::endl;

    DolphinDBLib::finalizeRuntime();
    return 0;
}

Creating Pairs

The following examples illustrate how to create a pair.
# include "Swordfish.h"

int main() {
    DolphinDBLib::initializeRuntime();

    // create a DOUBLE pair p
    VectorSP p = Util::createPair(DT_DOUBLE);
    p->setInt(0, 1.1);
    p->setDouble(1, 3.6);
    // check whether p is a pair
    std::cout << p->isPair() << std::endl;

    DolphinDBLib::finalizeRuntime();
    return 0;
}

Creating Matrices

The following examples illustrate how to create a matrix.
# include "Swordfish.h"

int main() {
    DolphinDBLib::initializeRuntime();

    int *rawData = new int[12]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    VectorSP m1 = Util::createMatrix(DT_INT, 4, 3, 12, 0, rawData);

    DolphinDBLib::finalizeRuntime();
    return 0;
}

/* output:
#0 #1 #2 #3
-- -- -- --
1  4  7  10
2  5  8  11
3  6  9  12
*/

Creating Dictionaries

The following examples illustrate how to create a dictionary and append key-value pairs to it.
# include "Swordfish.h"

int main() {
    DolphinDBLib::initializeRuntime();

    // create a dictionary d1
    DictionarySP d1 = Util::createDictionary(DT_INT, nullptr, DT_DOUBLE, nullptr);

    // append a key-value pair to d1
    ConstantSP k = Util::createConstant(DT_INT);
    k->setInt(1);
    ConstantSP v = Util::createConstant(DT_DOUBLE);
    v->setDouble(1.1);
    d1->set(k, v);
    k->setInt(2);
    v->setDouble(2.2);
    d1->set(k, v);
    std::cout << d1->getString() << std::endl;

    DolphinDBLib::finalizeRuntime();
    return 0;
}

Creating Sets

The following examples illustrate how to create a set and append values to it.
# include "Swordfish.h"

int main() {
    DolphinDBLib::initializeRuntime();

    // create a set s1
    SetSP s1 = Util::createSet(DT_INT, nullptr, 3);

    // append elements to s1
    ConstantSP val = Util::createConstant(DT_INT);
    val->setInt(1);
    s1->append(val);
    val->setInt(2);
    s1->append(val);
    std::cout << s1->getString() << std::endl;

    DolphinDBLib::finalizeRuntime();
    return 0;
}

Creating Tables

The following examples illustrate how to create a table and append records to it.
# include "Swordfish.h"

int main() {
    DolphinDBLib::initializeRuntime();

    // create a table tb1
    std::string colName1 = "id";
    std::string colName2 = "val";
    std::vector<std::string> colNames = {colName1, colName2};
    std::vector<DATA_TYPE> colTypes = {DT_INT, DT_DOUBLE};
    TableSP tb1 = Util::createTable(colNames, colTypes, 0, 0);

    // append new records to tb1
    std::string errMsg;
    ConstantSP id = Util::createConstant(DT_INT);
    ConstantSP val = Util::createConstant(DT_DOUBLE);
    for (auto i = 0; i < 10; ++i)
    {
    id->setInt(i);
    val->setDouble(i * 1.1);
    std::vector<ConstantSP> rowValues = {id, val};
    INDEX rowIdx = i;
    tb1->append(rowValues, rowIdx, errMsg);
    }
    if (errMsg.empty())
    {
    std::cout << tb1->getString() << std::endl;
    }
    else
    {
    std::cout << "insert rows failed! " << errMsg << std::endl;
    }

    DolphinDBLib::finalizeRuntime();
    return 0;
}