Is there a way to dynamically change a comparison operator? - c++

I'm creating an AI Director and need a way to change when the AI needs to pressure the player and when they need to move away. I've got a TArray of Objects and am checking the distance from the player. I'd like to either get the largest distance or the smallest distance.
I know this doesn't work:
operator comparer = PlayerTensionLevel > BackstageThreshold ? > : <;
Both of the variables used in the one line Bool are floats. I'm hoping that the comparer can be used in a situation like:
if(DistanceSquared(objectA, objectB) comparer _currentThresholdDistance){
_currentObject = objectA;
_currentThresholdDistance = DistanceSquared(objectA, objectB);
}

You can compute with bool! If you aren’t concerned with differing behavior for ties, you can just write
if((DistanceSquared(objectA, objectB) > _currentThresholdDistance) ==
(PlayerTensionLevel > BackstageThreshold)) …
(Technically, the extra parentheses here are unnecessary, but it’s probably not reasonable to expect the reader to know that relational operators have higher precedence than equality operators.)

As already mentioned in #SamVarshavchik's comment above,
You can use std::function and assign it to either std::less or std::greater based on PlayerTensionLevel and BackstageThreshold.
After you determine the comparator you can use it against the current values of _currentThresholdDistance and the the squared distance between the objects (here I just used dist2 to represent it).
#include <iostream>
#include <functional>
std::function<bool(float a, float b)>
GetComp(float PlayerTensionLevel, float BackstageThreshold)
{
if (PlayerTensionLevel > BackstageThreshold) {
return std::less<float>{};
}
return std::greater<float>{};
}
int main() {
float _currentThresholdDistance = 1;
float dist2 = 2;
float PlayerTensionLevel = 100;
float BackstageThreshold;
BackstageThreshold = 101;
std::cout << GetComp(PlayerTensionLevel, BackstageThreshold)
(dist2, _currentThresholdDistance) << std::endl;
BackstageThreshold = 99;
std::cout << GetComp(PlayerTensionLevel, BackstageThreshold)
(dist2, _currentThresholdDistance) << std::endl;
}
Output:
1
0

Related

Able to iterate through c++ map from stl, but unable to asign values or print out class variables

