Goal
I am working on implementing an IntegerRing, which is a structure in abstract algebra. This type of ring is an Abelian group (something that I have already implemented) under addition. Rings are equipped with two operators, + and *.
Choice of implementation
For this reason, I have decided to define IntegerGroup as class that has GroupElements that have the operators. The complete, working code for that is found below:
IntegerGroup.h
#ifndef DATAGROUP_H
#define DATAGROUP_H
#include "Array.h"
#include <iostream>
// This group is the integers mod n
// multiplication in integer group is simply integer addition modulo n
class IntegerGroup
{
public:
IntegerGroup();
IntegerGroup(int);
class GroupElement
{
int m;
IntegerGroup* group;
public:
GroupElement();
GroupElement(int, IntegerGroup*);
~GroupElement();
GroupElement operator*(const GroupElement&);
GroupElement operator*=(const GroupElement&);
bool operator==(const GroupElement&);
bool operator!=(const GroupElement&);
int val() const;
friend std::ostream& operator<<(std::ostream& o, const GroupElement& e)
{
return (o << e.m);
}
};
GroupElement identity() const;
int size() const;
friend std::ostream& operator<<(std::ostream& o, const IntegerGroup& g)
{
return (o << g.elements);
}
private:
int n;
//GroupElement * identity;
Array<GroupElement> elements;
void createNewElement(int);
};
#endif
IntegerGroup.cpp
#include "IntegerGroup.h"
#include <new>
#include <iostream>
IntegerGroup::IntegerGroup()
{
}
IntegerGroup::IntegerGroup(int n)
: n(n), elements(Array<IntegerGroup::GroupElement>(n))
{
//this is to have integers in [0,n-1]
for (int j = 0; j < n; j++)
{
this->createNewElement(j);
}
}
void IntegerGroup::createNewElement(int m)
{
// create new GroupElement
GroupElement newElement(m, this);
// store it at index m in elements
this->elements[m] = newElement;
}
IntegerGroup::GroupElement::GroupElement()
: group(0)
{
}
IntegerGroup::GroupElement::GroupElement(int m, IntegerGroup * g)
: group(g)
{
// this->m must be in [0, g->size() - 1]
this->m = m % g->size();
if (this->m < 0) this->m = g->size() + this->m;
}
IntegerGroup::GroupElement::~GroupElement()
{
if (this->group)
{
this->group = 0;
}
}
IntegerGroup::GroupElement IntegerGroup::identity() const
{
// IntegerGroup consists of all integers in [0, n-1], and identity is 0
return this->elements[0];
}
// this group is simply the integers mod n, and should be populated integers in [0,n-1]
// thus, multiplication is simply a matter of returning the element at index (a+b)%n
IntegerGroup::GroupElement IntegerGroup::GroupElement::operator*(const IntegerGroup::GroupElement& b)
{
// if the group is not defined
if (!this->group)
// we simply perform integer multiplication
return GroupElement(this->val() * b.val());
// otherwise, perform group multiplication
return GroupElement((this->val() + b.val()) % this->group->size());
}
IntegerGroup::GroupElement IntegerGroup::GroupElement::operator*=(const IntegerGroup::GroupElement& b)
{
return ((*this) = (*this) * b);
}
bool IntegerGroup::GroupElement::operator==(const IntegerGroup::GroupElement& b)
{
return this->m == b.m;
}
bool IntegerGroup::GroupElement::operator!=(const IntegerGroup::GroupElement& b)
{
return !(*this == b);
}
int IntegerGroup::GroupElement::val() const { return this->m; }
int IntegerGroup::size() const { return this->n; }
Array.cpp, Array.h are merely templated wrapper classes. The code to that is also already working. You can find the files for them on GitHub here, or you could use std::vector instead. (It just now occurred to me that right now, I could do that.)
The problem
When I tried creating IntegerRing, and compiling, I got a myriad of bizarre errors, most of which had to do with the class's own functions using private class data.
Here is my implementation thus far of IntegerRing:
IntegerRing.h
#ifndef INTEGERRING_H
#define INTEGERRING_H
#include "IntegerGroup.h"
#include "Operators.h"
class IntegerRing : public IntegerGroup
{
public:
class Element : public IntegerGroup::GroupElement
{
public:
using IntegerGroup::GroupElement;
/*Element();
Element(int);
Element(int, IntegerRing*);
~Element();*/
operator IntegerGroup::GroupElement() { return IntegerGroup::GroupElement(); }
Element(const IntegerGroup::GroupElement& el)
{
// copy everything from el into *this
this->m = el.m;
this->group = el.group;
}
/*Element operator+(const Element&);
Element operator-(const Element&);
Element operator*(const Element&);
Element operator+=(const Element&);
Element operator-=(const Element&);
Element operator*=(const Element&);*/
};
Element identity(Operators);
private:
};
#endif
IntegerRing.cpp
#include "IntegerRing.h"
#include "IntegerGroup.h"
#include "Operators.h"
/*IntegerRing::Element::Element()
{
}*/
/*IntegerRing::Element(const IntegerGroup::GroupElement& el)
{
// copy everything from el into *this
this->m = el.m;
this->group = el.group;
}
/*
IntegerRing::Element IntegerRing::Element::operator+(const IntegerRing::Element& b)
{
// IntegerRing is simply Abelian group under addition
// thus, we treat the elements like group elements first, multiply under that group, and cast to ring elements
return (IntegerRing::Element)(((IntegerGroup::GroupElement)(*this)) * ((IntegerGroup::GroupElement)b));
}
IntegerRing::Element IntegerRing::Element::operator-(const IntegerRing::Element& b)
{
int val;
// if this has a group
if (this->group)
{
// compute (this->m - b.m) % this->group->size()
val = (this->m - b.m) % this->group->size();
// if that value is negative, add this->group->size() to it
if (val < 0) val = this->group->size() + val;
}
// otherwise, val is simply the integer difference of this->m,b.m
else val = this->m - b.m;
// return element with this value
return Element(val);
}
IntegerRing::Element IntegerRing::Element::operator*(const IntegerRing::Element& b)
{
if (this->group)
return IntegerRing::Element((this->m - b.m) % this->group->size());
return IntegerRing::Element(this->m - b.m);
}
IntegerRing::Element IntegerRing::Element::operator+=(const IntegerRing::Element& b)
{
return ((*this) = (*this) + b);
}
IntegerRing::Element IntegerRing::Element::operator-=(const IntegerRing::Element& b)
{
return ((*this) = (*this) - b);
}
IntegerRing::Element IntegerRing::Element::operator*=(const IntegerRing::Element& b)
{
return ((*this) = (*this) * b);
}
*/
IntegerRing::Element IntegerRing::identity(Operators op)
{
// if op is ADDITIVE
if (op == ADDITIVE)
// return what the base version of this method would return
return (IntegerRing::Element)(((IntegerGroup::GroupElement*)this)->identity());
// multiplicative identity requested, and it is 1
return (IntegerRing::Element)this->elements[0];
}
Operators.h
#ifndef OPERATORS_H
#define OPERATORS_H
enum Operators
{
ADDITIVE, MULTIPLICATIVE
};
#endif
The compiler thinks the copy constructor for IntegerRing::Element is really a function that returns an int.
Screenshot of errors
Here is screenshot of errors:
How do I resolve all this?
The reason is that you can't access class's private fields.
Inheritance/Nested Class do not change this.(Exception is inner class can always access it's enclosing class's any member(since C++11))
For the first error in log using IntegerGroup::GroupElement; should be usingIntegerGroup::GroupElement::GroupElement; inside IntegerRing::Element, by the way, I don't see the need of this class.
Turns out that my years of not using C++ for serious OOP like this has caused me to forget things. First of which: derived classes have access to protected,public members, and not private unless you declare derived class a friend in base class.
Second: how to write copy constructors. Sadly, derived classes have access to their own inherited protected data members, not base class's. To remedy this, I just write copy constructor like this:
IntegerRing::Element::Element(const IntegerGroup::GroupElement::GroupElement& el)
: IntegerGroup::GroupElement(el)
{
}
Related
I'm attempting to get a basic constant forward-iterator to work in C++.
namespace Rcpp {
class SparseMatrix {
public:
IntegerVector i, p;
NumericVector x;
int begin_col(int j) { return p[j]; };
int end_col(int j) { return p[j + 1]; };
class iterator {
public:
int index;
iterator(SparseMatrix& g) : parent(g) {}
iterator(int ind) { index = ind; }; // ERROR!
bool operator!=(int x) const { return index != x; };
iterator operator++(int) { ++index; return (*this); };
int row() { return parent.i[index]; };
double value() { return parent.x[index]; };
private:
SparseMatrix& parent;
};
};
}
My intention is to use the iterator in contexts similar to the following:
// sum of values in column 7
Rcpp::SparseMatrix A(nrow, ncol, fill::random);
double sum = 0;
for(Rcpp::SparseMatrix::iterator it = A.begin_col(7); it != A.end_col(7); it++)
sum += it.value();
Two questions:
The compiler throws an error on the line indicated above: uninitialized reference member in 'class Rcpp::SparseMatrix&' [-fpermissive]. How can this be fixed?
How might double value() { return parent.x[index]; }; be re-worked to return a pointer to the value rather than a copy of the value?
A little context on the SparseMatrix class: like a dgCMatrix in R, this object of class SparseMatrix consists of three vectors:
i holds row pointers for every element in x
p gives indices in i which correspond to the start of each column
x contains non-zero values
Thanks to #Evg, here's the solution:
namespace Rcpp {
class SparseMatrix {
public:
IntegerVector i, p;
NumericVector x;
class iterator {
public:
int index;
iterator(SparseMatrix& g, int ind) : parent(g) { index = ind; }
bool operator!=(iterator x) const { return index != x.index; };
iterator& operator++() { ++index; return (*this); };
int row() { return parent.i[index]; };
double& value() { return parent.x[index]; };
private:
SparseMatrix& parent;
};
iterator begin_col(int j) { return iterator(*this, p[j]); };
iterator end_col(int j) { return iterator(*this, p[j + 1]); };
};
}
And it can be used as follows, for instance, to calculate colSums:
//[[Rcpp::export]]
Rcpp::NumericVector Rcpp_colSums(Rcpp::SparseMatrix& A) {
Rcpp::NumericVector sums(A.cols());
for (int i = 0; i < A.cols(); ++i)
for (Rcpp::SparseMatrix::iterator it = A.begin_col(i); it != A.end_col(i); it++)
sums(i) += it.value();
return sums;
}
And, the above function is faster than RcppArmadillo, RcppEigen, and R::Matrix equivalents when microbenchmarked from R!
Edit:
The above syntax is inspired by Armadillo. I've come to realize that a slightly different syntax (which involves fewer constructions) gives an iterator similar to Eigen:
class col_iterator {
public:
col_iterator(SparseMatrix& ptr, int col) : ptr(ptr) { indx = ptr.p[col]; max_index = ptr.p[col + 1]; }
operator bool() const { return (indx != max_index); }
col_iterator& operator++() { ++indx; return *this; }
const double& value() const { return ptr.x[indx]; }
int row() const { return ptr.i[indx]; }
private:
SparseMatrix& ptr;
int indx, max_index;
};
Which can then be used like this:
int col = 0;
for (Rcpp::SparseMatrix::col_iterator it(A, col); it; ++it)
Rprintf("row: %3d, value: %10.2e", it.row(), it.value());
So I created a class called myClass that takes in an int and has a private variable that stores the int as a vector in binary (i.e. 12 is '1100'). I want to define an operator that adds two myClass variables together as a vector of bools (aka bit-wise operation).
Here is the code I have:
class myClass {
public:
myClass();
myClass(int a);
myClass& operator+(const myClass& value);
private:
std::vector<bool> bit;
};
I want this to work in the main function:
int main() {
std::cin >> value;
Integer a = value;
std::cin >> value;
Integer b = value;
myClass c = a+b;
return 0;
}
Operator definition:
myClass myClass::operator+(const myClass& rhs) {
Integer c = // not sure what to do here
return c;
}
The part that's confusing me is that it must take in an integer but then the operator does the operation on the vector of bools.
Well obviously you need to do the same as when you add normal numbers on paper. Start with the lowest significance bits, and add them together. If the result overflows (eg. binary 1+1=10) then remember that overflow for the next iteration.
I'd strongly suggest that you first create constructor that takes bool array for your class:
myClass(std::vector<bool> bits);
We'll use that in the implementation. Now what you want is to add the lists of bools. I have created an implementation that doesn't care how big the lists are. This will be handy if you want to calculate with huge integers:
#include <vector>
bool add_bools(const bool A, const bool B) {
return !(A && B) && (A || B);
}
/** Loops over vectors and adds the booleans in them
the booleans are considered to be in little endian order
so that the least significant is first in the array. **/
std::vector<bool> add_vectors(const std::vector<bool>& first,
const std::vector<bool>& second) {
std::vector<bool> result;
// Remembers that previous addition produced false when
// we add true+true
bool overflow = false;
const int bits = first.size()>second.size()?first.size():second.size();
for (int i = 0; i < bits || overflow; ++i) {
bool bitA, bitB;
bitA = i<first.size() ? first[i]:false;
bitB = i<second.size() ? second[i]:false;
bool tmp_result = add_bools(bitA, bitB);
// remember to add overflow from previous iteration
result.push_back(add_bools(tmp_result, overflow));
// remember overflow for next iteration
overflow = (bitA&&bitB) || (overflow && tmp_result);
}
return result;
}
#include <iostream>
void test_add_vectors() {
std::vector<bool> first;
std::vector<bool> second;
const int bits = 5;
for (int i = 0, l = bits; i < l; ++i) {
first.push_back(false);
second.push_back(true);
}
first[0] = true;
std::vector<bool> result = add_vectors(first, second);
for (int i = 0, l = result.size(); i < l; ++i) {
std::cout<< (result[i]?'1':'0')<<" ";
}
}
You can use that implementation like this, making use of the constructor that takes bool array:
myClass myClass::operator+(const myClass& rhs) {
myClass result(add_vectors(bit, rhs.bit));
return result;
}
You need to define a way to go to and from an integer representation. Here's a rough idea:
#include <vector>
#include <iostream>
class myClass {
private:
void setInt(int x) {
bit.clear();
while (x) {
if (x & 1)
bit.push_back(1);
else
bit.push_back(0);
x>>=1;
}
reverse(bit.begin(), bit.end());
}
public:
int toInt() const {
int i = 0;
for (size_t b = 0; b < bit.size(); b++) {
if (bit[bit.size() - 1 - b])
i |= 1<<b;
}
return i;
}
myClass(int a) {
setInt(a);
}
myClass& operator+(const myClass& value) {
setInt(toInt() + value.toInt());
return *this;
}
private:
std::vector<bool> bit;
};
int main() {
myClass c(10);
myClass d(20);
std::cout << "c=" << c.toInt() << "\n";
std::cout << "d=" << d.toInt() << "\n";
std::cout << "Sum=" << (c + d).toInt() << "\n";
}
`myClass c = a+b;`
Since a and b are both declared as Integer, this line will call operator+(const Integer& x, const Integer& y) or Integer::operator+(const Integer& x). The only way it will call myClass::operator+(const myClass& rhs) is if you have a conversion constructor myClass::myClass(const Integer& i).
Suppose I have a simple vector class where elements are accessed through a proxy class.
Vector class:
class vec {
public:
vec(int len) {
length = len;
data = new double [len];
}
proxy operator[](int i) {
if (i >= 0 && i < length) {
return proxy(i, data);
}
else {
std::cerr << "AHHHH!\n";
exit(1);
}
}
private:
int length;
double * data;
};
Proxy class:
class proxy {
public:
proxy(int i, double * d) {
index = i;
data = d;
}
void operator=(double rhs) {
data[index] = rhs;
}
private:
int index;
double * data;
};
How can I assign elements from the vector (or rather, from the proxy) to a variable of type double? In other words, how do I accomplish the following:
int main() {
vec a(2);
double x = 3.14;
a[0] = x; // Works!
x = a[0]; // How to make work?
return 0;
}
Unfortunately, I can't write something like:
friend double operator=(double & lhs, const proxy & p) { ... }
since operator= must be a member.
Add a conversion function to your proxy class:
class proxy
{
public:
operator double() const { return data[index]; }
// ...
};
I have been trying to overload the + operator with 2 custom classes Fraction and Integer. I'd ideally like the + operator to return the simplest version of the operation (i.e. 1/4 + 3/4 == 1 (Integer) ). I haven't found a good way to dynamically assign the return type, so I've tried to return multiple values enclosed in a Struct or tuple. I encouter a segfault when actually attempting the operation in main as follows:
///main/////////
int main(){
Fraction *f = new Fraction(1,4);
Fraction *f2 = new Fraction(3,4);
Fraction *resF = new Fraction();//results
Integer *resI = new Integer();
boost::tie(resF, resI) = *f+*f2; //SEGFAULT here
}
The two classes involved are deriviatives of a common abstract base class, with members and functions defined here:
#include <boost/tuple/tuple.hpp>
#include <iostream>
//Number class
//forward declarations for tuple
class Integer;
class Fraction;
//abstract base class
template<class T>//T is derived class
class Number{
virtual const boost::tuple<Fraction*, Integer*> operator+ (const Number&) {};
virtual void display(std::ostream &) const {} ;
virtual bool operator==(const Number& rhs) const{} ;
};//end of Number class
//Integer class
class Integer: public Number<Integer>{
int numericValue;//<! the value of the integer
public:
int getValue() const;//<!access private member variable numericValue
void setValue(int);//<!set private member variable numericValue
Integer();//<!default constructor
Integer(int);//<!param constructor
virtual ~Integer() {}//<!destructor
//display
void display(std::ostream &) const;//<!stream a display of the number
//int == int
bool operator==(const Integer&) const;//<! comparator int-int
// int + int
const Integer operator+ (const Integer &);//<! add int+int
};
//DEFINITIONS////////////////////
//Default constructor
Integer::Integer(){
numericValue = 0;
}
// param constructor
Integer::Integer(int num){
numericValue = num;
}
//get integer value
int Integer::getValue() const{
return this->numericValue;
}
//set integer value
void Integer::setValue(int x){
this->numericValue = x;
}
//display int
void Integer::display(std::ostream& stream) const{
stream << this->numericValue<<std::endl;
}
// int + int
const Integer Integer::operator+(const Integer &rhs){
Integer temp = this->numericValue + rhs.numericValue;
return temp;
}
// int == int
bool Integer::operator==(const Integer& rhs) const{
if(this->numericValue == rhs.numericValue)
return true;
else
return false;
}
//end of Integer class
//Fraction class
class Fraction: public Number<Fraction>{
Integer numerator;
Integer denominator;
boost::tuple<Fraction*, Integer*> resOfAdd;
public:
int getNumerator();//<! to access private member
int getDenominator();//<! to access private member
bool isInteger;//<! flag if the fraction result of '+' can be reduced as an integer
bool isWhole();//!<tells if can be simplified to integer
Integer fToI;//<! store the integer value of the fraction if it is whole
Fraction() = default;//<! default constructor
Fraction(const int &, const int &);//<!param constructor
const Fraction simplify(const Fraction &in);//<! simplifies fraction if possible
int gcdCalculate(int lhs, int rhs);//!<greatest common denominator
int lcmCalculate(const int lhs, const int rhs);//<!least common
virtual ~Fraction() {}
//display
void display(std::ostream &) const;
// frac == frac
bool operator==(const Fraction& rhs) const;
//frac + frac
boost::tuple<Fraction*, Integer*> operator+(const Fraction &);
};//end of Fraction class
//DEFINITIONS///////////////////
// param constructor
Fraction::Fraction(const int & num, const int & den){
numerator.setValue(num);
denominator.setValue(den);
if(denominator.getValue()==1){//also an integer
fToI = Integer(numerator.getValue());
}
if(denominator.getValue() < 0 && numerator.getValue() > 0){//negative sign on bottom
denominator.setValue(denominator.getValue()*-1);
numerator.setValue(numerator.getValue()*-1); //switch it to the top
}
if(denominator.getValue() < 0 && numerator.getValue() < 0){//both top and bottom are negative
denominator.setValue(denominator.getValue()*-1);
numerator.setValue(numerator.getValue()*-1); //flip them to positive
}
}
//get ifInteger
bool Fraction::isWhole(){
return this->isInteger;
}
//get numerator
int Fraction::getNumerator(){
return this->numerator.getValue();
}
//get denominator
int Fraction::getDenominator(){
return this->denominator.getValue();
}
// display the fraction value
void Fraction::display(std::ostream & stream) const{
stream << this->numerator.getValue() << "/" << this->denominator.getValue()<<std::endl;
}
//simplify fraction
const Fraction Fraction::simplify(const Fraction &in){
int gcd = gcdCalculate(in.numerator.getValue(), in.denominator.getValue());
Fraction res = Fraction(in.numerator.getValue()/gcd, in.denominator.getValue()/gcd);
return res;
}
//lcm - least common multiplier
int Fraction::lcmCalculate(const int lhs, const int rhs){
int temp = gcdCalculate(lhs, rhs);
return temp ? (lhs / temp * rhs) : 0;
}
//gcd - greatest common divisor
int Fraction::gcdCalculate(int a, int b){
return b == 0 ? a : gcdCalculate(b, a % b);
}
//frac + frac -- causing problem
boost::tuple<Fraction*, Integer*>/*numRep<Fraction, Integer>*/ Fraction::operator+(const Fraction &rhsIn){
int numRes, denRes;
Fraction* resF;
Integer* resI; //if there is an integer result
//simplify input
Fraction lhs = simplify(*this);
Fraction rhs = simplify(rhsIn);
int lcm = lcmCalculate(lhs.denominator.getValue(), rhs.denominator.getValue());
int gcd = gcdCalculate(lhs.denominator.getValue(), rhs.denominator.getValue());
//share denominator?
if(lhs.denominator.getValue() == rhs.denominator.getValue()){
numRes = lhs.numerator.getValue() + rhs.numerator.getValue();//simply add the numerators
denRes = lhs.denominator.getValue();//keep denominator
}
else{
// a1 a2 a1*b2+a2*b1
// -- + -- = -----------
// b1 b2 b1*b2
int a1 = lhs.getNumerator();
int b1 = lhs.getDenominator();
int a2 = rhs.numerator.getValue();
int b2 = rhs.denominator.getValue();
numRes = a1*b2 + a2*b1;
denRes = b1*b2;
}
*resF = Fraction(numRes, denRes);
//simplify
*resF = simplify(*resF);
if(resF->denominator.getValue() == 1){//integer result
resF->isInteger = true;//flag
fToI = Integer(resF->numerator.getValue());//make Integer
resI = &fToI; //return the integer when you can
}
else{
resI = new Integer(0);
}
//put the fraction and the (possible) integer representations into a number struct
resOfAdd = boost::make_tuple(resF, resI);
std::cout<<" + = ";
resF->display(std::cout);
delete resF;
delete resI;
return resOfAdd;
}
I must be doing something wrong to get the same segfault error using both a struct and tuple. Could anyone advise on my mistake, or suggest an alternate/superior solution to assign the return value dynamically? I understand that dynamically flexible return types may not be possible. Thank you for your time and help.
Fraction* resf;
...
*resf =
resf is an uninitialized pointer, and you are trying to copy-assign something to the location it points to.
Returning a pointer here is a bad idea because it introduces ownership semantics. Just return by value:
boost::tuple<Fraction, Integer> ...
If you were using pointers so you could indicate whether the integer was present or not, consider using boost::optional.
I have a simple two-dimensional line class which holds two vectors of doubles. I have added getValue and setValue functions, but would prefer the public interface to have the square bracket operator available alongside these functions. The following code shows the implementation and use:
#include <vector>
#include <algorithm>
#include <cassert>
class Simple2DLine
{
public:
Simple2DLine();
// Simple read method with linear interpolation
double getValue(double x) const;
// Simple write method, adds a curve point, keeping the arrays sorted
void setValue(double x, double y);
double& operator [](double x);
const double operator [](double x) const;
private:
std::vector<double> m_X;
std::vector<double> m_Y;
int getNearestIndex(double x) const;
};
Simple2DLine::Simple2DLine()
{
}
void Simple2DLine::setValue(double x, double y)
{
// Get the index of the point at or just before 'x'
int idx = getNearestIndex(x);
// Check if the exact point already exists.
if (idx >= 0)
{
if (m_X[idx] == x)
{
m_Y[idx] = y;
return;
}
else
{
// Insert adds the value just BEFORE idx, so increment it before inserting.
++idx;
m_X.insert(m_X.begin() + idx,x);
m_Y.insert(m_Y.begin() + idx,y);
return;
}
}
// Otherwise, just insert at the front.
m_X.insert(m_X.begin(),x);
m_Y.insert(m_Y.begin(),y);
}
double Simple2DLine::getValue(double x) const
{
// Make sure there are points - if not, return 0.
if (m_X.size() == 0)
{
return 0;
}
// Make sure it's not out of bounds.
if (x < m_X.front() || x > m_X.back())
{
return 0;
}
// Check if it's at or after the last point
if (x == m_X.back())
{
return m_X.back();
}
// Find the point just before the given point.
int idx = getNearestIndex(x);
// Check if we're on the exact point
if (m_X[idx] == x)
{
return m_X[idx];
}
else
{
// Find the distance from the nearest point and linearly interpolate.
double dist = x - m_X[idx];
return m_Y[idx] + dist * (m_Y[idx + 1] - m_Y[idx]) / (m_X[idx + 1] - m_X[idx]);
}
}
double& Simple2DLine::operator [](double x)
{
// Create a space for the new value
setValue(x,0.0);
int idx = getNearestIndex(x);
return m_Y[idx];
}
const double Simple2DLine::operator [](double x) const
{
return getValue(x);
}
// Returns the index of the point at or just before 'x'. Invalid values return -1.
int Simple2DLine::getNearestIndex(double x) const
{
if (m_X.empty())
{
return -1;
}
std::vector<double>::const_iterator xBegin(m_X.begin());
std::vector<double>::const_iterator xEnd(m_X.end());
// Get an iterator to the first value GREATER than our search value
std::vector<double>::const_iterator it = upper_bound(xBegin,xEnd,x);
// If the iterator is at the beginning, all values are greater
if (it == xBegin)
{
return -1;
}
// Otherwise, decrement the iterator by 1, and return its' distance from the start.
return (it - 1) - xBegin;
}
int main(int argc, char** argv)
{
Simple2DLine tda;
tda.setValue(0.0,10.0);
tda.setValue(1.0,15.0);
tda.setValue(2.0,20.0);
tda.setValue(3.0,25.0);
double tmp = tda.getValue(0.5);
assert(abs(tmp - 12.5) < 0.000001);
tmp = tda.getValue(1.5);
assert(abs(tmp - 17.5) < 0.000001);
tmp = tda.getValue(2.5);
assert(abs(tmp - 22.5) < 0.000001);
// Here, the wrong version of the overridden operator is being called.
tmp = tda[1.5];
tda[2.5] = 22.5;
}
When I access the line object in the following fashion, the correct version of the operator is called (non-const)
tda[2.5] = 22.5;
However, when I try to use the const version, as follows:
tmp = tda[1.5];
the non-const version is called. Is there an error in my implementation? Or is it not possible to access the class in this fashion?
The const version is called on const objects. So if you have an object declared like const Simple2DLine tda, const overloaded version of operator[] will be called.
Practically, you will see const objects as function parameters like:
void foo(const Simple2DLine& tda)
{
std::cout<< tda[0];
}
There you will notice const overloaded function being called.
Also your const overloaded operator[] can still return a reference.
Do you assume that automatically the const operator has to be called just because the expression containing it appears on the right side of an equation? This is not the way it works. The const version will be called if you have a const object.
You could e.g. try assigning the object to a const reference.
Simple2DLine const & tdaconst = tda;
tmp = tdaconst[1.5];
In the above code, the const version will be called.