<commit_before>/*
 * Copyright 2009-2018 The VOTCA Development Team (http://www.votca.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <iostream>
#include <unordered_map>
#include <votca/csg/beadstructure.h>
#include <votca/tools/graph_bf_visitor.h>
#include <votca/tools/graphalgorithm.h>
#include <votca/tools/graphdistvisitor.h>

using namespace votca::csg;
using namespace votca::tools;
using namespace std;

/**********************
 * Internal Functions *
 **********************/

shared_ptr<GraphNode> BaseBeadToGraphNode(BaseBead *basebead) {
  unordered_map<string, double> attributes1;
  unordered_map<string, string> attributes2;

  attributes1["Mass"] = basebead->getMass();
  attributes1["Charge"] = basebead->getQ();
  attributes2["Name"] = basebead->getName();

  /// Add graphnodes
  GraphNode graphnode;
  graphnode.setDouble(attributes1);
  graphnode.setStr(attributes2);

  return make_shared<GraphNode>(graphnode);
}

/***************************
 * Public Facing Functions *
 ***************************/

void BeadStructure::AddBead(BaseBead *bead) {
  if (beads_.count(bead->getId())) {
    string err = "Cannot add bead with Id ";
    err += to_string(bead->getId());
    err += " because each bead must have a unique Id and a bead with that Id ";
    err += "already exists within the beadstructure";
    throw invalid_argument(err);
  }
  auto numberOfBeads = beads_.size();
  beads_[bead->getId()] = bead;
  if (numberOfBeads != beads_.size()) {
    graphUpToDate = false;
    structureIdUpToDate = false;
  }
}

void BeadStructure::ConnectBeads(int bead1_id, int bead2_id) {
  if (!(beads_.count(bead1_id)) || !(beads_.count(bead2_id))) {
    string err = "Cannot connect beads in bead structure that do not exist";
    throw invalid_argument(err);
  }
  if (bead1_id == bead2_id) {
    string err = "Beads cannot be self-connected";
    throw invalid_argument(err);
  }
  auto numberOfConnections = connections_.size();
  connections_.insert(Edge(bead1_id, bead2_id));
  if (numberOfConnections != connections_.size()) {
    graphUpToDate = false;
    structureIdUpToDate = false;
  }
}

void BeadStructure::InitializeGraph_() {
  cerr << "Graph up to Date " << graphUpToDate << endl;
  if (!graphUpToDate) {
    vector<Edge> connections_vector;
    for (auto edge : connections_) {
      connections_vector.push_back(edge);
    }

    unordered_map<int, GraphNode> graphnodes;
    for (auto id_bead_ptr_pair : beads_) {
      graphnodes_[id_bead_ptr_pair.first] =
          BaseBeadToGraphNode(id_bead_ptr_pair.second);
      graphnodes[id_bead_ptr_pair.first] =
          *(graphnodes_[id_bead_ptr_pair.first]);
    }
    graph_ = make_shared<Graph>(Graph(connections_vector, graphnodes));
    graphUpToDate = true;
  }
}

void BeadStructure::CalculateStructure_() {

  InitializeGraph_();
  if (!structureIdUpToDate) {
    findStructureId<GraphDistVisitor>(*graph_);
    structureIdUpToDate = false;
  }
}

bool BeadStructure::isSingleMolecule() {

  InitializeGraph_();
  auto vertices = graph_->getVertices();
  if (vertices.size() == 0)
    return false;<commit_msg>Fixed bool<commit_after>/*
 * Copyright 2009-2018 The VOTCA Development Team (http://www.votca.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <iostream>
#include <unordered_map>
#include <votca/csg/beadstructure.h>
#include <votca/tools/graph_bf_visitor.h>
#include <votca/tools/graphalgorithm.h>
#include <votca/tools/graphdistvisitor.h>

using namespace votca::csg;
using namespace votca::tools;
using namespace std;

/**********************
 * Internal Functions *
 **********************/

shared_ptr<GraphNode> BaseBeadToGraphNode(BaseBead *basebead) {
  unordered_map<string, double> attributes1;
  unordered_map<string, string> attributes2;

  attributes1["Mass"] = basebead->getMass();
  attributes1["Charge"] = basebead->getQ();
  attributes2["Name"] = basebead->getName();

  /// Add graphnodes
  GraphNode graphnode;
  graphnode.setDouble(attributes1);
  graphnode.setStr(attributes2);

  return make_shared<GraphNode>(graphnode);
}

/***************************
 * Public Facing Functions *
 ***************************/

void BeadStructure::AddBead(BaseBead *bead) {
  if (beads_.count(bead->getId())) {
    string err = "Cannot add bead with Id ";
    err += to_string(bead->getId());
    err += " because each bead must have a unique Id and a bead with that Id ";
    err += "already exists within the beadstructure";
    throw invalid_argument(err);
  }
  auto numberOfBeads = beads_.size();
  beads_[bead->getId()] = bead;
  if (numberOfBeads != beads_.size()) {
    graphUpToDate = false;
    structureIdUpToDate = false;
  }
}

void BeadStructure::ConnectBeads(int bead1_id, int bead2_id) {
  if (!(beads_.count(bead1_id)) || !(beads_.count(bead2_id))) {
    string err = "Cannot connect beads in bead structure that do not exist";
    throw invalid_argument(err);
  }
  if (bead1_id == bead2_id) {
    string err = "Beads cannot be self-connected";
    throw invalid_argument(err);
  }
  auto numberOfConnections = connections_.size();
  connections_.insert(Edge(bead1_id, bead2_id));
  if (numberOfConnections != connections_.size()) {
    graphUpToDate = false;
    structureIdUpToDate = false;
  }
}

void BeadStructure::InitializeGraph_() {
  cerr << "Graph up to Date " << graphUpToDate << endl;
  if (!graphUpToDate) {
    vector<Edge> connections_vector;
    for (auto edge : connections_) {
      connections_vector.push_back(edge);
    }

    unordered_map<int, GraphNode> graphnodes;
    for (auto id_bead_ptr_pair : beads_) {
      graphnodes_[id_bead_ptr_pair.first] =
          BaseBeadToGraphNode(id_bead_ptr_pair.second);
      graphnodes[id_bead_ptr_pair.first] =
          *(graphnodes_[id_bead_ptr_pair.first]);
    }
    graph_ = make_shared<Graph>(Graph(connections_vector, graphnodes));
    graphUpToDate = true;
  }
}

void BeadStructure::CalculateStructure_() {

  InitializeGraph_();
  if (!structureIdUpToDate) {
    findStructureId<GraphDistVisitor>(*graph_);
    structureIdUpToDate = true;
  }
}

bool BeadStructure::isSingleMolecule() {

  InitializeGraph_();
  auto vertices = graph_->getVertices();
  if (vertices.size() == 0)
    return false;
{'fraction_non_alphanumeric': 0.1415977961432507, 'fraction_numerical': 0.007713498622589532, 'mean_word_length': 4.402529761904762, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6483097', 'n_tokens_mistral': 2350, 'n_tokens_neox': 2292, 'n_words': 570}