diff options
Diffstat (limited to 'utils')
| -rw-r--r-- | utils/fast_sparse_vector.h | 19 | ||||
| -rw-r--r-- | utils/filelib.cc | 12 | ||||
| -rw-r--r-- | utils/filelib.h | 1 | 
3 files changed, 23 insertions, 9 deletions
diff --git a/utils/fast_sparse_vector.h b/utils/fast_sparse_vector.h index 4aae2039..9d72cb87 100644 --- a/utils/fast_sparse_vector.h +++ b/utils/fast_sparse_vector.h @@ -235,6 +235,13 @@ class FastSparseVector {      }      return *this;    } +  FastSparseVector<T> erase_zeros(const T& EPSILON = 1e-4) const { +    FastSparseVector<T> o; +    for (const_iterator it = begin(); it != end(); ++it) { +      if (fabs(it->second) > EPSILON) o.set_value(it->first, it->second); +    } +    return o; +  }    const_iterator begin() const {      return const_iterator(*this, false);    } @@ -344,15 +351,9 @@ const FastSparseVector<T> operator+(const FastSparseVector<T>& x, const FastSpar  template <typename T>  const FastSparseVector<T> operator-(const FastSparseVector<T>& x, const FastSparseVector<T>& y) { -  if (x.size() > y.size()) { -    FastSparseVector<T> res(x); -    res -= y; -    return res; -  } else { -    FastSparseVector<T> res(y); -    res -= x; -    return res; -  } +  FastSparseVector<T> res(x); +  res -= y; +  return res;  }  template <class T> diff --git a/utils/filelib.cc b/utils/filelib.cc index 79ad2847..a0969b1a 100644 --- a/utils/filelib.cc +++ b/utils/filelib.cc @@ -20,3 +20,15 @@ bool DirectoryExists(const string& dir) {    return false;  } +void MkDirP(const string& dir) { +  if (DirectoryExists(dir)) return; +  if (mkdir(dir.c_str(), 0777)) { +    perror(dir.c_str()); +    abort(); +  } +  if (chmod(dir.c_str(), 07777)) { +    perror(dir.c_str()); +    abort(); +  } +} + diff --git a/utils/filelib.h b/utils/filelib.h index dda98671..a8622246 100644 --- a/utils/filelib.h +++ b/utils/filelib.h @@ -12,6 +12,7 @@  bool FileExists(const std::string& file_name);  bool DirectoryExists(const std::string& dir_name); +void MkDirP(const std::string& dir_name);  // reads from standard in if filename is -  // uncompresses if file ends with .gz  | 
