Coverage Report

Created: 2023-02-22 06:06

/src/wxwidgets/include/wx/hashset.h
Line
Count
Source (jump to first uncovered line)
1
/////////////////////////////////////////////////////////////////////////////
2
// Name:        wx/hashset.h
3
// Purpose:     wxHashSet class
4
// Author:      Mattia Barbon
5
// Modified by:
6
// Created:     11/08/2003
7
// Copyright:   (c) Mattia Barbon
8
// Licence:     wxWindows licence
9
/////////////////////////////////////////////////////////////////////////////
10
11
#ifndef _WX_HASHSET_H_
12
#define _WX_HASHSET_H_
13
14
#include "wx/hashmap.h"
15
16
// see comment in wx/hashmap.h which also applies to different standard hash
17
// set classes
18
19
#if wxUSE_STD_CONTAINERS
20
21
#include <unordered_set>
22
23
// we need to define the class declared by _WX_DECLARE_HASH_SET as a class and
24
// not a typedef to allow forward declaring it
25
#define _WX_DECLARE_HASH_SET_IMPL( KEY_T, HASH_T, KEY_EQ_T, PTROP, CLASSNAME, CLASSEXP )  \
26
CLASSEXP CLASSNAME                                                            \
27
    : public std::unordered_set< KEY_T, HASH_T, KEY_EQ_T >                    \
28
{                                                                             \
29
public:                                                                       \
30
    explicit CLASSNAME(size_type n = 3,                                       \
31
                       const hasher& h = hasher(),                            \
32
                       const key_equal& ke = key_equal(),                     \
33
                       const allocator_type& a = allocator_type())            \
34
        : std::unordered_set< KEY_T, HASH_T, KEY_EQ_T >(n, h, ke, a)          \
35
    {}                                                                        \
36
    template <class InputIterator>                                            \
37
    CLASSNAME(InputIterator f, InputIterator l,                               \
38
              const hasher& h = hasher(),                                     \
39
              const key_equal& ke = key_equal(),                              \
40
              const allocator_type& a = allocator_type())                     \
41
        : std::unordered_set< KEY_T, HASH_T, KEY_EQ_T >(f, l, h, ke, a)       \
42
    {}                                                                        \
43
    CLASSNAME(const std::unordered_set< KEY_T, HASH_T, KEY_EQ_T >& s)         \
44
        : std::unordered_set< KEY_T, HASH_T, KEY_EQ_T >(s)                    \
45
    {}                                                                        \
46
}
47
48
#define _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, PTROP, CLASSNAME, CLASSEXP )   \
49
    _WX_DECLARE_HASH_SET_IMPL(                                                \
50
        KEY_T,                                                                \
51
        HASH_T,                                                               \
52
        KEY_EQ_T,                                                             \
53
        PTROP,                                                                \
54
        CLASSNAME,                                                            \
55
        CLASSEXP)
