/****************************************************************************
 *   This file is part of the aGrUM/pyAgrum library.                        *
 *                                                                          *
 *   Copyright (c) 2005-2026 by                                             *
 *       - Pierre-Henri WUILLEMIN(_at_LIP6)                                 *
 *       - Christophe GONZALES(_at_AMU)                                     *
 *                                                                          *
 *   The aGrUM/pyAgrum library is free software; you can redistribute it    *
 *   and/or modify it under the terms of either :                           *
 *                                                                          *
 *    - the GNU Lesser General Public License as published by               *
 *      the Free Software Foundation, either version 3 of the License,      *
 *      or (at your option) any later version,                              *
 *    - the MIT license (MIT),                                              *
 *    - or both in dual license, as here.                                   *
 *                                                                          *
 *   (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html)    *
 *                                                                          *
 *   This aGrUM/pyAgrum library is distributed in the hope that it will be  *
 *   useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,          *
 *   INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or 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.                                        *
 *                                                                          *
 *   See LICENCES for more details.                                         *
 *                                                                          *
 *   SPDX-FileCopyrightText: Copyright 2005-2026                            *
 *       - Pierre-Henri WUILLEMIN(_at_LIP6)                                 *
 *       - Christophe GONZALES(_at_AMU)                                     *
 *   SPDX-License-Identifier: LGPL-3.0-or-later OR MIT                      *
 *                                                                          *
 *   Contact  : info_at_agrum_dot_org                                       *
 *   homepage : http://agrum.gitlab.io                                      *
 *   gitlab   : https://gitlab.com/agrumery/agrum                           *
 *                                                                          *
 ****************************************************************************/


/** @file
 * @brief Source implementation of classes for directed edge sets
 *
 * @author Pierre-Henri WUILLEMIN(_at_LIP6) & Christophe GONZALES(_at_AMU)
 *
 */
#include <agrum/base/graphs/parts/arcGraphPart.h>

#ifdef GUM_NO_INLINE
#  include <agrum/base/graphs/parts/arcGraphPart_inl.h>
#endif   // GU%_NO_INLINE
#include "agrum/base/graphs/graphElements.h"

namespace gum {

  ///////////////////// ArcGraphPart
  ArcGraphPart::ArcGraphPart(Size arcs_size, bool arcs_resize_policy) :
      _arcs_(arcs_size, arcs_resize_policy) {
    GUM_CONSTRUCTOR(ArcGraphPart);
  }

  ArcGraphPart::ArcGraphPart(ArcGraphPart&& s) :
      _arcs_(std::move(s._arcs_)), _parents_(std::move(s._parents_)),
      _children_(std::move(s._children_)) {
    GUM_CONS_MOV(ArcGraphPart);
  }

  ArcGraphPart::ArcGraphPart(const ArcGraphPart& s) : _arcs_(s._arcs_) {
    GUM_CONS_CPY(ArcGraphPart);

    // copy the sets of parents
    const NodeProperty< NodeSet* >& pars = s._parents_;
    _parents_.resize(pars.capacity());

    for (const auto& [key, nodeset]: pars) {
      NodeSet* newpar = new NodeSet(*nodeset);
      _parents_.insert(key, newpar);
    }

    // copy the sets of children
    const NodeProperty< NodeSet* >& children = s._children_;
    _children_.resize(children.capacity());

    for (const auto& [key, nodeset]: children) {
      NodeSet* newchildren = new NodeSet(*nodeset);
      _children_.insert(key, newchildren);
    }

    // send signals to indicate that there are new arcs
    if (onArcAdded.hasListener()) {
      for (const auto& arc: _arcs_) {
        GUM_EMIT2(onArcAdded, arc.tail(), arc.head());
      }
    }
  }

  ArcGraphPart::~ArcGraphPart() {
    GUM_DESTRUCTOR(ArcGraphPart);
    // be sure to deallocate all the parents and children sets
    clearArcs();
  }

  void ArcGraphPart::clearArcs() {
    for (const auto& elt: _parents_)
      delete elt.second;

    _parents_.clear();

    for (const auto& elt: _children_)
      delete elt.second;

    _children_.clear();

    // we need this copy only if at least one onArcDeleted listener exists
    if (onArcDeleted.hasListener()) {
      ArcSet tmp = _arcs_;
      _arcs_.clear();

      for (const auto& arc: tmp)
        GUM_EMIT2(onArcDeleted, arc.tail(), arc.head());
    } else {
      _arcs_.clear();
    }
  }

  ArcGraphPart& ArcGraphPart::operator=(const ArcGraphPart& s) {
    // avoid self assignment
    if (this != &s) {
      // copy the arcs
      clearArcs();
      _arcs_ = s._arcs_;

      // copy the sets of parents
      _parents_.resize(s._parents_.capacity());

      for (const auto& [key, nodeset]: s._parents_) {
        NodeSet* newpar = new NodeSet(*nodeset);
        _parents_.insert(key, newpar);
      }

      // copy the sets of children
      _children_.resize(s._children_.capacity());

      for (const auto& [key, nodeset]: s._children_) {
        NodeSet* newchildren = new NodeSet(*nodeset);
        _children_.insert(key, newchildren);
      }

      if (onArcAdded.hasListener()) {
        for (const auto& arc: _arcs_) {
          GUM_EMIT2(onArcAdded, arc.tail(), arc.head());
        }
      }

      GUM_OP_CPY(ArcGraphPart);
    }

    return *this;
  }

  ArcGraphPart& ArcGraphPart::operator=(ArcGraphPart&& s) {
    if (this != &s) {
      clearArcs();
      _arcs_     = std::move(s._arcs_);
      _parents_  = std::move(s._parents_);
      _children_ = std::move(s._children_);
      if (onArcAdded.hasListener()) {
        for (const auto& arc: _arcs_) {
          GUM_EMIT2(onArcAdded, arc.tail(), arc.head());
        }
      }
      GUM_OP_MOV(ArcGraphPart);
    }
    return *this;
  }

  std::string ArcGraphPart::toString() const {
    std::stringstream s;
    bool              first = true;
    s << "{";

    for (const auto& arc: _arcs_) {
      if (first) {
        first = false;
      } else {
        s << ",";
      }

      s << arc;
    }

    s << "}";

    return s.str();
  }

  std::ostream& operator<<(std::ostream& stream, const ArcGraphPart& set) {
    stream << set.toString();
    return stream;
  }

} /* namespace gum */
