<commit_before>#include "runtime/core/sequencetypes.h"

#include "system/globalenv.h"
#include "errors/error_factory.h"

namespace xqp
{

/*******************************************************************************

********************************************************************************/
InstanceOfIterator::InstanceOfIterator(yy::location loc,
                                       PlanIter_t& aTreatExpr,
                                       TypeSystem::xqtref_t aSequenceType,
                                       bool aMustBeInstance)
  : UnaryBaseIterator<InstanceOfIterator> ( loc, aTreatExpr ),
    theSequenceType (aSequenceType),
    mustBeInstance (aMustBeInstance)
{ }

InstanceOfIterator::~InstanceOfIterator() 
{ }


Item_t
InstanceOfIterator::nextImpl(PlanState& planState)
{
  Item_t lTreatItem;
  TypeSystem::xqtref_t lTreatType;
  TypeSystem::quantifier_t lQuantifier;
  bool lResult;

  PlanIteratorState* state;
  DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
  
  lTreatItem = consumeNext(theChild, planState);

  lQuantifier = GENV_TYPESYSTEM.quantifier(*theSequenceType);

  if (lTreatItem != 0)
  {
    if (GENV_TYPESYSTEM.is_subtype(*GENV_TYPESYSTEM.create_type(lTreatItem->getType(), 
                                    TypeSystem::QUANT_ONE), *theSequenceType)) {
      lTreatItem = consumeNext(theChild, planState);
      if (lTreatItem != 0) {
        if (lQuantifier == TypeSystem::QUANT_ONE || lQuantifier == TypeSystem::QUANT_QUESTION) {
          lResult = false;
        } else {
          lResult = true;
          do {
            if (!GENV_TYPESYSTEM.is_subtype(*GENV_TYPESYSTEM.create_type(lTreatItem->getType(), TypeSystem::QUANT_ONE), *theSequenceType)) {
              lResult = false;
            }
            lTreatItem = consumeNext(theChild, planState);
          } while (lTreatItem != 0);
        }
      } else {
        lResult = true;
      }
    } else {
      lResult = false;
    }
  }
  else
  {
    if (lQuantifier == TypeSystem::QUANT_ONE || lQuantifier == TypeSystem::QUANT_PLUS) {
      lResult = false;
    } else {
      lResult = true;
    }
  }
    
  if (mustBeInstance && ! lResult)
    ZORBA_ERROR_ALERT (ZorbaError::XPDY0050, NULL, false);
  STACK_PUSH(Zorba::getItemFactory()->createBoolean(lResult), state);
  STACK_END();
}

  

/*******************************************************************************

********************************************************************************/

CastIterator::CastIterator(
    const yy::location& loc,
    PlanIter_t& aChild,<commit_msg>Fixed EMPTY_TYPE case in instanceof<commit_after>#include "runtime/core/sequencetypes.h"

#include "system/globalenv.h"
#include "errors/error_factory.h"

namespace xqp
{

/*******************************************************************************

********************************************************************************/
InstanceOfIterator::InstanceOfIterator(yy::location loc,
                                       PlanIter_t& aTreatExpr,
                                       TypeSystem::xqtref_t aSequenceType,
                                       bool aMustBeInstance)
  : UnaryBaseIterator<InstanceOfIterator> ( loc, aTreatExpr ),
    theSequenceType (aSequenceType),
    mustBeInstance (aMustBeInstance)
{ }

InstanceOfIterator::~InstanceOfIterator() 
{ }


Item_t
InstanceOfIterator::nextImpl(PlanState& planState)
{
  Item_t lTreatItem;
  TypeSystem::xqtref_t lTreatType;
  TypeSystem::quantifier_t lQuantifier;
  bool lResult;

  PlanIteratorState* state;
  DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
  
  lTreatItem = consumeNext(theChild, planState);

  lQuantifier = GENV_TYPESYSTEM.quantifier(*theSequenceType);

  if (lTreatItem != 0)
  {
    if (GENV_TYPESYSTEM.is_subtype(*GENV_TYPESYSTEM.create_type(lTreatItem->getType(), 
                                    TypeSystem::QUANT_ONE), *theSequenceType)) {
      lTreatItem = consumeNext(theChild, planState);
      if (lTreatItem != 0) {
        if (lQuantifier == TypeSystem::QUANT_ONE || lQuantifier == TypeSystem::QUANT_QUESTION) {
          lResult = false;
        } else {
          lResult = true;
          do {
            if (!GENV_TYPESYSTEM.is_subtype(*GENV_TYPESYSTEM.create_type(lTreatItem->getType(), TypeSystem::QUANT_ONE), *theSequenceType)) {
              lResult = false;
            }
            lTreatItem = consumeNext(theChild, planState);
          } while (lTreatItem != 0);
        }
      } else {
        lResult = true;
      }
    } else {
      lResult = false;
    }
  }
  else
  {
    if ((lQuantifier == TypeSystem::QUANT_ONE
        || lQuantifier == TypeSystem::QUANT_PLUS)
        && !GENV_TYPESYSTEM.is_equal(*GENV_TYPESYSTEM.EMPTY_TYPE, *theSequenceType)) {
      lResult = false;
    } else {
      lResult = true;
    }
  }
    
  if (mustBeInstance && ! lResult)
    ZORBA_ERROR_ALERT (ZorbaError::XPDY0050, NULL, false);
  STACK_PUSH(Zorba::getItemFactory()->createBoolean(lResult), state);
  STACK_END();
}

  

/*******************************************************************************

********************************************************************************/

CastIterator::CastIterator(
    const yy::location& loc,
    PlanIter_t& aChild,
{'fraction_non_alphanumeric': 0.21633888048411498, 'fraction_numerical': 0.00264750378214826, 'mean_word_length': 3.2042925278219396, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3610283', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1532, 'n_words': 233}