<commit_before>// Copyright 2016 Annotator Team

#define Annotator_AnnotatorLib_Object_BODY

/************************************************************
 Object class body
 ************************************************************/

#include "AnnotatorLib/Object.h"
#include <assert.h>
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include "AnnotatorLib/Annotation.h"
#include "AnnotatorLib/Class.h"

// Derived includes directives

namespace AnnotatorLib {

// statics
static unsigned long lastId = 100;
static unsigned long namePostfixNmb = 0;

Object::Object() : Object(genId(), shared_ptr<Class>(nullptr)) {
  name = genName();
}

Object::Object(const shared_ptr<Class> c) : Object(genId(), c) {}

Object::Object(unsigned long id, const shared_ptr<Class> c) : id(id) {
  objectClass = c;
  name = genName();
  if (lastId < id)
    lastId = id;  // avoid collisions when loading objects from file
}

unsigned long Object::genId() {
  lastId += 3;
  return lastId;
}

unsigned long Object::getId() const { return id; }

std::string Object::getName() const { return name; }

void Object::setName(std::string name) {
  // avoid postfix collisions when setting a name
  std::string name_postfix = name.substr(name.find('_') + 1);
  if (!name_postfix.empty()) {
    unsigned long nmb;
    try {
      nmb = boost::lexical_cast<unsigned long>(name_postfix);
    } catch (boost::bad_lexical_cast &e) {
    }
    namePostfixNmb = std::max(nmb, namePostfixNmb);
  }
  this->name = name;
}

std::string Object::genName() {
  std::string prefix = "unnamed_obj_";
  const shared_ptr<Class> c = getClass();
  if (c) prefix = c->getName() + "_";
  return prefix + std::to_string(++namePostfixNmb);
}

shared_ptr<Class> Object::getClass() const { return objectClass; }

void Object::setClass(shared_ptr<Class> c) { objectClass = c; }

std::vector<shared_ptr<Attribute>> Object::getAttributes() const {
  return attributes;
}

bool Object::addAttribute(shared_ptr<Attribute> attribute) {
  if (attribute != nullptr &&
      std::find(attributes.begin(), attributes.end(), attribute) ==
          attributes.end()) {
    attributes.push_back(attribute);
    return true;
  }
  return false;
}

bool Object::removeAttribute(shared_ptr<Attribute> attribute) {
  std::vector<shared_ptr<Attribute>>::const_iterator position =
      std::find(attributes.begin(), attributes.end(), attribute);
  if (position != attributes.end()) {
    attributes.erase(position);
    return true;
  }
  return false;
}

shared_ptr<Annotation> Object::getFirstAnnotation() const {
  if (annotations.size() == 0) return nullptr;
  // anntations are sorted by frame-number, thus we directly return the first
  return annotations.begin()->second.lock();
}
<commit_msg>circumvent bug in gcc-4.8 (with c++11)<commit_after>// Copyright 2016 Annotator Team

#define Annotator_AnnotatorLib_Object_BODY

/************************************************************
 Object class body
 ************************************************************/

#include "AnnotatorLib/Object.h"
#include <assert.h>
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include "AnnotatorLib/Annotation.h"
#include "AnnotatorLib/Class.h"

// Derived includes directives

namespace AnnotatorLib {

// statics
static unsigned long lastId = 100;
static unsigned long namePostfixNmb = 0;

Object::Object() : Object(genId(), shared_ptr<Class>(nullptr)) {
  name = genName();
}

Object::Object(const shared_ptr<Class> c) : Object(genId(), c) {}

Object::Object(unsigned long id, const shared_ptr<Class> c) : id(id) {
  objectClass = c;
  name = genName();
  if (lastId < id)
    lastId = id;  // avoid collisions when loading objects from file
}

unsigned long Object::genId() {
  lastId += 3;
  return lastId;
}

unsigned long Object::getId() const { return id; }

std::string Object::getName() const { return name; }

void Object::setName(std::string name) {
  // avoid postfix collisions when setting a name
  std::string name_postfix = name.substr(name.find('_') + 1);
  if (!name_postfix.empty()) {
    unsigned long nmb;
    try {
      nmb = boost::lexical_cast<unsigned long>(name_postfix);
    } catch (boost::bad_lexical_cast &e) {
    }
    namePostfixNmb = std::max(nmb, namePostfixNmb);
  }
  this->name = name;
}

std::string Object::genName() {
  std::string prefix = "unnamed_obj_";
  const shared_ptr<Class> c = getClass();
  if (c) prefix = c->getName() + "_";
  return prefix + std::to_string(++namePostfixNmb);
}

shared_ptr<Class> Object::getClass() const { return objectClass; }

void Object::setClass(shared_ptr<Class> c) { objectClass = c; }

std::vector<shared_ptr<Attribute>> Object::getAttributes() const {
  return attributes;
}

bool Object::addAttribute(shared_ptr<Attribute> attribute) {
  if (attribute != nullptr &&
      std::find(attributes.begin(), attributes.end(), attribute) ==
          attributes.end()) {
    attributes.push_back(attribute);
    return true;
  }
  return false;
}

bool Object::removeAttribute(shared_ptr<Attribute> attribute) {
  std::vector<shared_ptr<Attribute>>::iterator position =
      std::find(attributes.begin(), attributes.end(), attribute);
  if (position != attributes.end()) {
    attributes.erase(position);
    return true;
  }
  return false;
}

shared_ptr<Annotation> Object::getFirstAnnotation() const {
  if (annotations.size() == 0) return nullptr;
  // anntations are sorted by frame-number, thus we directly return the first
  return annotations.begin()->second.lock();
}

{'fraction_non_alphanumeric': 0.1801998183469573, 'fraction_numerical': 0.004722979109900091, 'mean_word_length': 4.920430107526882, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 43, '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': '4212821', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1697, 'n_words': 411}