Product SiteDocumentation Site

5.2.2. Class utility

File: wmr_common.h
class utility
{
public:
    template <typename T>
    static std::string toString(const T &);
    template <typename T>
    static T fromString(const std::string &);

    static int stringToInt(const std::string &);
    static long stringToLong(const std::string &);
    static float stringToFloat(const std::string &);
    static double stringToDouble(const std::string &);
    static bool stringToBool(const std::string &);

    static std::vector<std::string> split(const std::string &, char);
    static std::vector<std::string> split_multi(const std::string &, const std::string &);
};

template <typename T, typename K>
static void emit(const T &, const K &);
Provides a set of helper methods to those using this library. In particular it focuses on string manipulation and conversion since data is passed to and from the mapper and reducer as strings.
static std::string toString(const T &)
Converts data of type Tto a std::string by using a std::stringstream object. Returns the string that is created.
static T fromString(const std::string &)
Converts the given std::string to type T by using a std::stringstream object. Returns the data that is created from the string.
static std::vector<std::string> split(const std::string &, char)
Splits the given string (argument 1) on every instance of the given delimiter (argument 2). Only a single delimiter may be passed to this method. Returns a std::vector of std::string objects resulting from the split.
static std::vector<std::string> split_multi(const std::string &, const std::string &)
Splits the given string (argument 1) on every instance of any of the delimiters passed in argument 2. If two delimiters occur side-by-side, they are skipped (no empty string is provided). Returns a std::vector of std::string objects resulting from the split.
static void emit(const T &, const K &)
Prints the first argument followed by the delimiter specified by wmr_common (almost always tab) followed by the second argument to standard output. Emit can only be called with types that support the << operator with a std::ostream object (cout).
Convenience methods to avoid templates:
static int stringToInt(const std::string &)
Since the generic fromString<T> method returns data of type T, the template parameters must be given when calling it. Given the audience of WMR and the strong emphasis on beginning CS students, non-templated methods have been provided in case providing template arguments provides to be too confusing. This method calls fromString<T> with the appropriate template parameter.
static long stringToLong(const std::string &)
See: utility::stringToInt
static float stringToFloat(const std::string &)
See: utility::stringToInt
static double stringToDouble(const std::string &)
See: utility::stringToInt
static bool stringToBool(const std::string &)
See: utility::stringToInt