/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
    vector_set

    An unordered set-type class implemented with std::vector for very fast
    iteration and efficient memory storage for small(ish) data sets

    ex: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0429r1.pdf

    Qore Programming Language

    Copyright (C) 2003 - 2023 Qore Technologies, s.r.o.

    constants can only be defined when parsing
    constants values will be substituted during the 2nd parse phase

    reads and writes are (must be) wrapped under the program-level parse lock

    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.

    Note that the Qore library is released under a choice of three open-source
    licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
    information.
*/

#ifndef _QORE_VECTOR_SET_H

#define _QORE_VECTOR_SET_H

#include <vector>
#include <algorithm>
#include <iterator>

struct string_compare {
    DLLLOCAL explicit string_compare(const char* const& baseline) : baseline(baseline) {
    }

    DLLLOCAL bool operator() (const char* const& arg) {
        return !strcmp(arg, baseline);
    }

    const char* const baseline;
};

template <typename T>
class vector_set_t {
public:
    typedef T value_type;

private:
    typedef std::vector<value_type> vector_t;

public:
    typedef typename vector_t::iterator iterator;
    typedef typename vector_t::const_iterator const_iterator;

    DLLLOCAL vector_set_t() {
    }

    DLLLOCAL vector_set_t(const T& v) {
        insert(v);
    }

    DLLLOCAL iterator find(const T& v) {
        return std::find(vector.begin(), vector.end(), v);
    }

    DLLLOCAL const_iterator find(const T& v) const {
        return std::find(vector.begin(), vector.end(), v);
    }

    // same as find, just for map compatibility
    DLLLOCAL iterator lower_bound(const T& v) {
        return std::find(vector.begin(), vector.end(), v);
    }

    // same as find, just for map compatibility
    DLLLOCAL const_iterator lower_bound(const T& v) const {
        return std::find(vector.begin(), vector.end(), v);
    }

    DLLLOCAL iterator begin() {
        return vector.begin();
    }

    DLLLOCAL const_iterator begin() const {
        return vector.begin();
    }

    DLLLOCAL iterator end() {
        return vector.end();
    }

    DLLLOCAL const_iterator end() const {
        return vector.end();
    }

    DLLLOCAL bool empty() const {
        return vector.empty();
    }

    DLLLOCAL void clear() {
        vector.clear();
    }

    DLLLOCAL size_t size() const {
        return vector.size();
    }

    DLLLOCAL void erase(const T& v) {
        iterator i = find(v);
        if (i != vector.end())
            vector.erase(i);
    }

    DLLLOCAL void erase(iterator i) {
        vector.erase(i);
    }

    // iterator arg ignored
    DLLLOCAL iterator insert(iterator i, const T& v) {
        bool created;
        return find_create(v, created);
    }

    DLLLOCAL std::pair<iterator, bool> insert(const T& v) {
        bool created;
        iterator i = find_create(v, created);
        return std::pair<iterator, bool>(i, created);
    }

private:
    DLLLOCAL iterator find_create(const T& v, bool& created) {
        iterator i = find(v);
        if (i == vector.end()) {
            i = vector.insert(vector.end(), v);
            created = true;
        }
        else {
            created = false;
        }
        return i;
    }

    vector_t vector;
};

#endif