source: cpp/common/util-stl.h @ 1203

Last change on this file since 1203 was 1130, checked in by Maciej Komosinski, 4 years ago

Used std::min(), std::max() explicitly to avoid compiler confusion. Used std::size() explicitly instead of the equivalent macro

File size: 1.6 KB
RevLine 
[840]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
[1124]2// Copyright (C) 1999-2021  Maciej Komosinski and Szymon Ulatowski.
[840]3// See LICENSE.txt for details.
4
5#ifndef _UTIL_STL_H_
6#define _UTIL_STL_H_
7
[1124]8#include <map>
[1130]9#include <algorithm>
[840]10
11template<typename T, std::size_t N> void push_back(vector<T>& v, T(&d)[N])
12{
13        for (unsigned int i = 0; i < N; i++)
14                v.push_back(d[i]);
15}
16
17template<typename T> void erase(vector<T>& v, const T& e)
18{
19        typename vector<T>::iterator it = std::find(v.begin(), v.end(), e);
20        if (it != v.end())
21                v.erase(it);
22}
23
24template<typename T> void deleteVectorElements(vector<T*>& v)
25{
26        for (typename vector<T*>::iterator it = v.begin(); it != v.end(); it++)
27                delete *it;
28        v.clear();
29}
30
31template<typename T> int findIndex(vector<T>& v, const T& e)
32{
33        typename vector<T>::iterator it = find(v.begin(), v.end(), e);
34        if (it != v.end())
[888]35                return int(&*it - &v.front());
[840]36        return -1;
37}
38
[1124]39template<typename Key,typename Value> Value mapValueOrDefault(const std::map<Key,Value> &map, const Key& key, const Value& default_value)
40{
41        auto found = map.find(key);
42        if (found != map.end())
43                return found->second;
44        else
45                return default_value;
46}
47
[840]48template<class T> class DeletingVector  // deletes the elements (pointers) in destructor
49{
50public:
51        std::vector<T*> vector;
52        ~DeletingVector()
53        {
[888]54                for (int i = (int)vector.size() - 1; i >= 0; i--)
[840]55                        delete vector[i];
56        }
57        T* operator[](int i) { return vector[i]; }
58        int size() { return vector.size(); }
59        void push_back(T* x) { vector.push_back(x); }
60};
61
62#endif
Note: See TracBrowser for help on using the repository browser.