summaryrefslogtreecommitdiffstats
blob: c481aa85a8cf726c043fca04064cef7c3dfb5731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * Copyright (c) 2011-2014 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#pragma once

#include <algorithm>
#include <list>
#include <map>
#include <string>
#include <vector>

namespace Swift {

    /*
     * Generic erase()
     */
    namespace Detail {
        struct VectorCategory {};
        struct ListCategory {};
        struct MapCategory {};

        template<typename T>
        struct ContainerTraits;

        template<typename A, typename B>
        struct ContainerTraits< std::vector<A, B> > {
            typedef VectorCategory Category;
        };

        template<>
        struct ContainerTraits< std::string > {
            typedef VectorCategory Category;
        };

        template<typename A, typename B>
        struct ContainerTraits< std::list<A, B> > {
            typedef ListCategory Category;
        };

        template<typename A, typename B, typename C, typename D>
        struct ContainerTraits< std::map<A, B, C, D> > {
            typedef MapCategory Category;
        };

        template<typename C, typename V>
        void eraseImpl(C& c, const V& v, VectorCategory) {
            c.erase(std::remove(c.begin(), c.end(), v), c.end());
        }

        template<typename C, typename V>
        void eraseImpl(C& c, const V& v, ListCategory) {
            c.remove(v);
        }

        template<typename C, typename V>
        void eraseImpl(C& c, const V& v, MapCategory) {
          for (typename C::iterator it = c.begin(); it != c.end(); ) {
                if (it->second == v) {
                    c.erase(it++);
                }
                else {
                    ++it;
                }
            }
        }

        template<typename C, typename P>
        void eraseIfImpl(C& c, const P& p, MapCategory) {
          for (typename C::iterator it = c.begin(); it != c.end(); ) {
                if (p(*it)) {
                    c.erase(it++);
                }
                else {
                    ++it;
                }
            }
        }

        template<typename C, typename P>
        void eraseIfImpl(C& c, const P& p, VectorCategory) {
            c.erase(std::remove_if(c.begin(), c.end(), p), c.end());
        }
    }

    template<typename C, typename V>
    void erase(C& container, const V& value) {
        Detail::eraseImpl(container, value, typename Detail::ContainerTraits<C>::Category());
    }

    template<typename C, typename P>
    void eraseIf(C& container, const P& predicate) {
        Detail::eraseIfImpl(container, predicate, typename Detail::ContainerTraits<C>::Category());
    }

    template<typename Source, typename Target>
    void append(Target& target, const Source& source) {
        target.insert(target.end(), source.begin(), source.end());
    }

    template<typename Source, typename Target>
    void assign(Target& target, const Source& source) {
        target.assign(source.begin(), source.end());
    }

    template<typename A, typename B, typename C, typename D>
    B get(const std::map<A, B, C, D>& map, const A& key, const B& defaultValue) {
        typename std::map<A, B, C, D>::const_iterator i = map.find(key);
        if (i != map.end()) {
            return i->second;
        }
        else {
            return defaultValue;
        }
    }

    template<typename Container>
    void safeClear(Container& c) {
        std::fill(c.begin(), c.end(), 0);
        c.clear();
    }

    /*
     * Functors
     */
    template<typename K, typename V>
    class PairFirstEquals {
        public:
            PairFirstEquals(const K& value) : value(value) {
            }

            bool operator()(const std::pair<K,V>& pair) const {
                return pair.first == value;
            }

        private:
            K value;
    };

    template<typename K, typename V>
    class PairSecondEquals {
        public:
            PairSecondEquals(const V& value) : value(value) {
            }

            bool operator()(const std::pair<K,V>& pair) const {
                return pair.second == value;
            }

        private:
            V value;
    };
}