<commit_before>/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * This file is part of the libetonyek project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef IWORKCONTAINERCONTEXT_H_INCLUDED
#define IWORKCONTAINERCONTEXT_H_INCLUDED

#include <deque>

#include <boost/optional.hpp>

#include "IWORKTypes_fwd.h"
#include "IWORKRefContext.h"
#include "IWORKXMLContextBase.h"

namespace libetonyek
{

template<typename Type, class NestedParser, template<typename T, class C> class Collector, unsigned Id, unsigned RefId = 0>
class IWORKContainerContext : public IWORKXMLElementContextBase
{
  typedef boost::unordered_map<ID_t, Type> Dict_t;

public:
  IWORKContainerContext(IWORKXMLParserState &state, std::deque<Type> &elements)
    : IWORKXMLElementContextBase(state)
    , m_dict(0)
    , m_elements(elements)
    , m_collector(elements)
  {
  }

  IWORKContainerContext(IWORKXMLParserState &state, Dict_t &dict, std::deque<Type> &elements)
    : IWORKXMLElementContextBase(state)
    , m_dict(&dict)
    , m_elements(elements)
    , m_collector(elements)
  {
  }

protected:
  virtual IWORKXMLContextPtr_t element(const int name)
  {
    if (m_ref)
      handleRef();
    else if (m_collector.pending())
      m_collector.push();

    if (name == Id)
      return m_collector.makeContext<NestedParser>(getState());
    else if ((RefId != 0) && (name == RefId))
      return makeContext<IWORKRefContext>(getState(), m_ref);
    return IWORKXMLContextPtr_t();
  }

  virtual void endOfElement()
  {
    if (m_ref)
      handleRef();
    else if (m_collector.pending())
      m_collector.push();
  }

private:
  void handleRef()
  {
    assert(m_dict);

    const typename Dict_t::const_iterator it = m_dict->find(get(m_ref));
    if (it == m_dict->end())
      m_elements.push_back(Type());
    else
      m_elements.push_back(it->second);
  }<commit_msg>avoid segfault<commit_after>/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * This file is part of the libetonyek project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef IWORKCONTAINERCONTEXT_H_INCLUDED
#define IWORKCONTAINERCONTEXT_H_INCLUDED

#include <cassert>
#include <deque>

#include <boost/optional.hpp>

#include "IWORKTypes_fwd.h"
#include "IWORKRefContext.h"
#include "IWORKXMLContextBase.h"

namespace libetonyek
{

template<typename Type, class NestedParser, template<typename T, class C> class Collector, unsigned Id, unsigned RefId = 0>
class IWORKContainerContext : public IWORKXMLElementContextBase
{
  typedef boost::unordered_map<ID_t, Type> Dict_t;

public:
  IWORKContainerContext(IWORKXMLParserState &state, std::deque<Type> &elements)
    : IWORKXMLElementContextBase(state)
    , m_dict(0)
    , m_elements(elements)
    , m_collector(elements)
  {
  }

  IWORKContainerContext(IWORKXMLParserState &state, Dict_t &dict, std::deque<Type> &elements)
    : IWORKXMLElementContextBase(state)
    , m_dict(&dict)
    , m_elements(elements)
    , m_collector(elements)
  {
  }

protected:
  virtual IWORKXMLContextPtr_t element(const int name)
  {
    if (m_ref && m_dict)
      handleRef();
    else if (m_collector.pending())
      m_collector.push();

    if (name == Id)
      return m_collector.makeContext<NestedParser>(getState());
    else if ((RefId != 0) && (name == RefId))
      return makeContext<IWORKRefContext>(getState(), m_ref);
    return IWORKXMLContextPtr_t();
  }

  virtual void endOfElement()
  {
    if (m_ref && m_dict)
      handleRef();
    else if (m_collector.pending())
      m_collector.push();
  }

private:
  void handleRef()
  {
    assert(m_dict);

    const typename Dict_t::const_iterator it = m_dict->find(get(m_ref));
    if (it == m_dict->end())
      m_elements.push_back(Type());
    else
      m_elements.push_back(it->second);
  }
{'fraction_non_alphanumeric': 0.12283653846153846, 'fraction_numerical': 0.004326923076923077, 'mean_word_length': 3.9653937947494033, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1775241', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1436, 'n_words': 331}