Program Listing for File Cover.hpp

Return to documentation for file (include/networkit/structures/Cover.hpp)

/*
 * Cover.hpp
 *
 *  Created on: 03.10.2013
 *      Author: cls
 */

#ifndef NETWORKIT_STRUCTURES_COVER_HPP_
#define NETWORKIT_STRUCTURES_COVER_HPP_

#include <iterator>
#include <map>
#include <set>
#include <vector>

#include <networkit/Globals.hpp>
#include <networkit/structures/Partition.hpp>

namespace NetworKit {

class Cover final {

public:
    Cover();

    Cover(index z);

    Cover(const Partition &p);

    inline std::set<index> &operator[](const index &e) { return this->data[e]; }
    inline const std::set<index> &operator[](const index &e) const { return this->data[e]; }

    inline std::set<index> subsetsOf(index e) const {
        // TODO: assert (e < this->numberOfElements());
        return this->data[e];
    }

    bool contains(index e) const;

    bool inSameSubset(index e1, index e2) const;

    std::set<index> getMembers(index s) const;

    void addToSubset(index s, index e);

    void removeFromSubset(index s, index e);

    void moveToSubset(index s, index e);

    index toSingleton(index e);

    void allToSingletons();

    void mergeSubsets(index s, index t);

    index upperBound() const;

    index lowerBound() const;

    std::vector<count> subsetSizes() const;

    std::map<index, count> subsetSizeMap() const;

    count numberOfSubsets() const;

    count numberOfElements() const;

    index extend();

    std::set<index> getSubsetIds() const;

    void setUpperBound(index upper);

    template <typename Callback>
    void forEntries(Callback func) const;

    template <typename Callback>
    void parallelForEntries(Callback handle) const;

private:
    index z;
    index omega;
    std::vector<std::set<index>> data;

    inline index newSubsetId() {
        omega++;
        index s = omega;
        return s;
    }
};

template <typename Callback>
inline void Cover::forEntries(Callback handle) const {
    for (index e = 0; e <= this->z; e += 1) {
        handle(e, data[e]);
    }
}

template <typename Callback>
inline void Cover::parallelForEntries(Callback handle) const {
#pragma omp parallel for
    for (omp_index e = 0; e <= static_cast<omp_index>(this->z); e += 1) {
        handle(e, data[e]);
    }
}

} /* namespace NetworKit */

#endif // NETWORKIT_STRUCTURES_COVER_HPP_