/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
    Pseudo_QC_List.qpp

    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/QoreListIterator.h"
#include "qore/intern/RangeIterator.h"
#include "qore/intern/qore_list_private.h"

extern QoreClass* QC_LISTITERATOR;
extern QoreClass* QC_RANGEITERATOR;

//! Methods in this pseudo-class can be executed on @ref list "lists"
/**
 */
qclass <list> [arg=const QoreListNode* l = v.get<const QoreListNode>();vparent=<value>];

//! Returns Qore::NT_LIST
/**
    @return Qore::NT_LIST

    @par Example:
    @code{.py}
switch (l.typeCode()) {
    case NT_LIST:
        printf("%y: is a list\n", l);
        break;
}
    @endcode
*/
int <list>::typeCode() [flags=CONSTANT] {
   return NT_LIST;
}

//! returns @ref True if the list has a @ref hash_complex_type, @ref False if not
/** @par Example:
    @code{.py}
bool b = n.complexType();
    @endcode

    @return @ref True if the list has a @ref hash_complex_type, @ref False if not

    @see
    - <value>::fullType()

    @since %Qore 0.8.13
*/
bool <list>::complexType() [flags=CONSTANT] {
   const qore_list_private* lp = qore_list_private::get(*l);
   return lp->complexTypeInfo ? true : false;
}

//! Returns the number of elements in the list
/**
    @return the number of elements in the list

    @par Example:
    @code{.py}
int len = l.size();
    @endcode

    @see <list>::sizep()
*/
int <list>::size() [flags=CONSTANT] {
   return l->size();
}

//! Returns @ref True if the list is empty (size = 0), @ref False if not
/** The opposite of <list>::val()

    @return @ref True if the list is empty (size = 0), @ref False if not

    @par Example:
    @code{.py}
bool b = l.empty();
    @endcode
*/
bool <list>::empty() [flags=CONSTANT] {
   return l->empty();
}

//! Returns @ref False if the list is empty (size = 0), @ref True if not
/** The opposite of <list>::empty()

    @return @ref False if the list is empty (size = 0), @ref True if not

    @par Example:
    @code{.py}
bool b = l.val();
    @endcode

    @see
    - @ref perl-bool-eval "%perl-bool-eval"
    - @ref strict-bool-eval "%strict-bool-eval"
*/
bool <list>::val() [flags=CONSTANT] {
   return !l->empty();
}

//! Returns the first entry in the list
/** @return the first entry in the list

    @par Example:
    @code{.py}
auto e = l.first();
    @endcode

    @see <list>::last()

    @since %Qore 0.8.5
 */
auto <list>::first() [flags=CONSTANT] {
   return l->getReferencedEntry(0);
}

//! Returns the last entry in the list
/** @return the last entry in the list

    @par Example:
    @code{.py}
auto e = l.last();
    @endcode

    @see <list>::first()

    @since %Qore 0.8.5
 */
auto <list>::last() [flags=CONSTANT] {
   return l->getReferencedEntry((size_t)(l->size() - 1));
}

//! Creates a string from the list and a separator string given as an argument
/** Each element in the list will be converted to a string if necessary to be concatenated to the return value string; additionally if any string argument has a different @ref character_encoding "character encoding" than \a str, then it will be converted to <em>str</em>'s @ref character_encoding "character encoding" before concatentation to the return value string

    @param str the separator string

    @return a string created from a list and a separator string, each element in the list will be present in the return value separated by the separator string; the string returned with have the same @ref character_encoding "character encoding" as \a str

    @par Example:
    @code{.py}
string str = ("a", "b", "c").join(":"); # returns "a:b:c"
    @endcode

    @throw ENCODING-CONVERSION-ERROR this exception could be thrown if the string arguments have different @ref character_encoding "character encodings" and an error occurs during encoding conversion

    @note equivalent to join(string, list)

    @since %Qore 0.8.5
 */
string <list>::join(string str) [flags=RET_VALUE_ONLY] {
   return join_intern(str, l, 0, xsink);
}

//! Returns the number of elements in the list
/** For this type, this method is equivalent to size()

    @return the number of elements in the list

    @par Example:
    @code{.py}
printf("iterating %d element%s\n", val.lsize(), val.lsize() == 1 ? "" : "s");
foreach auto element in (val) {
    do_something(element);
}
    @endcode
*/
int <list>::lsize() [flags=CONSTANT] {
   return l->size();
}

//! Returns @ref True if the list contains arg, @ref False if it does not.
/** This call uses "soft" comparisons (where types may be converted).
    The computational complexity is <tt>O(n)</tt> (n = the length of the list).

    @param arg any value to check its presence in the list

    @return @ref True if the list contains arg, @ref False if it does not

    @par Example:
    @code{.py}
list<int> l = (1, 2, 3);
bool b = l.contains(5); # returns False
    @endcode
*/
bool <list>::contains(auto arg) [flags=RET_VALUE_ONLY] {
   return inlist_intern(arg, l, xsink);
}

//! Returns a @ref Qore::ListIterator "ListIterator" object for the list
/** @return a @ref Qore::ListIterator "ListIterator" object for the list

    @par Example:
    @code{.py}
map printf("+ %y\n", $1), l.iterator();
    @endcode

    @since %Qore 0.8.6
 */
AbstractIterator <list>::iterator() [flags=CONSTANT] {
   return new QoreObject(QC_LISTITERATOR, 0, new QoreListIterator(l));
}

//! Returns a @ref Qore::RangeIterator "RangeIterator" object for the list elements
/** @par Example:
    @code{.py}
map printf("+ %y\n", $1), l.ranegIterator();
    @endcode

    @param val an optional value to be returned instead of the default integer value

    @return a @ref Qore::RangeIterator "RangeIterator" object for the list elements

    @since
    - %Qore 0.8.8
    - %Qore 0.8.11.1 this pseudo-method takes the optional \a val argument
 */
AbstractIterator <list>::rangeIterator(auto val) [flags=CONSTANT] {
   if (l->empty())
      return new QoreObject(QC_SINGLEVALUEITERATOR, 0, new SingleValueIterator);

   ReferenceHolder<RangeIterator> r(new RangeIterator(0, l->size(), 1, val, xsink), xsink);
   if (*xsink)
      return QoreValue();

   return new QoreObject(QC_RANGEITERATOR, 0, r.release());
}

//! Returns @ref True since lists can return a non-zero size
/** @return @ref True since lists can return a non-zero size

    @see <list>::size()

    @since %Qore 0.8.9
*/
bool <list>::sizep() [flags=CONSTANT] {
   return true;
}
