//========================================================================= // STLUTIL.H - part of // OMNeT++/OMNEST // Discrete System Simulation in C++ // // Author: Andras Varga // //========================================================================= /*--------------------------------------------------------------* Copyright (C) 2006-2017 OpenSim Ltd. This file is distributed WITHOUT ANY WARRANTY. See the file `license' for details on this and other legal matters. *--------------------------------------------------------------*/ #ifndef __OMNETPP_COMMON_STLUTIL_H #define __OMNETPP_COMMON_STLUTIL_H // various utility functions to make STL containers more usable #include #include #include #include #include #include "commonutil.h" // Assert() namespace omnetpp { namespace common { template typename std::vector& addAll(std::vector& v, const std::vector& w) { v.insert(v.end(), w.begin(), w.end()); return v; } template typename std::set& addAll(std::set& s, const std::set& t) { s.insert(t.begin(), t.end()); return s; } template inline std::map& addAll(std::map& m, const std::map& n) { m.insert(n.begin(), n.end()); return m; } template typename std::vector::iterator find(std::vector& v, const T& a) { return std::find(v.begin(), v.end(), a); } template typename std::vector::const_iterator find(const std::vector& v, const T& a) { return std::find(v.begin(), v.end(), a); } template inline int count(const std::vector& v, const T& a) { return std::count(v.begin(), v.end(), a); } template int indexOf(const std::vector& v, const T& a) { auto it = find(v, a); return it == v.end() ? -1 : it - v.begin(); } template inline bool contains(const std::vector& v, const T& a) { return find(v, a) != v.end(); } template inline bool contains(const std::set& s, const T& a) { return s.find(a) != s.end(); } template inline bool containsKey(const std::map& m, const K& a) { return m.find(a) != m.end(); } template inline bool containsKey(const std::unordered_map& m, const K& a) { return m.find(a) != m.end(); } template void insert(std::vector& v, int pos, const T& a) { Assert(pos >= 0 && pos <= (int)v.size()); v.insert(v.begin() + pos, a); } template void erase(std::vector& v, int pos) { Assert(pos >= 0 && pos < (int)v.size()); v.erase(v.begin() + pos); } template inline void remove(std::vector& v, const A& a) { v.erase(std::remove(v.begin(), v.end(), a), v.end()); } template inline std::vector keys(const std::map& m) { std::vector result; for (auto it = m.begin(); it != m.end(); ++it) result.push_back(it->first); return result; } template inline std::vector values(const std::map& m) { std::vector result; for (auto it = m.begin(); it != m.end(); ++it) result.push_back(it->second); return result; } template void sort(std::vector& v) { std::sort(v.begin(), v.end()); } template std::vector sorted(const std::vector& v) { std::vector result = v; std::sort(result.begin(), result.end()); return result; } template std::string to_str(const std::vector& v) { std::stringstream out; out << '['; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) out << ", "; out << *it; } out << "]"; return out.str(); } template std::string to_str(const std::map& m) { std::stringstream out; out << '{'; for (auto it = m.begin(); it != m.end(); ++it) { if (it != m.begin()) out << ", "; out << it->first << " -> " << it->second; } out << "}"; return out.str(); } } // namespace common } // namespace omnetpp #endif