view src/utils.cpp @ 40:eaa99e09607d

Remove indentation due to namespace scope
author Jordi Gutiérrez Hermoso <jordigh@gmail.com>
date Fri, 12 Mar 2010 09:21:07 -0600 (2010-03-12)
parents 7f31f9e2d196
children 4134a0f2423d
line wrap: on
line source
#include "include/utils.hpp"
#include <string>
#include <map>
#include <algorithm>
#include <iostream>
#include <fstream>
#include "include/linalg.hpp"
#include "include/error.hpp"

namespace kwantix{
std::string trim(const std::string& s){
  if(s.length() == 0)
    return s;
  std::size_t beg = s.find_first_not_of(" \a\b\f\n\r\t\v");
  std::size_t end = s.find_last_not_of(" \a\b\f\n\r\t\v");
  if(beg == std::string::npos) // No non-spaces
    return "";
  return std::string(s, beg, end - beg + 1);
}

template<typename K, typename V> 
bool contains(const std::map<K,V>& m, K thing){
  return m.find(thing) != m.end();
}

template<typename E>
bool contains(const std::set<E>& s, E thing){
  return s.find(thing) != s.end();
}
  
template<typename E>
bool includes(const std::set<E>& s1, const std::set<E>& s2){
  return std::includes(s2.begin(), s2.end(), s1.begin(), s1.end());
}
  
using kwantix::vector;
using kwantix::matrix;

matrix read_matrix(std::string filename){
  std::ifstream ifs(filename.c_str());
  if(!ifs){
    kwantix::badArgument exc;
    exc.reason = "Cannot open file ";
    exc.reason += filename;
    exc.line = __LINE__;
    exc.file = __FILE__;
    throw exc;      
  }
  matrix v;
  ifs >> v;
  return v;
}

vector read_vector(std::string filename){
  std::ifstream ifs(filename.c_str());
  if(!ifs){
    kwantix::badArgument exc;
    exc.reason = "Cannot open file ";
    exc.reason += filename;
    exc.line = __LINE__;
    exc.file = __FILE__;
    throw exc;      
  }
  vector v;
  ifs >> v;
  return v;
}

std::map<kwantix::point, double> read_pd_map(std::string filename){
  std::ifstream ifs(filename.c_str());
  if(!ifs){
    kwantix::badArgument exc;
    exc.reason = "Cannot open file ";
    exc.reason += filename;
    exc.line = __LINE__;
    exc.file = __FILE__;
    throw exc;      
  }
  matrix M;
  ifs >> M;

  if(M.cols() < 2){
    kwantix::badArgument exc;
    exc.reason = 
      "Input matrix to read_pd_map is too narrow. \n"
      "Need at least two columns in the input matrix";
    exc.line = __LINE__;
    exc.file = __FILE__;
    throw exc;
  }
    
  std::map <kwantix::point, double> result;
  size_t m = M.cols();
  kwantix::slice s(1,m-1);
  for(size_t i = 1; i <= M.rows(); i++)
    result[M(i,s)] = M(i,m);
  return result;
}

void show_exception(kwantix::error exc){
  using namespace std;

  cerr << exc.file << ": ""Caught an exception!" << endl;
  cerr << exc.file << ":" << exc.line << " error: "<< exc.reason << endl;
  cerr << "From file " << exc.file << endl;
}

}

#include <boost/shared_ptr.hpp>
#include "include/ddm.hpp"
//Instantiations
namespace kwantix{
using boost::shared_ptr;
template bool contains(const std::set<kwantix::point>&, kwantix::point E);
template bool contains(const std::map<kwantix::point, kwantix::vector>& m, 
                       kwantix::point thing);
template bool includes(const std::set<kwantix::point>& s1, 
                       const std::set<kwantix::point>& s2);
template bool contains(const std::map<kwantix::point, 
                       shared_ptr<const kwantix::overlapping_domain> >&,
                       kwantix::point );

template bool contains(const std::set<shared_ptr
                       <const kwantix::overlapping_domain> >&, 
                       shared_ptr<const kwantix::overlapping_domain> E); 

}