summaryrefslogtreecommitdiffstats
path: root/src/usr/diag/prdf/common/util/UtilMapX.H
blob: 523e54a2538936df6c3d46d3d7fa0a40f8b7b680 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/diag/prdf/common/util/UtilMapX.H $                    */
/*                                                                        */
/* IBM CONFIDENTIAL                                                       */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2004,2013              */
/*                                                                        */
/* p1                                                                     */
/*                                                                        */
/* Object Code Only (OCO) source materials                                */
/* Licensed Internal Code Source Materials                                */
/* IBM HostBoot Licensed Internal Code                                    */
/*                                                                        */
/* The source code for this program is not published or otherwise         */
/* divested of its trade secrets, irrespective of what has been           */
/* deposited with the U.S. Copyright Office.                              */
/*                                                                        */
/* Origin: 30                                                             */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

#ifndef __UTIL_UTILMAPX_H
#define __UTIL_UTILMAPX_H

#include "UtilTree.H"
#include <algorithm>

namespace PRDF
{

template <class _A, class _B>
class UtilMapX
{
    public:
        UtilMapX();
        UtilMapX(const UtilMapX<_A,_B> &);


        void insert(const _A &, const _B &);
        void insert(const std::pair<_A,_B> &);
        void remove(const _A &);
        bool find(const _A &);
        const std::pair<_A,_B>& peek();
        void empty() { cv_tree.empty(); };
        _B & operator[] (const _A &);

    private:
        UtilTree cv_tree;

        class mapXComparator : public UtilTree::comparator
        {
            public:
                virtual int operator() (void * _a, void * _b) const
                {
                    std::pair<_A,_B>* a;
                    std::pair<_A,_B>* b;

                    a = static_cast<std::pair<_A,_B>*>(_a);
                    b = static_cast<std::pair<_A,_B>*>(_b);

                    return (a->first < b->first ? -1 :
                                (a->first == b->first ? 0 : 1));
                };
        };

        class mapXCleanup : public UtilTree::cleanup
        {
            public:
                virtual void operator() (void * _a) const
                {
                    std::pair<_A,_B>* a = static_cast<std::pair<_A,_B>*>(_a);
                    delete a;
                };
        };

        class mapXCopier : public UtilTree::copier
        {
            public:
                virtual void * operator() (void * _a) const
                {
                    std::pair<_A,_B>* a = static_cast<std::pair<_A,_B>*>(_a);
                    return (void *) new std::pair<_A,_B>(*a);
                };
        };

        mapXComparator cv_compare;
        mapXCleanup cv_clean;
        mapXCopier cv_copy;

    public:
        class iterator
        {
            private:
                UtilTree::iterator _pos;
            public:
                iterator(UtilTree::iterator i) { _pos = i; };
                iterator & operator++()
                    { ++_pos; return *this; };
                iterator & operator--()
                    { --_pos; return *this; };
                bool operator==(const iterator& i) const
                    { return _pos == i._pos; };
                bool operator!=(const iterator& i) const
                    { return _pos != i._pos; };
                std::pair<_A, _B> operator*()
                    {
                        std::pair<_A, _B> * a =
                            static_cast<std::pair<_A, _B> *>(*_pos);
                        if (NULL == a)
                            return std::pair<_A, _B>();
                        return *a;
                    };
        };
        iterator end() const { return iterator(cv_tree.end()); };
        iterator begin() const { return iterator(cv_tree.begin()); };

};

template <class _A, class _B>
UtilMapX<_A,_B>::UtilMapX()
{
    cv_tree.setComparator(&cv_compare);
    cv_tree.setCleanup(&cv_clean);
    cv_tree.setCopier(&cv_copy);
};

template <class _A, class _B>
UtilMapX<_A,_B>::UtilMapX(const UtilMapX<_A,_B> & i_copy)
{
    cv_tree = i_copy.cv_tree;
    cv_tree.setComparator(&cv_compare);
    cv_tree.setCleanup(&cv_clean);
    cv_tree.setCopier(&cv_copy);
};

template <class _A, class _B>
void UtilMapX<_A,_B>::insert(const _A & a, const _B & b)
{
    cv_tree.insert(new std::pair<_A,_B>(a,b));
};

template <class _A, class _B>
void UtilMapX<_A,_B>::insert(const std::pair<_A,_B> & i)
{
    cv_tree.insert(new std::pair<_A,_B>(i));
};

template <class _A, class _B>
void UtilMapX<_A,_B>::remove(const _A & a)
{
    std::pair<_A,_B> p(a,_B());
    cv_tree.remove(&p);
};

template <class _A, class _B>
bool UtilMapX<_A,_B>::find(const _A & a)
{
    std::pair<_A,_B> p(a,_B());
    return (NULL != cv_tree.find(&p));
};

template <class _A, class _B>
const std::pair<_A,_B> & UtilMapX<_A,_B>::peek()
{
    void * tmp = cv_tree.peek();
    if (NULL == tmp)
    {
        static const std::pair<_A,_B> l;
        return l;
    }
    return *static_cast<const std::pair<_A,_B> *>(tmp);
};

template <class _A, class _B>
_B & UtilMapX<_A, _B>::operator[] (const _A & a)
{
    std::pair<_A,_B> p(a,_B());
    std::pair<_A, _B> * l_node =
        static_cast<std::pair<_A, _B> *>(cv_tree.find(&p));

    if (NULL == l_node)
    {
        this->insert(p);
        l_node = static_cast<std::pair<_A, _B> *>(cv_tree.find(&p));
    }

    return l_node->second;
};

} // end namespace PRDF

#endif

OpenPOWER on IntegriCloud