<commit_before>/*
 * Copyright (C) 2016 Fondazione Istituto Italiano di Tecnologia
 * Authors: <NAME>, <NAME>
 * CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
 *
 */

#include "SparseMatrix.h"

#include "Triplets.h"

#include <cassert>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <iterator>

#ifndef UNUSED
#define UNUSED(x) (void)(sizeof((x), 0))
#endif

namespace iDynTree {

    SparseMatrix::SparseMatrix() : SparseMatrix(0, 0) {}

    SparseMatrix::SparseMatrix(unsigned rows, unsigned cols)
    : SparseMatrix(rows, cols, iDynTree::VectorDynSize())
    { }

    SparseMatrix::SparseMatrix(unsigned rows, unsigned cols, const iDynTree::VectorDynSize& memoryReserveDescription)
    : m_outerStarts(rows + 1, 0)
    , m_allocatedSize(0)
    , m_rows(rows)
    , m_columns(cols)
    , m_ordering(RowMajor)
    {
        initializeMatrix(rows, cols, memoryReserveDescription.data(), memoryReserveDescription.size());
    }

    void SparseMatrix::initializeMatrix(unsigned rows, unsigned cols, const double *vector, unsigned vectorSize)
    {
        UNUSED(rows);
        UNUSED(cols);

        for (unsigned i = 0; i < vectorSize; ++i) {
            m_allocatedSize += vector[i];
        }

        m_values.reserve(m_allocatedSize);

        // Inner indeces has same size of values
        m_innerIndeces.reserve(m_allocatedSize);
    }

    SparseMatrix::~SparseMatrix() {}

    bool SparseMatrix::valueIndex(unsigned row, unsigned col, unsigned &rowNZIndex) const
    {
        assert(row >= 0 && row < rows()
               && col >= 0 && col < columns());
        //TODO: more fast search?

        //find the row and how many zeros are in that row
        rowNZIndex = m_outerStarts[row];
        unsigned rowNNZ = m_outerStarts[row + 1] - rowNZIndex;

        //find inner position
        for (unsigned i = rowNZIndex; i < rowNZIndex + rowNNZ; ++i) {
            if (m_innerIndeces[i] < 0) continue;
            if (col == static_cast<unsigned>(m_innerIndeces[i])) {
                rowNZIndex = i;
                return true;
            }
            if (static_cast<unsigned>(m_innerIndeces[i]) > col) {
                rowNZIndex = i;
                return false;
            }
        }
        rowNZIndex += rowNNZ;
        return false;
    }

    unsigned SparseMatrix::insert(unsigned int row, unsigned int col, double value)
    {
        assert(row >= 0 && row < rows()
               && col >= 0 && col < columns());

        //first: check if there is space in the arrays
        if (m_allocatedSize <= m_values.size()) {
            reserve(m_values.size() + 10);
        }

        //find insertion position
        unsigned insertionIndex = 0;
        if (valueIndex(row, col, insertionIndex)) {
            //???: what if the element exists alredy in the matrix?
            return insertionIndex;
        }

        //I found the index. Now I have to shift to the right the values and inner elements<commit_msg>Changed how elements are accessed in the matrix<commit_after>/*
 * Copyright (C) 2016 Fondazione Istituto Italiano di Tecnologia
 * Authors: <NAME>, <NAME>
 * CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
 *
 */

#include "SparseMatrix.h"

#include "Triplets.h"

#include <cassert>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <iterator>

#ifndef UNUSED
#define UNUSED(x) (void)(sizeof((x), 0))
#endif

namespace iDynTree {

    SparseMatrix::SparseMatrix() : SparseMatrix(0, 0) {}

    SparseMatrix::SparseMatrix(unsigned rows, unsigned cols)
    : SparseMatrix(rows, cols, iDynTree::VectorDynSize())
    { }

    SparseMatrix::SparseMatrix(unsigned rows, unsigned cols, const iDynTree::VectorDynSize& memoryReserveDescription)
    : m_outerStarts(rows + 1, 0)
    , m_allocatedSize(0)
    , m_rows(rows)
    , m_columns(cols)
    , m_ordering(RowMajor)
    {
        initializeMatrix(rows, cols, memoryReserveDescription.data(), memoryReserveDescription.size());
    }

    void SparseMatrix::initializeMatrix(unsigned rows, unsigned cols, const double *vector, unsigned vectorSize)
    {
        UNUSED(rows);
        UNUSED(cols);

        for (unsigned i = 0; i < vectorSize; ++i) {
            m_allocatedSize += vector[i];
        }

        m_values.reserve(m_allocatedSize);

        // Inner indeces has same size of values
        m_innerIndeces.reserve(m_allocatedSize);
    }

    SparseMatrix::~SparseMatrix() {}

    bool SparseMatrix::valueIndex(unsigned row, unsigned col, unsigned &rowNZIndex) const
    {
        assert(row >= 0 && row < rows()
               && col >= 0 && col < columns());
        //We can use std::lower_bound to between rowNZIndex and rowNZIndex + rowNNZ
        //They are already sorted. The only critical point is if we have -1 in the matrix
        //Which right now it does not apply
        int colBegin = m_outerStarts[row];
        int colEnd = m_outerStarts[row + 1];
        std::vector<int>::const_iterator innerVectorBegin = m_innerIndeces.begin();

        //initialize the return value to be the first element of the row
        rowNZIndex = colBegin;
        if (colEnd - colBegin == 0) {
            //empty row, avoid searching
            return false;
        }

        std::vector<int>::const_iterator foundIndex = std::lower_bound(innerVectorBegin + colBegin,
                                                                       innerVectorBegin + colEnd, col);

        //Compute the index of the first element next or equal to the one we
        //were looking for

        rowNZIndex = std::distance(innerVectorBegin, foundIndex);
        if (foundIndex == innerVectorBegin + colEnd) {
            //not found
            //return the index of the last element of the row
            //as we have to put the element as last element
            return false;
        }

        if (*foundIndex >= 0
            && static_cast<unsigned>(*foundIndex) == col) {
            //found
            return true;
        }
        
        return false;
    }

    unsigned SparseMatrix::insert(unsigned int row, unsigned int col, double value)
    {
        assert(row >= 0 && row < rows()
               && col >= 0 && col < columns());

        //first: check if there is space in the arrays
        if (m_allocatedSize <= m_values.size()) {
            reserve(m_values.size() + 10);
        }

        //find insertion position
        unsigned insertionIndex = 0;
        if (valueIndex(row, col, insertionIndex)) {
            //???: what if the element exists alredy in the matrix?
            return insertionIndex;
        }

        //I found the index. Now I have to shift to the right the values and inner elements
{'fraction_non_alphanumeric': 0.10712711241734019, 'fraction_numerical': 0.006759735488611315, 'mean_word_length': 2.3843858776727997, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6486122', 'n_tokens_mistral': 2071, 'n_tokens_neox': 1976, 'n_words': 583}