/* -*- mode: c++; indent-tabs-mode: nil -*- */
/** @file QC_HashIterator.qpp HashIterator 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"

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

    @par Example: HashIterator basic usage
    @code{.py}
    hash<auto> h = {"key1": 1, "key2": 2,};

    HashIterator it(h);
    while (it.next()) {
        printf("getKey: %n; getKeyValue: %n; getValue: %n; getValuePair: %n\n",
               it.getKey(), it.getKeyValue(), it.getValue(), it.getValuePair());
    }

    getKey: "key1"; getKeyValue: 1; getValue: 1; getValuePair: hash: (key : "key1", value : 1)
    getKey: "key2"; getKeyValue: 2; getValue: 2; getValuePair: hash: (key : "key2", value : 2)
    @endcode

    @note
    - In general, the HashIterator 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 HashReverseIterator
 */
qclass HashIterator [arg=QoreHashIterator* i; ns=Qore; vparent=AbstractQuantifiedBidirectionalIterator];

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

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

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

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

//! Moves the current position to the next element in the hash; returns @ref False if there are no more elements; if the iterator is not pointing at a valid element before this call, the iterator will be positioned on the first element in the hash if the hash is not empty
/** This method will return @ref True again after it returns @ref False once if hash is not empty, otherwise it will always return @ref False.
    The iterator object should not be used after this method returns @ref False

    @return @ref False if there are no more elements in the hash (in which case the iterator object is invalid and should not be used); @ref True if successful (meaning that the iterator object is valid)

    @par Example:
    @code{.py}
while (i.next()) {
    printf(" + %y = %y\n", i.getKey(), i.getValue());
}
    @endcode

    @throw ITERATOR-THREAD-ERROR this exception is thrown if this method is called from any thread other than the thread that created the object
 */
bool HashIterator::next() {
   if (i->check(xsink))
      return false;
   return i->next();
}

//! Moves the current position to the previous element in the hash; returns @ref False if there are no more elements; if the iterator is not pointing at a valid element before this call, the iterator will be positioned on the last element in the hash if the hash is not empty
/** This method will return @ref True again after it returns @ref False once if the hash is not empty, otherwise it will always return @ref False.
    The iterator object should not be used after this method returns @ref False

    @return @ref False if there are no more elements in the hash (in which case the iterator object is invalid and should not be used); @ref True if successful (meaning that the iterator object is valid)

    @par Example:
    @code{.py}
while (i.prev()) {
    printf(" + %y = %y\n", i.getKey(), i.getValue());
}
    @endcode

    @throw ITERATOR-THREAD-ERROR this exception is thrown if this method is called from any thread other than the thread that created the object
 */
bool HashIterator::prev() {
   if (i->check(xsink))
      return false;
   return i->prev();
}

//! returns @ref True if the hash is empty; @ref False if not
/** @return @ref True if the hash is empty; @ref False if not

    @par Example:
    @code{.py}
if (i.empty())
    printf("the hash is empty\n");
    @endcode
 */
bool HashIterator::empty() [flags=CONSTANT] {
   return i->empty();
}

//! returns @ref True if on the first element of the hash
/** @return @ref True if on the first element of the hash

    @par Example:
    @code{.py}
while (i.next()) {
    if (i.first())
        printf("START:\n");
}
    @endcode
 */
bool HashIterator::first() [flags=CONSTANT] {
   return i->first();
}

//! returns @ref True if on the last element of the hash
/** @return @ref True if on the last element of the hash

    @par Example:
    @code{.py}
while (i.next()) {
    if (i.last())
        printf("END.\n");
}
    @endcode
 */
bool HashIterator::last() [flags=CONSTANT] {
   return i->last();
}

//! returns the current key value or throws an \c INVALID-ITERATOR exception if the iterator is invalid
/** The current hash key can be returned with @ref getKey().

    @return the current key value or throws an \c INVALID-ITERATOR exception if the iterator is invalid

    @par Example:
    @code{.py}
while (i.next()) {
    printf("+ %y\n", i.getValue());
}
    @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
 */
auto HashIterator::getValue() [flags=RET_VALUE_ONLY] {
   if (i->check(xsink))
      return QoreValue();
   return i->getReferencedValue(xsink);
}

//! returns the current value of the current hash key being iterated or throws an \c INVALID-ITERATOR exception if the iterator is invalid
/** @return the current value of the current hash key being iterated or throws an \c INVALID-ITERATOR exception if the iterator is invalid

    @par Example:
    @code{.py}
while (i.next()) {
    printf("+ %y\n", i.getKeyValue());
}
    @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
 */
auto HashIterator::getKeyValue() [flags=RET_VALUE_ONLY] {
   if (i->check(xsink))
      return QoreValue();
   return i->getReferencedKeyValue(xsink);
}

//! 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}
while (i.next()) {
    printf("+ %y\n", i.getValuePair());
}
    @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
 */
hash<auto> HashIterator::getValuePair() [flags=RET_VALUE_ONLY] {
   if (i->check(xsink))
      return QoreValue();
   return i->getReferencedValuePair(xsink);
}

//! returns the current key value or throws an \c INVALID-ITERATOR exception if the iterator is invalid
/** @return the current key value or throws an \c INVALID-ITERATOR exception if the iterator is invalid

    @par Example:
    @code{.py}
while (i.next()) {
    printf("+ %y\n", i.getKey());
}
    @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
 */
string HashIterator::getKey() [flags=RET_VALUE_ONLY] {
   if (i->check(xsink))
      return QoreValue();
   return i->getKey(xsink);
}

//! returns @ref True "True" if the iterator is currently pointing at a valid element, @ref False "False" if not
/** @return @ref True "True" if the iterator is currently pointing at a valid element, @ref False "False" if not

    @par Example:
    @code{.py}
if (i.valid())
    printf("current value: %y\n", i.getValue());
    @endcode
 */
bool HashIterator::valid() [flags=CONSTANT] {
   return i->valid();
}

//! Reset the iterator instance to its initial state
/** Reset the iterator instance to its initial state

   @par Example
   @code{.py}
i.reset();
   @endcode

    @throw ITERATOR-THREAD-ERROR this exception is thrown if this method is called from any thread other than the thread that created the object
 */
HashIterator::reset() {
   if (i->check(xsink))
       return QoreValue();
   i->reset();
}
