/* -*- mode: c++; indent-tabs-mode: nil -*- */
/** @file QC_HashPairIterator.qpp HashPairIterator class definition */
/*
    Qore Programming Language

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

    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.
*/

#include <qore/Qore.h>
#include "qore/intern/QoreHashIterator.h"

extern QoreClass* QC_HASHITERATOR;

//! This class an iterator class for hashes
/** Call HashPairIterator::next() to iterate through the hash; do not use the iterator if HashPairIterator::next() returns @ref False.
    A hash can be iterated in reverse order by calling HashPairIterator::prev() instead of HashPairIterator::next()

    @par Example: HashPairIterator basic usage
    @code{.py}
    hash<auto> data = {
        "key1" : 1,
        "key2" : 2,
        "key3" : 3,
    };

    HashPairIterator it(data);
    while (it.next()) {
        printf("iter: %n\n", it.getValue());
    }

    iter: hash: (key : "key1", value : 1)
    iter: hash: (key : "key2", value : 2)
    iter: hash: (key : "key3", value : 3)
    @endcode

    @note
    - In general, the HashPairIterator class is not designed to be accessed from multiple threads; it was created without locking for
      fast and efficient use when used from a single thread.  For methods that would be unsafe to use in another thread, any use of
      such methods in threads other than the thread where the constructor was called will cause an \c ITERATOR-THREAD-ERROR to be thrown.

    @see HashPairReverseIterator
 */
qclass HashPairIterator [arg=QoreHashIterator* i; ns=Qore; vparent=HashIterator];

//! Creates the hash iterator object
/** @param h the hash to iterate

    @par Example:
    @code{.py}
HashPairIterator hi(h);
    @endcode
 */
HashPairIterator::constructor(hash<auto> h) {
   self->setPrivate(CID_HASHPAIRITERATOR, new QoreHashIterator(h));
}

//! Creates an empty hash iterator object
/** @par Example:
    @code{.py}
*hash<auto> h = get_hash_or_nothing();
HashPairIterator hi(h);
    @endcode
 */
HashPairIterator::constructor() {
   self->setPrivate(CID_HASHPAIRITERATOR, new QoreHashIterator);
}

//! Creates a copy of the HashPairIterator object, iterating the same object as the original and in the same position
/** @par Example:
    @code{.py}
HashPairIterator ni = i.copy();
    @endcode
 */
HashPairIterator::copy() {
   self->setPrivate(CID_HASHPAIRITERATOR, new QoreHashIterator(*i));
}

//! returns a hash with the current key and value (a hash with 2 keys: \c "key" and \c "value") or throws an \c INVALID-ITERATOR exception if the iterator is invalid
/** @return a hash with the current key and value (a hash with 2 keys: \c "key" and \c "value") or throws an \c INVALID-ITERATOR exception if the iterator is invalid

    @par Example:
    @code{.py}
map printf("%s: %y\n", $1.key, $1.value), hash.pairIterator();
    @endcode

    @throw INVALID-ITERATOR the iterator is not pointing at a valid element
    @throw ITERATOR-THREAD-ERROR this exception is thrown if this method is called from any thread other than the thread that created the object

    @since %Qore 0.8.6.2
 */
hash<auto> HashPairIterator::getValue() [flags=RET_VALUE_ONLY] {
    if (i->check(xsink))
        return QoreValue();
    return i->getReferencedValuePair(xsink);
}
