CMagic  0.5.0
Portable C library of utilities and data structures
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
cmagic::set< T > Class Template Reference

A container that stores unique elements following a specific order. More...

#include <set.hpp>

Classes

class  iterator
 

Public Types

using value_type = T
 Type of set elements. More...
 
using size_type = size_t
 Type used to measure element size. More...
 

Public Member Functions

 set ()
 Constructs an empty set with standard memory allocation. More...
 
 operator bool () const
 Checks if the set is properly initialized. More...
 
iterator begin () const
 Return iterator to beginning. More...
 
iterator end () const
 Return iterator to end. More...
 
void clear ()
 Removes all elements from the set, leaving the container with a size of 0. More...
 
std::pair< iterator, bool > insert (const value_type &val)
 Inserts a new element to the set if it is not equivalent to any element already contained in the set. More...
 
std::pair< iterator, bool > insert (value_type &&val)
 Inserts a new element to the set if it is not equivalent to any element already contained in the set. More...
 
void erase (const value_type &val)
 Removes a single element from the set. More...
 
size_type size () const
 Returns the number of elements in the set. More...
 
bool empty () const
 Returns whether the set is empty (i.e. whether its size is 0). More...
 
iterator find (const value_type &val) const
 Searches the container for an element equivalent to val and returns an iterator to it if found, otherwise it returns set::end. More...
 

Static Public Member Functions

static set custom_allocation_set ()
 Constructs an empty set using custom CMagic memory allocation from memory.h. More...
 

Detailed Description

template<typename T>
class cmagic::set< T >

A container that stores unique elements following a specific order.

Each value in the set is unique. The value of the elements in a set cannot be modified once in the container but they can be inserted or removed from the container. Set is implemented as an AVL tree.

Member Typedef Documentation

◆ size_type

template<typename T >
using cmagic::set< T >::size_type = size_t

Type used to measure element size.

◆ value_type

template<typename T >
using cmagic::set< T >::value_type = T

Type of set elements.

Constructor & Destructor Documentation

◆ set()

template<typename T >
cmagic::set< T >::set ( )
inline

Constructs an empty set with standard memory allocation.

Returns
a new empty set

Member Function Documentation

◆ begin()

template<typename T >
iterator cmagic::set< T >::begin ( ) const
inline

Return iterator to beginning.

Returns an iterator pointing to the first element in the set. If the container is empty, the returned iterator value shall not be dereferenced.

Returns
an iterator to the beginning of the container

◆ clear()

template<typename T >
void cmagic::set< T >::clear ( )
inline

Removes all elements from the set, leaving the container with a size of 0.

◆ custom_allocation_set()

template<typename T >
static set cmagic::set< T >::custom_allocation_set ( )
inlinestatic

Constructs an empty set using custom CMagic memory allocation from memory.h.

Returns
a new empty set

◆ empty()

template<typename T >
bool cmagic::set< T >::empty ( ) const
inline

Returns whether the set is empty (i.e. whether its size is 0).

This function does not modify the container in any way. To clear the content of a set, see set::clear.

Returns
true if the container size is 0, false otherwise

◆ end()

template<typename T >
iterator cmagic::set< T >::end ( ) const
inline

Return iterator to end.

It does not point to any element, and thus shall not be dereferenced.

Returns
an iterator to the element past the end of the sequence

◆ erase()

template<typename T >
void cmagic::set< T >::erase ( const value_type val)
inline

Removes a single element from the set.

Parameters
valvalue to be removed from the set. Function does nothing if the element doesn't exist in the set.

◆ find()

template<typename T >
iterator cmagic::set< T >::find ( const value_type val) const
inline

Searches the container for an element equivalent to val and returns an iterator to it if found, otherwise it returns set::end.

Parameters
valvalue to be searched for
Returns
an iterator to the element, if val is found, or set::end otherwise

◆ insert() [1/2]

template<typename T >
std::pair<iterator, bool> cmagic::set< T >::insert ( const value_type val)
inline

Inserts a new element to the set if it is not equivalent to any element already contained in the set.

Because elements in a set are unique, the insertion operation checks whether an inserted element is equivalent to an element already in the container, and if so, the element is not inserted, returning an iterator to this existing element.

Parameters
valvalue to be copied (or moved) to the set
Returns
a pair, with its member pair::first set to an iterator pointing to either the newly inserted element or to the equivalent element already in the set or end if allocation of the new element has failed. The pair::second element in the pair is set to true if a new element was inserted or false if an equivalent element already existed (or could not be inserted due to allocation failure).

◆ insert() [2/2]

template<typename T >
std::pair<iterator, bool> cmagic::set< T >::insert ( value_type &&  val)
inline

Inserts a new element to the set if it is not equivalent to any element already contained in the set.

Because elements in a set are unique, the insertion operation checks whether an inserted element is equivalent to an element already in the container, and if so, the element is not inserted, returning an iterator to this existing element.

Parameters
valvalue to be copied (or moved) to the set
Returns
a pair, with its member pair::first set to an iterator pointing to either the newly inserted element or to the equivalent element already in the set or end if allocation of the new element has failed. The pair::second element in the pair is set to true if a new element was inserted or false if an equivalent element already existed (or could not be inserted due to allocation failure).

◆ operator bool()

template<typename T >
cmagic::set< T >::operator bool ( ) const
inline

Checks if the set is properly initialized.

Example usage:

if (set) {
set.insert(123);
} else {
std::cerr << "Set allocation failed!\n";
}
Returns
true if set is initialized, false if set allocation has failed and no operation should be made on it

◆ size()

template<typename T >
size_type cmagic::set< T >::size ( ) const
inline

Returns the number of elements in the set.

Returns
number of elements in the set

The documentation for this class was generated from the following file: