view src/std_ex.hpp @ 0:add35537fdbb tip

Initial import
author irh <ian.r.hobson@gmail.com>
date Thu, 25 Aug 2011 11:05:55 +0100
parents
children
line wrap: on
line source
//  Copyright 2011, Ian Hobson.
//
//  This file is part of gpsynth.
//
//  gpsynth is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  gpsynth is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with gpsynth in the file COPYING. 
//  If not, see http://www.gnu.org/licenses/.

// Miscellaneous C++ stdlib helper utilities

#pragma once

#include <cstdlib>
#include <iostream>
#include <iterator>
#include <fstream>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>

namespace stdx {
  
typedef unsigned int uint;

// Helper function for retrieving values from const std::maps
template<typename M>
const typename M::mapped_type& GetFromMap(const M& map, 
                                          const typename M::key_type& key) {
  typename M::const_iterator i = map.find(key);
  if (i == map.end()) {
    throw std::runtime_error("GetFromMap: specified key not in map.");
  }
  return i->second;
}


template<typename T>
inline T RandomRange(T min, T max) {
  return static_cast<T>(((double)rand() / (float)RAND_MAX) * (max - min) + min);
}

template<typename T>
inline T RandomRangeInt(T min, T max) {
  return static_cast<T>(((double)rand() / (float)RAND_MAX) 
                        * (max - min) 
                        + min + 0.5);
}
  
template<typename T>
inline T Random(T maximum) {
  return static_cast<T>(((double)rand() / (double)RAND_MAX) * maximum);
}

inline bool Random5050() {
  return rand() > (RAND_MAX / 2);
}
  
inline double RandomCoefficient() {
  return static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
}
  
inline bool Chance(double probability) {
  return RandomCoefficient() < probability;
}

template<typename T>
class RandomGenerator {
  T minimum_;
  T maximum_;
  
public:
  RandomGenerator(T minimum = 0, T maximum = 1)
  : minimum_(minimum),
  maximum_(maximum)
  {}
  
  T operator()() {
    return RandomRange(minimum_, maximum_);
  }
};


// clamps input T to range specified by U and V
template<typename T, typename U, typename V>
T Clamp(const T& value, const U& minimum, const V& maximum) {
  return (value > maximum) ? maximum : (value < minimum) ? minimum : value;
}
  
// Function object that increments its internal value by the provided step size
template<typename T>
class Stepper {
  T value_;
  T step_size_;
public:
  Stepper(const T& step_size, const T& initial_value = T())
  : value_(initial_value),
    step_size_(step_size)
  {}
  
  T operator()() {
    T result = value_;
    value_ += step_size_;
    return result;
  }
};
  
// Function object that returns true if value is less than internal value
template<typename T>
class LessThan {
  T value_;
public:
  LessThan(const T& value) : value_(value) {}
  bool operator()(const T& other) { return other < value_; }
};

// writes a container to a stream
template<typename Container>
void DumpContainer(const Container& container,
                   const std::string& separator = "\n",
                   std::ostream& output = std::cout) {
  typedef typename Container::value_type Value;
  std::copy(container.begin(), container.end(),
            std::ostream_iterator<Value>(output, separator.c_str()));
}

// saves a container to a specified file path
template<typename Container>
void SaveContainerToFile(const Container& container,
                         const std::string& file_path,
                         const std::string& separator = "\n") {
  std::ofstream file(file_path.c_str());
  DumpContainer(container, separator, file);
}

template<typename T>
void AppendToFile(const T& value_to_append,
                  const std::string& file_path,
                  const std::string& separator = "\n") {
  std::ofstream file(file_path.c_str(),
                     std::ofstream::app | std::ofstream::out);
  file << value_to_append << separator;
}
  
inline std::string LoadFile(const std::string& file_path) {
  std::ifstream file(file_path.c_str());
  std::stringstream buffer;
  buffer << file.rdbuf();
  return buffer.str();
}

} // stdx namespace