vasnprintf: Don't use %n on modern, ISO C 99 compliant platforms.
[gnulib.git] / lib / gl_set.hh
blob3b64bc5b1abaeb4ba4ff402b95c52b22c6cb6d08
1 /* Abstract set data type as a C++ class.
2 Copyright (C) 2006-2020 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2018.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #ifndef _GL_SET_HH
19 #define _GL_SET_HH
21 #include "gl_set.h"
22 #include "gl_xset.h"
24 /* gl_Set is a C++ wrapper around the gl_set data type.
25 Its element type is 'ELTYPE *'.
27 It is merely a pointer, not a smart pointer. In other words:
28 it does NOT do reference-counting, and the destructor does nothing. */
30 template <class T> class gl_Set;
32 template <class ELTYPE>
33 class gl_Set<ELTYPE *>
35 public:
36 // ------------------------------ Constructors ------------------------------
38 gl_Set ()
39 : _ptr (NULL)
42 /* Creates an empty set.
43 IMPLEMENTATION is one of GL_ARRAY_SET, GL_LINKEDHASH_SET, GL_HASH_SET.
44 EQUALS_FN is an element comparison function or NULL.
45 HASHCODE_FN is an element hash code function or NULL.
46 DISPOSE_FN is an element disposal function or NULL. */
47 gl_Set (gl_set_implementation_t implementation,
48 bool (*equals_fn) (ELTYPE * /*elt1*/, ELTYPE * /*elt2*/),
49 size_t (*hashcode_fn) (ELTYPE *),
50 void (*dispose_fn) (ELTYPE *))
51 : _ptr (gl_set_create_empty (implementation,
52 reinterpret_cast<gl_setelement_equals_fn>(equals_fn),
53 reinterpret_cast<gl_setelement_hashcode_fn>(hashcode_fn),
54 reinterpret_cast<gl_setelement_dispose_fn>(dispose_fn)))
57 /* Copy constructor. */
58 gl_Set (const gl_Set& x)
59 { _ptr = x._ptr; }
61 /* Assignment operator. */
62 gl_Set& operator= (const gl_Set& x)
63 { _ptr = x._ptr; return *this; }
65 // ------------------------------- Destructor -------------------------------
67 ~gl_Set ()
68 { _ptr = NULL; }
70 // ----------------------- Read-only member functions -----------------------
72 /* Returns the current number of elements in the set. */
73 size_t size () const
74 { return gl_set_size (_ptr); }
76 /* Searches whether an element is already in the set.
77 Returns true if found, or false if not present in the set. */
78 bool search (ELTYPE * elt) const
79 { return gl_set_search (_ptr, elt); }
81 // ----------------------- Modifying member functions -----------------------
83 /* Adds an element to the set.
84 Returns true if it was not already in the set and added, false otherwise. */
85 bool add (ELTYPE * elt)
86 { return gl_set_add (_ptr, elt); }
88 /* Removes an element from the set.
89 Returns true if it was found and removed. */
90 bool remove (ELTYPE * elt)
91 { return gl_set_remove (_ptr, elt); }
93 /* Frees the entire set.
94 (But this call does not free the elements of the set. It only invokes
95 the DISPOSE_FN on each of the elements of the set.) */
96 void free ()
97 { gl_set_free (_ptr); _ptr = NULL; }
99 // ------------------------------ Private stuff ------------------------------
101 private:
102 gl_set_t _ptr;
104 public:
105 // -------------------------------- Iterators --------------------------------
106 // Only a forward iterator.
107 // Does not implement the STL operations (++, *, and != .end()), but a simpler
108 // interface that needs only one virtual function call per iteration instead
109 // of three.
111 class iterator {
112 public:
114 /* If there is a next element, stores the next element in ELT, advances the
115 iterator and returns true. Otherwise, returns false. */
116 bool next (ELTYPE *& elt)
118 const void *next_elt;
119 bool has_next = gl_set_iterator_next (&_state, &next_elt);
120 if (has_next)
121 elt = static_cast<ELTYPE *>(next_elt);
122 return has_next;
125 ~iterator ()
126 { gl_set_iterator_free (&_state); }
128 #if defined __xlC__ || defined __HP_aCC || defined __SUNPRO_CC
129 public:
130 #else
131 private:
132 friend iterator gl_Set::begin ();
133 #endif
135 iterator (gl_set_t ptr)
136 : _state (gl_set_iterator (ptr))
139 private:
141 gl_set_iterator_t _state;
144 /* Creates an iterator traversing the set.
145 The set's contents must not be modified while the iterator is in use,
146 except for removing the last returned element. */
147 iterator begin ()
148 { return iterator (_ptr); }
151 #endif /* _GL_SET_HH */