I am currently trying to use the map provided from the c++ stl. My key value pair are both a pointer to a custom object. I am able to store data into the map, and also successfully iterate through all the keys and values of the map. However, whenever I attempt to access one of the values member variables in the map through its key I get the following runtime error:
Segmentation fault (core dumped)
I am using a custom comparator for the map. I believe this is where the issue is arising. I believe this becasue whenever I run the code without the custom comparator the error goes away.
Note that the use of the Eigen library is just being used for linear algebra purposes. Please try to look past it in the example code, as it is just being used in my particular implementation to the problem I am trying to solve.
Please use the following code for context:
#include <iostream>
#include <map>
#include <Eigen/Dense>
#include <vector>
#include "A_star.h"
using namespace std;
struct NodeCostPointer {
bool operator()(star::Node* const& a,star::Node* const& b){
return a->f_cost >= b->f_cost;
int main() {
Eigen::Matrix<float,3,3> obj;
obj(0,0) = 3360;
obj(1,0) = 3360;
obj(2,0) = 3360;
obj(0,1) = 7000;
obj(1,1) = 7000;
obj(2,1) = 7000;
obj(0,2) = 5280;
obj(1,2) = 5280;
obj(2,2) = 5280;
Eigen::Matrix<float,3,1> end;
end(0) = 12300;
end(1) = 12300;
end(2) = 12300;
Eigen::Matrix<float,3,1> start;
start(0) = -.8;
start(1) = -.8;
start(2) = -.8;
map<star::Node*,star::Node*, NodeCostPointer> test;
star::Node* nodetest = new star::Node(start,6,0);
star::Node* nodetest2 = new star::Node(end,6,0);
star::Node* nodetest3 = new star::Node(start,5,std::numeric_limits<float>::infinity());
test[nodetest] = nodetest3;
test[nodetest2] = nodetest3;
// error occurs here:
cout << test[nodetest]->f_cost << endl;
// or
test[nodetest]->f_cost = 4;
map<star::Node*,star::Node*,NodeCostPointer>::iterator itt;
for(itt=test.begin();itt!=test.end();itt++){
cout << itt->first->cordinates << endl;
cout << " \n";
}
return 0;
Implementation of Node class:
star::Node::Node(Eigen::Matrix<float,3,1> _cordinates, float _h_cost, float _g_cost) : cordinates {_cordinates}, h_cost {_h_cost}, g_cost {_g_cost}{
f_cost = h_cost+g_cost;
obstical = false;
closedNode = false;
};
Thank you #timl and others for pointing me the correct direction. The problem was due to not satisfying the strict weak ordering requirement for comparison. Changing the comparator to the following seems to have made everything work:
struct NodeCostPointer {
bool operator()(star::Node* const& a,star::Node* const& b){
return std::tie(a->f_cost,a->h_cost,a->cordinates(0)) < std::tie(b->f_cost,b->h_cost,b->cordinates(0));
}
After learning about how this notation works I decided to add additional things for comparison i.e. h_cost and coordinates. However, not including them would have of course also worked.

C++ Return a Variable of an object

I am learning c++ currently and i have run to this problem:
Error Message: This Code should be of type bool or should be converted to bool.
The main function must stay the same, so i was wondering, that i use the line [A] and actuall return an bool.
The method should compare two cubics with each other, if they are the same or not the same.
Thanks in advance! :) <3
#include <iostream>
#include <cfloat>
class cubics
{
private:
double x,y,z;
bool var;
public:
cubics same(cubics cube)
{
double difference_x = x - cube.x;
double difference_y = y - cube.y;
double difference_z = z - cube.z;
if ( // If the difference between two objects are 0, then both cubics are the same; epsilon is used because we calculate with double floating precision to avoid the error)
(difference_x <= std::numeric_limits<double>::epsilon( )) and
(difference_y <= std::numeric_limits<double>::epsilon( )) and
(difference_z <= std::numeric_limits<double>::epsilon( ))
)
{
return (cube.var= true); // [A] I'm actually returning bool. But does the compiler want me to return the whole object!?
}
else
{
return (cube.var=false); // [A]
}
}
int main(){
cubics q2,q3;
cout << "The Cubics q2 and q3 are ";
if (q2.same(q3)) // <-- This line confuses me, however it must stay formally for my computerproject the same :) I understand that it means q2.same(q3) == true, but i don't know how i can return a boolean. I tryed [A]
cout << "same." << endl;
else
cout << "not same." << endl;
}
}
To return a boolean, you make the function… return a boolean.
Right now, it is trying to return an object of type cubics:
cubics same(cubics cube)
^^^^^^
Instead:
bool same(cubics cube)
^^^^
And return true, or return false, as appropriate.
That's it!
Your bool var doesn't need to exist at all.
I'd also recommend you take cube by reference; there's no need to take it by value, which makes a copy. So:
bool same(const cubics& cube)

Converting Eigen::SparseMatrix<double> to deal.ii ::SparseMatrix<double>?

This is kind of an obscure question and I don't really expect anyone to answer, but I have this method that takes (and returns) an Eigen::SparseMatrix. I want to put it into the deal.ii library, is there a way to copy/convert a SparseMatrix from deal.ii/Eigen? I know you can copy deal.ii to Trilinos SparseMatrix something like:
`SparseMatrix<double> matrix(sparsity);
...//fill matrix
Epetra_Map map(TrilinosWrappers::types::int_type(5),
TrilinosWrappers::types::int_type(5),
0,
Utilities::Trilinos::comm_world());
TrilinosWrappers::SparseMatrix tmatrix;
tmatrix.reinit (map, map, matrix, 0, false);`
Is there a similar way Eigen::SparseMatrix? I guess Eigen don't really have that kind of support in deal.ii. So perhaps there is some 'brute force' type method, like this attempt at code which obviously doesn't work:
`
Eigen::SparseMatrix<double> ConvertToEigenMatrix(SparseMatrix<double> data)
{
Eigen::SparseMatrix<double> eMatrix(data.m(), data.n());
for (int i = 0; i < data.m(); ++i)
eMatrix.row(i) = Eigen::SparseMatrix<double> ::Map(&data[i][0], data.n());
return eMatrix;
`
Ok, so I figured out how to convert from dealii::SparseMatrix -> Eigen::SparseMatrix.
SparseMatrix<double>::iterator smi = matrix.begin();
SparseMatrix<double>::iterator smi_end = matrix.end();
unsigned int row,col;
double val;
for (; smi!=smi_end; ++smi)
{
row = smi->row();
col = smi->column();
val = smi->value();
spMat.insert(row, col) = val;
std::cout << val << std::endl;
}
No, I just need to figure out the reverse.
This question is old but maybe I can still help. I am one of the deal.II developers and I don't remember seeing this on the mailing list (which is much more active for these types of questions than SO).
A SparseMatrix in deal.II does not store its own sparsity pattern: instead, it stores a pointer to a SparsityPattern object. You'll need to loop over the eigen matrix twice: once to set up the SparsityPattern and a second time to copy matrix values. Something like the following seems to work:
#include <deal.II/lac/dynamic_sparsity_pattern.h>
#include <deal.II/lac/sparsity_pattern.h>
#include <deal.II/lac/sparse_matrix.h>
#include <eigen3/Eigen/Sparse>
#include <iostream>
int main()
{
const std::size_t shape = 3;
Eigen::SparseMatrix<double> matrix(shape, shape);
matrix.insert(0, 0) = 1.0;
matrix.insert(0, 1) = 2.0;
matrix.insert(0, 2) = 1.0;
matrix.insert(2, 2) = 2.0;
matrix.makeCompressed();
{
dealii::SparsityPattern sparsity_pattern(matrix.rows(), matrix.cols());
dealii::DynamicSparsityPattern dynamic_sparsity_pattern(matrix.rows(), matrix.cols());
for (decltype(matrix.outerSize()) row_n = 0; row_n < matrix.outerSize(); ++row_n)
for (Eigen::SparseMatrix<double>::InnerIterator it(matrix, row_n); it; ++it)
dynamic_sparsity_pattern.add(it.row(), it.col());
sparsity_pattern.copy_from(dynamic_sparsity_pattern);
dealii::SparseMatrix<double> matrix2(sparsity_pattern);
for (decltype(matrix.outerSize()) row_n = 0; row_n < matrix.outerSize(); ++row_n)
for (Eigen::SparseMatrix<double>::InnerIterator it(matrix, row_n); it; ++it)
matrix2.set(it.row(), it.col(), it.value());
matrix2.print(std::cout); // prints the right matrix
}
}
You will have to manage the lifetime of the SparsityPattern object too.
deal.II does not use CSR or CSC: it uses its own CSR-like format where the entry on the main diagonal is stored first in the array containing the matrix entries for that row, so we really do need to copy with the iterator interfaces.

how to improve natural sort program for decimals?

I have std::strings containing numbers in the leading section that I need to sort. The numbers can be integers or floats.
The vector<std::string> sort was not optimal, I found the following natural sort program which was much better. I still have a small issue with numbers smaller than zero that do not sort just right. Does anyone have a suggestion to improve? We're using Visual Studio 2003.
The complete program follows.
TIA,
Bert
#include <list>
#include <string>
#include <iostream>
using namespace std;
class MyData
{
public:
string m_str;
MyData(string str) {
m_str = str;
}
long field1() const
{
int second = m_str.find_last_of("-");
int first = m_str.find_last_of("-", second-1);
return atol(m_str.substr(first+1, second-first-1).c_str());
}
long field2() const
{
return atol(m_str.substr(m_str.find_last_of("-")+1).c_str());
}
bool operator < (const MyData& rhs)
{
if (field1() < rhs.field1()) {
return true;
} else if (field1() > rhs.field1()) {
return false;
} else {
return field2() < rhs.field2();
}
}
};
int main()
{
// Create list
list<MyData> mylist;
mylist.push_front(MyData("93.33"));
mylist.push_front(MyData("0.18"));
mylist.push_front(MyData("485"));
mylist.push_front(MyData("7601"));
mylist.push_front(MyData("1001"));
mylist.push_front(MyData("0.26"));
mylist.push_front(MyData("0.26"));
// Sort the list
mylist.sort();
// Dump the list to check the result
for (list<MyData>::const_iterator elem = mylist.begin(); elem != mylist.end(); ++elem)
{
cout << (*elem).m_str << endl;
}
return 1;
}
GOT:
0.26
0.26
0.18
93.33
485
1001
7601
EXPECTED:
0.18
0.26
0.26
93.33
485
1001
7601
Use atof() instead of atol() to have the comparison take the fractional part of the number into account. You will also need to change the return types to doubles.
If it's just float strings, I'd rather suggest to create a table with two columns (first row contains the original string, second row is filled with the string converted to float), sort this by the float column and then output/use the sorted string column.
If the data are all numbers I would create a new class to contain the data.
It can have a string to include the data but then allows you to have better methods to model behaviour - in this case espacially to implement operator <
The implementation could also include use of a library that calculates to exact precion e.g. GNU multiple precision this would do the comparison and canversion from string (or if the numbers do not have that many significant figures you could use doubles)
I would compute the values once and store them.
Because they are not actually part of the objects state (they are just calcualted values) mark them as mutable. Then they can also be set during const methods.
Also note that MyClass is a friend of itself and thus can access the private members of another object of the same class. So there is no need for the extranious accessor methods. Remember Accessor methods are to protect other classes from changes in the implementation not the class you are implementing.
The problem with ordering is that atoi() is only reading the integer (ie it stops at the '.' character. Thus all your numbers smaller than 0 have a zero value for comparison and thus they will appear in a random order. To compare against the full value you need to extract them as a floating point value (double).
class MyData
{
private:
mutable bool gotPos;
mutable double f1;
mutable double f2;
public:
/*
* Why is this public?
*/
std::string m_str;
MyData(std::string str)
:gotPos(false)
,m_str(str) // Use initializer list
{
// If you are always going to build f1,f2 then call BuildPos()
// here and then you don't need the test in the operator <
}
bool operator < (const MyData& rhs)
{
if (!gotPos)
{ buildPos();
}
if (!rhs.gotPos)
{ rhs.buildPos();
}
if (f1 < rhs.f1) return true;
if (f1 > rhs.f1) return false;
return f2 < rhs.f2;
}
private:
void buildPos() const
{
int second = m_str.find_last_of("-");
int first = m_str.find_last_of("-", second-1);
// Use boost lexical cast as it handles doubles
// As well as integers.
f1 = boost::lexical_cast<double>(m_str.substr(first + 1, second-first - 1));
f2 = boost::lexical_cast<double>(m_str.substr(second + 1));
gotPos = true;
}
};

What is the best way to create a sparse array in C++?

I am working on a project that requires the manipulation of enormous matrices, specifically pyramidal summation for a copula calculation.
In short, I need to keep track of a relatively small number of values (usually a value of 1, and in rare cases more than 1) in a sea of zeros in the matrix (multidimensional array).
A sparse array allows the user to store a small number of values, and assume all undefined records to be a preset value. Since it is not physically possibly to store all values in memory, I need to store only the few non-zero elements. This could be several million entries.
Speed is a huge priority, and I would also like to dynamically choose the number of variables in the class at runtime.
I currently work on a system that uses a binary search tree (b-tree) to store entries. Does anyone know of a better system?
For C++, a map works well. Several million objects won't be a problem. 10 million items took about 4.4 seconds and about 57 meg on my computer.
My test application is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <map>
class triple {
public:
int x;
int y;
int z;
bool operator<(const triple &other) const {
if (x < other.x) return true;
if (other.x < x) return false;
if (y < other.y) return true;
if (other.y < y) return false;
return z < other.z;
}
};
int main(int, char**)
{
std::map<triple,int> data;
triple point;
int i;
for (i = 0; i < 10000000; ++i) {
point.x = rand();
point.y = rand();
point.z = rand();
//printf("%d %d %d %d\n", i, point.x, point.y, point.z);
data[point] = i;
}
return 0;
}
Now to dynamically choose the number of variables, the easiest solution is to represent index as a string, and then use string as a key for the map. For instance, an item located at [23][55] can be represented via "23,55" string. We can also extend this solution for higher dimensions; such as for three dimensions an arbitrary index will look like "34,45,56". A simple implementation of this technique is as follows:
std::map data<string,int> data;
char ix[100];
sprintf(ix, "%d,%d", x, y); // 2 vars
data[ix] = i;
sprintf(ix, "%d,%d,%d", x, y, z); // 3 vars
data[ix] = i;
The accepted answer recommends using strings to represent multi-dimensional indices.
However, constructing strings is needlessly wasteful for this. If the size isn’t known at compile time (and thus std::tuple doesn’t work), std::vector works well as an index, both with hash maps and ordered trees. For std::map, this is almost trivial:
#include <vector>
#include <map>
using index_type = std::vector<int>;
template <typename T>
using sparse_array = std::map<index_type, T>;
For std::unordered_map (or similar hash table-based dictionaries) it’s slightly more work, since std::vector does not specialise std::hash:
#include <vector>
#include <unordered_map>
#include <numeric>
using index_type = std::vector<int>;
struct index_hash {
std::size_t operator()(index_type const& i) const noexcept {
// Like boost::hash_combine; there might be some caveats, see
// <https://stackoverflow.com/a/50978188/1968>
auto const hash_combine = [](auto seed, auto x) {
return std::hash<int>()(x) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
};
return std::accumulate(i.begin() + 1, i.end(), i[0], hash_combine);
}
};
template <typename T>
using sparse_array = std::unordered_map<index_type, T, index_hash>;
Either way, the usage is the same:
int main() {
using i = index_type;
auto x = sparse_array<int>();
x[i{1, 2, 3}] = 42;
x[i{4, 3, 2}] = 23;
std::cout << x[i{1, 2, 3}] + x[i{4, 3, 2}] << '\n'; // 65
}
Boost has a templated implementation of BLAS called uBLAS that contains a sparse matrix.
https://www.boost.org/doc/libs/release/libs/numeric/ublas/doc/index.htm
Eigen is a C++ linear algebra library that has an implementation of a sparse matrix. It even supports matrix operations and solvers (LU factorization etc) that are optimized for sparse matrices.
Complete list of solutions can be found in the wikipedia. For convenience, I have quoted relevant sections as follows.
https://en.wikipedia.org/wiki/Sparse_matrix#Dictionary_of_keys_.28DOK.29
Dictionary of keys (DOK)
DOK consists of a dictionary that maps (row, column)-pairs to the
value of the elements. Elements that are missing from the dictionary
are taken to be zero. The format is good for incrementally
constructing a sparse matrix in random order, but poor for iterating
over non-zero values in lexicographical order. One typically
constructs a matrix in this format and then converts to another more
efficient format for processing.[1]
List of lists (LIL)
LIL stores one list per row, with each entry containing the column
index and the value. Typically, these entries are kept sorted by
column index for faster lookup. This is another format good for
incremental matrix construction.[2]
Coordinate list (COO)
COO stores a list of (row, column, value) tuples. Ideally, the entries
are sorted (by row index, then column index) to improve random access
times. This is another format which is good for incremental matrix
construction.[3]
Compressed sparse row (CSR, CRS or Yale format)
The compressed sparse row (CSR) or compressed row storage (CRS) format
represents a matrix M by three (one-dimensional) arrays, that
respectively contain nonzero values, the extents of rows, and column
indices. It is similar to COO, but compresses the row indices, hence
the name. This format allows fast row access and matrix-vector
multiplications (Mx).
Small detail in the index comparison. You need to do a lexicographical compare, otherwise:
a= (1, 2, 1); b= (2, 1, 2);
(a<b) == (b<a) is true, but b!=a
Edit: So the comparison should probably be:
return lhs.x<rhs.x
? true
: lhs.x==rhs.x
? lhs.y<rhs.y
? true
: lhs.y==rhs.y
? lhs.z<rhs.z
: false
: false
Hash tables have a fast insertion and look up. You could write a simple hash function since you know you'd be dealing with only integer pairs as the keys.
The best way to implement sparse matrices is to not to implement them - atleast not on your own. I would suggest to BLAS (which I think is a part of LAPACK) which can handle really huge matrices.
Since only values with [a][b][c]...[w][x][y][z] are of consequence, we only store the indice themselves, not the value 1 which is just about everywhere - always the same + no way to hash it. Noting that the curse of dimensionality is present, suggest go with some established tool NIST or Boost, at least read the sources for that to circumvent needless blunder.
If the work needs to capture the temporal dependence distributions and parametric tendencies of unknown data sets, then a Map or B-Tree with uni-valued root is probably not practical. We can store only the indice themselves, hashed if ordering ( sensibility for presentation ) can subordinate to reduction of time domain at run-time, for all 1 values. Since non-zero values other than one are few, an obvious candidate for those is whatever data-structure you can find readily and understand. If the data set is truly vast-universe sized I suggest some sort of sliding window that manages file / disk / persistent-io yourself, moving portions of the data into scope as need be. ( writing code that you can understand ) If you are under commitment to provide actual solution to a working group, failure to do so leaves you at the mercy of consumer grade operating systems that have the sole goal of taking your lunch away from you.
Here is a relatively simple implementation that should provide a reasonable fast lookup (using a hash table) as well as fast iteration over non-zero elements in a row/column.
// Copyright 2014 Leo Osvald
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef UTIL_IMMUTABLE_SPARSE_MATRIX_HPP_
#define UTIL_IMMUTABLE_SPARSE_MATRIX_HPP_
#include <algorithm>
#include <limits>
#include <map>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
// A simple time-efficient implementation of an immutable sparse matrix
// Provides efficient iteration of non-zero elements by rows/cols,
// e.g. to iterate over a range [row_from, row_to) x [col_from, col_to):
// for (int row = row_from; row < row_to; ++row) {
// for (auto col_range = sm.nonzero_col_range(row, col_from, col_to);
// col_range.first != col_range.second; ++col_range.first) {
// int col = *col_range.first;
// // use sm(row, col)
// ...
// }
template<typename T = double, class Coord = int>
class SparseMatrix {
struct PointHasher;
typedef std::map< Coord, std::vector<Coord> > NonZeroList;
typedef std::pair<Coord, Coord> Point;
public:
typedef T ValueType;
typedef Coord CoordType;
typedef typename NonZeroList::mapped_type::const_iterator CoordIter;
typedef std::pair<CoordIter, CoordIter> CoordIterRange;
SparseMatrix() = default;
// Reads a matrix stored in MatrixMarket-like format, i.e.:
// <num_rows> <num_cols> <num_entries>
// <row_1> <col_1> <val_1>
// ...
// Note: the header (lines starting with '%' are ignored).
template<class InputStream, size_t max_line_length = 1024>
void Init(InputStream& is) {
rows_.clear(), cols_.clear();
values_.clear();
// skip the header (lines beginning with '%', if any)
decltype(is.tellg()) offset = 0;
for (char buf[max_line_length + 1];
is.getline(buf, sizeof(buf)) && buf[0] == '%'; )
offset = is.tellg();
is.seekg(offset);
size_t n;
is >> row_count_ >> col_count_ >> n;
values_.reserve(n);
while (n--) {
Coord row, col;
typename std::remove_cv<T>::type val;
is >> row >> col >> val;
values_[Point(--row, --col)] = val;
rows_[col].push_back(row);
cols_[row].push_back(col);
}
SortAndShrink(rows_);
SortAndShrink(cols_);
}
const T& operator()(const Coord& row, const Coord& col) const {
static const T kZero = T();
auto it = values_.find(Point(row, col));
if (it != values_.end())
return it->second;
return kZero;
}
CoordIterRange
nonzero_col_range(Coord row, Coord col_from, Coord col_to) const {
CoordIterRange r;
GetRange(cols_, row, col_from, col_to, &r);
return r;
}
CoordIterRange
nonzero_row_range(Coord col, Coord row_from, Coord row_to) const {
CoordIterRange r;
GetRange(rows_, col, row_from, row_to, &r);
return r;
}
Coord row_count() const { return row_count_; }
Coord col_count() const { return col_count_; }
size_t nonzero_count() const { return values_.size(); }
size_t element_count() const { return size_t(row_count_) * col_count_; }
private:
typedef std::unordered_map<Point,
typename std::remove_cv<T>::type,
PointHasher> ValueMap;
struct PointHasher {
size_t operator()(const Point& p) const {
return p.first << (std::numeric_limits<Coord>::digits >> 1) ^ p.second;
}
};
static void SortAndShrink(NonZeroList& list) {
for (auto& it : list) {
auto& indices = it.second;
indices.shrink_to_fit();
std::sort(indices.begin(), indices.end());
}
// insert a sentinel vector to handle the case of all zeroes
if (list.empty())
list.emplace(Coord(), std::vector<Coord>(Coord()));
}
static void GetRange(const NonZeroList& list, Coord i, Coord from, Coord to,
CoordIterRange* r) {
auto lr = list.equal_range(i);
if (lr.first == lr.second) {
r->first = r->second = list.begin()->second.end();
return;
}
auto begin = lr.first->second.begin(), end = lr.first->second.end();
r->first = lower_bound(begin, end, from);
r->second = lower_bound(r->first, end, to);
}
ValueMap values_;
NonZeroList rows_, cols_;
Coord row_count_, col_count_;
};
#endif /* UTIL_IMMUTABLE_SPARSE_MATRIX_HPP_ */
For simplicity, it's immutable, but you can can make it mutable; be sure to change std::vector to std::set if you want a reasonable efficient "insertions" (changing a zero to a non-zero).
I would suggest doing something like:
typedef std::tuple<int, int, int> coord_t;
typedef boost::hash<coord_t> coord_hash_t;
typedef std::unordered_map<coord_hash_t, int, c_hash_t> sparse_array_t;
sparse_array_t the_data;
the_data[ { x, y, z } ] = 1; /* list-initialization is cool */
for( const auto& element : the_data ) {
int xx, yy, zz, val;
std::tie( std::tie( xx, yy, zz ), val ) = element;
/* ... */
}
To help keep your data sparse, you might want to write a subclass of unorderd_map, whose iterators automatically skip over (and erase) any items with a value of 0.