From c1357ea1fa0fcc30dd3e52eff93b13c7b5e93b0a Mon Sep 17 00:00:00 2001
From: Max Bonomi <massimiliano.bonomi@gmail.com>
Date: Sun, 17 Jul 2011 13:28:31 -0700
Subject: [PATCH] Basic methods for SparseGrid Class. Need testing and
 optimization. Should we have some inline function?

---
 src/Grid.cpp | 57 +++++++++++++++++++++++++++++++++++++---------------
 src/Grid.h   | 25 ++++++++++++-----------
 2 files changed, 54 insertions(+), 28 deletions(-)

diff --git a/src/Grid.cpp b/src/Grid.cpp
index f2ef1a2fd..50966b5b5 100644
--- a/src/Grid.cpp
+++ b/src/Grid.cpp
@@ -2,6 +2,7 @@
 #include <cassert>
 #include <math.h>
 #include "Grid.h"
+#include <iostream>
 
 using namespace std;
 
@@ -64,8 +65,7 @@ unsigned Grid::getIndex(vector<unsigned> indices) const {
 
 unsigned Grid::getIndex(vector<double> x) const {
  assert(x.size()==dimension());
- vector<unsigned> indices=getIndices(x);
- return getIndex(indices);
+ return getIndex(getIndices(x));
 }
 
 // we are flattening arrays using a column-major order
@@ -101,8 +101,7 @@ vector<double> Grid::getPoint(vector<unsigned> indices) const {
 }
 
 vector<double> Grid::getPoint(unsigned index) const {
- vector<unsigned> indices=getIndices(index);
- return getPoint(indices);
+ return getPoint(getIndices(index));
 }
 
 double Grid::getValue(unsigned index) const { 
@@ -110,19 +109,13 @@ double Grid::getValue(unsigned index) const {
 }
 
 double Grid::getValue(vector<unsigned> indices) const {
- return grid_[getIndex(indices)];
+ return getValue(getIndex(indices));
 }
 
-double Grid::getValue(vector<double> x) const{
- assert(x.size()==dimension());
- vector<unsigned> indices;
- double value;
+double Grid::getValue(vector<double> x) const {
  if(dospline_){
 // TO DO 
- } else {
-  value=getValue(getIndices(x));
- }
- return value;
+ } else { return getValue(getIndices(x)); }
 }
 
 void Grid::setValue(unsigned index, double value){
@@ -130,7 +123,7 @@ void Grid::setValue(unsigned index, double value){
 }
 
 void Grid::setValue(vector<unsigned> indices, double value){
- grid_[getIndex(indices)]=value; 
+ setValue(getIndex(indices),value); 
 }
 
 void Grid::addValue(unsigned index, double value){
@@ -138,7 +131,7 @@ void Grid::addValue(unsigned index, double value){
 }
 
 void Grid::addValue(vector<unsigned> indices, double value){
- grid_[getIndex(indices)]+=value;
+ addValue(getIndex(indices),value);
 }
 
 // Sparse version of grid with map
@@ -148,4 +141,36 @@ void SparseGrid::clear(){
 
 unsigned SparseGrid::size() const{
  return map_.size();
-}
\ No newline at end of file
+}
+
+double SparseGrid::getValue(unsigned index) const { 
+ double value=0.0;
+ if(map_.find(index)!=map_.end()) value=map_.find(index)->second;
+ return value;
+}
+
+double SparseGrid::getValue(vector<unsigned> indices) const {
+ return getValue(getIndex(indices));
+}
+
+double SparseGrid::getValue(vector<double> x) const {
+ if(dospline_){
+// TO DO 
+ } else { return getValue(getIndices(x)); }
+}
+
+void SparseGrid::setValue(unsigned index, double value){
+ map_[index]=value;
+}
+
+void SparseGrid::setValue(vector<unsigned> indices, double value){
+ setValue(getIndex(indices),value); 
+}
+
+void SparseGrid::addValue(unsigned index, double value){
+ map_[index]+=value;
+}
+
+void SparseGrid::addValue(vector<unsigned> indices, double value){
+ addValue(getIndex(indices),value);
+}
diff --git a/src/Grid.h b/src/Grid.h
index a32504924..2213cbacd 100644
--- a/src/Grid.h
+++ b/src/Grid.h
@@ -21,31 +21,33 @@ public:
  double getMax(unsigned i) const;
  double getSide(unsigned i) const;
  unsigned dimension() const;
+ 
  vector<unsigned> getIndices(unsigned index) const;
  vector<unsigned> getIndices(vector<double> x) const;
  unsigned getIndex(vector<unsigned> indices) const;
  unsigned getIndex(vector<double> x) const;
  vector<double> getPoint(unsigned index) const;
  vector<double> getPoint(vector<unsigned> indices) const;
+ 
 
 //! a class that inherits from this, like SparseGrid, should override these methods
  virtual unsigned size() const;
  virtual void clear();
- virtual double getValue(vector<double> x) const;
- virtual double getValue(unsigned index) const;
+ virtual double getValue(unsigned index) const; 
  virtual double getValue(vector<unsigned> indices) const;
- virtual void setValue(vector<unsigned> indices, double value);
+ virtual double getValue(vector<double> x) const;
  virtual void setValue(unsigned index, double value);
+ virtual void setValue(vector<unsigned> indices, double value);
+ virtual void addValue(unsigned index, double value); 
  virtual void addValue(vector<unsigned> indices, double value);
- virtual void addValue(unsigned index, double value);
 
  ~Grid(){};
 };
 
 class SparseGrid : public Grid
 {
- std::map<unsigned,double> map_;
-
+ map<unsigned,double> map_;
+ map<unsigned,double>::iterator it_;
 
  public:
  SparseGrid(vector<double> gmin,vector<double> gmax,vector<unsigned> nbin,bool dospline):
@@ -53,14 +55,13 @@ class SparseGrid : public Grid
 
  unsigned size() const;
  void clear();
-/*
- double getValue(vector<double> x) const;
- double getValue(unsigned index) const;
+ double getValue(unsigned index) const; 
  double getValue(vector<unsigned> indices) const;
- void setValue(vector<unsigned> indices, double value);
+ double getValue(vector<double> x) const;
  void setValue(unsigned index, double value);
+ void setValue(vector<unsigned> indices, double value);
+ void addValue(unsigned index, double value); 
  void addValue(vector<unsigned> indices, double value);
- void addValue(unsigned index, double value);
-*/ 
+
  ~SparseGrid(){};
 };
\ No newline at end of file
-- 
GitLab