56
57
#else // no appropriate STL class, use our own implementation
58
59
// this is a complex way of defining an easily inlineable identity function...
60
#define _WX_DECLARE_HASH_SET_KEY_EX( KEY_T, CLASSNAME, CLASSEXP )            \
61
CLASSEXP CLASSNAME                                                           \
62
{                                                                            \
63
    typedef KEY_T key_type;                                                  \
64
    typedef const key_type const_key_type;                                   \
65
    typedef const_key_type& const_key_reference;                             \
66
public:                                                                      \
67
0
    CLASSNAME() { }                                                          \
68
    const_key_reference operator()( const_key_reference key ) const          \
69
0
        { return key; }                                                      \
70
};
71
72
#define _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, PTROP, CLASSNAME, CLASSEXP )\
73
_WX_DECLARE_HASH_SET_KEY_EX( KEY_T, CLASSNAME##_wxImplementation_KeyEx, CLASSEXP ) \
74
_WX_DECLARE_HASHTABLE( KEY_T, KEY_T, HASH_T,                                 \
75
    CLASSNAME##_wxImplementation_KeyEx, KEY_EQ_T, PTROP,                     \
76
    CLASSNAME##_wxImplementation_HashTable, CLASSEXP, grow_lf70, never_shrink ) \
77
CLASSEXP CLASSNAME:public CLASSNAME##_wxImplementation_HashTable             \
78
{                                                                            \
79
public:                                                                      \
80
    _WX_DECLARE_PAIR( iterator, bool, Insert_Result, CLASSEXP )              \
81
                                                                             \
82
    explicit CLASSNAME( size_type hint = 100, hasher hf = hasher(),          \
83
                        key_equal eq = key_equal() )                         \
84
        : CLASSNAME##_wxImplementation_HashTable( hint, hf, eq,              \
85
0
                      CLASSNAME##_wxImplementation_KeyEx() ) {}              \
86
                                                                             \
87
    Insert_Result insert( const key_type& key )                              \
88
0
    {                                                                        \
89
0
        bool created;                                                        \
90
0
        Node *node = GetOrCreateNode( key, created );                        \
91
0
        return Insert_Result( iterator( node, this ), created );             \
92
0
    }                                                                        \
93
                                                                             \
94
    const_iterator find( const const_key_type& key ) const                   \
95
0
    {                                                                        \
96
0
        return const_iterator( GetNode( key ), this );                       \
97
0
    }                                                                        \
98
                                                                             \
99
    iterator find( const const_key_type& key )                               \
100
0
    {                                                                        \
101
0
        return iterator( GetNode( key ), this );                             \
102
0
    }                                                                        \
103
                                                                             \
104
    size_type erase( const key_type& k )                                     \
105
0
        { return CLASSNAME##_wxImplementation_HashTable::erase( k ); }       \
106
0
    void erase( const iterator& it ) { erase( *it ); }                       \
107
0
    void erase( const const_iterator& it ) { erase( *it ); }                 \
108
                                                                             \
109
    /* count() == 0 | 1 */                                                   \
110
    size_type count( const const_key_type& key ) const                       \
111
0
        { return GetNode( key ) ? 1 : 0; }                                   \
112
}
113
114
#endif // STL/wx implementations
115
116
117
// these macros are to be used in the user code
118
#define WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME) \
119
    _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, wxPTROP_NORMAL, CLASSNAME, class )
120
121
// and these do exactly the same thing but should be used inside the
122
// library
123
// note: DECL is not used since the class is inline
124
#define WX_DECLARE_HASH_SET_WITH_DECL( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME, DECL) \
125
    _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, wxPTROP_NORMAL, CLASSNAME, class )
126
127
#define WX_DECLARE_EXPORTED_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME) \
128
    WX_DECLARE_HASH_SET_WITH_DECL( KEY_T, HASH_T, KEY_EQ_T, \
129
                                   CLASSNAME, class WXDLLIMPEXP_CORE )
130
131
// Finally these versions allow to define hash sets of non-objects (including
132
// pointers, hence the confusing but wxArray-compatible name) without
133
// operator->() which can't be used for them. This is mostly used inside the
134
// library itself to avoid warnings when using such hash sets with some less
135
// common compilers (notably Sun CC).
136
#define WX_DECLARE_HASH_SET_PTR( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME) \
137
    _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, wxPTROP_NOP, CLASSNAME, class )
138
// note: DECL is not used since the class is inline
139
#define WX_DECLARE_HASH_SET_WITH_DECL_PTR( KEY_T, HASH_T, KEY_EQ_T, CLASSNAME, DECL) \
140
    _WX_DECLARE_HASH_SET( KEY_T, HASH_T, KEY_EQ_T, wxPTROP_NOP, CLASSNAME, class )
141
142
// delete all hash elements
143
//
144
// NB: the class declaration of the hash elements must be visible from the
145
//     place where you use this macro, otherwise the proper destructor may not
146
//     be called (a decent compiler should give a warning about it, but don't
147
//     count on it)!
148
#define WX_CLEAR_HASH_SET(type, hashset)                                     \
149
    {                                                                        \
150
        type::iterator it, en;                                               \
151
        for( it = (hashset).begin(), en = (hashset).end(); it != en; ++it )  \
152
            delete *it;                                                      \
153
        (hashset).clear();                                                   \
154
    }
155
156
#endif // _WX_HASHSET_H_