<commit_before>/* ---------------------------------------------------------------------
 * Numenta Platform for Intelligent Computing (NuPIC)
 * Copyright (C) 2013, Numenta, Inc.  Unless you have an agreement
 * with Numenta, Inc., for a separate license for this software code, the
 * following terms and conditions apply:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero Public License for more details.
 *
 * You should have received a copy of the GNU Affero Public License
 * along with this program.  If not, see http://www.gnu.org/licenses.
 *
 * http://numenta.org/licenses/
 * ---------------------------------------------------------------------
 */

#include <algorithm> // sort
#include <iomanip>
#include <iostream>
#include <set>
#include <sstream>
#include <vector>

#include <assert.h>
#include <map>
#include <nupic/math/ArrayAlgo.hpp> // is_in
#include <nupic/math/StlIo.hpp>     // binary_save
#include <nupic/utils/Log.hpp>
#include <nupic/utils/Random.hpp>

#include <nupic/algorithms/Segment.hpp>

using namespace nupic::algorithms::Cells4;

//----------------------------------------------------------------------
/**
 * Utility routine. Given a src cell index, prints synapse as:
 *  [column, cell within col]
 */
void printSynapse(UInt srcCellIdx, UInt nCellsPerCol) {
  UInt col = (UInt)(srcCellIdx / nCellsPerCol);
  UInt cell = srcCellIdx - col * nCellsPerCol;
  std::cout << "[" << col << "," << cell << "]  ";
}

//----------------------------------------------------------------------
Segment::Segment(InSynapses _s, Real frequency, bool seqSegFlag,
                 Real permConnected, UInt iteration)
    : _totalActivations(1), _positiveActivations(1), _lastActiveIteration(0),
      _lastPosDutyCycle(1.0 / iteration), _lastPosDutyCycleIteration(iteration),
      _seqSegFlag(seqSegFlag), _frequency(frequency), _synapses(std::move(_s)),
      _nConnected(0) {
  for (UInt i = 0; i != _synapses.size(); ++i)
    if (_synapses[i].permanence() >= permConnected)
      ++_nConnected;

  std::sort(_synapses.begin(), _synapses.end(), InSynapseOrder());
  NTA_ASSERT(invariants());
}

//--------------------------------------------------------------------------------
Segment &Segment::operator=(const Segment &o) {
  if (&o != this) {
    _seqSegFlag = o._seqSegFlag;
    _frequency = o._frequency;
    _synapses = o._synapses;
    _nConnected = o._nConnected;
    _totalActivations = o._totalActivations;
    _positiveActivations = o._positiveActivations;
    _lastActiveIteration = o._lastActiveIteration;
    _lastPosDutyCycle = o._lastPosDutyCycle;
    _lastPosDutyCycleIteration = o._lastPosDutyCycleIteration;
  }
  NTA_ASSERT(invariants());
  return *this;
}

//--------------------------------------------------------------------------------
bool Segment::operator==(const Segment &other) const {
  if (_totalActivations != other._totalActivations ||
      _positiveActivations != other._positiveActivations ||
      _lastActiveIteration != other._lastActiveIteration ||
      !nearlyEqual(_lastPosDutyCycle, other._lastPosDutyCycle) ||
      _lastPosDutyCycleIteration  !=  other._lastPosDutyCycleIteration ||
      _seqSegFlag != other._seqSegFlag ||
      !nearlyEqual(_frequency, other._frequency) ||
      _nConnected != other._nConnected) {
    return false;
  }
  return _synapses == other._synapses;
}

//--------------------------------------------------------------------------------
Segment::Segment(const Segment &o)
    : _totalActivations(o._totalActivations),
      _positiveActivations(o._positiveActivations),
      _lastActiveIteration(o._lastActiveIteration),
      _lastPosDutyCycle(o._lastPosDutyCycle),
      _lastPosDutyCycleIteration(o._lastPosDutyCycleIteration),
      _seqSegFlag(o._seqSegFlag), _frequency(o._frequency),
      _synapses(o._synapses), _nConnected(o._nConnected) {
  NTA_ASSERT(invariants());
}

bool Segment::isActive(const CState &activities, Real permConnected,
                       UInt activationThreshold) const {
  { NTA_ASSERT(invariants()); }<commit_msg>Segment: fix equals redefinition<commit_after>/* ---------------------------------------------------------------------
 * Numenta Platform for Intelligent Computing (NuPIC)
 * Copyright (C) 2013, Numenta, Inc.  Unless you have an agreement
 * with Numenta, Inc., for a separate license for this software code, the
 * following terms and conditions apply:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero Public License for more details.
 *
 * You should have received a copy of the GNU Affero Public License
 * along with this program.  If not, see http://www.gnu.org/licenses.
 *
 * http://numenta.org/licenses/
 * ---------------------------------------------------------------------
 */

#include <algorithm> // sort
#include <iomanip>
#include <iostream>
#include <set>
#include <sstream>
#include <vector>

#include <assert.h>
#include <map>
#include <nupic/math/ArrayAlgo.hpp> // is_in
#include <nupic/math/StlIo.hpp>     // binary_save
#include <nupic/utils/Log.hpp>
#include <nupic/utils/Random.hpp>

#include <nupic/algorithms/Segment.hpp>

using namespace nupic::algorithms::Cells4;

//----------------------------------------------------------------------
/**
 * Utility routine. Given a src cell index, prints synapse as:
 *  [column, cell within col]
 */
void printSynapse(UInt srcCellIdx, UInt nCellsPerCol) {
  UInt col = (UInt)(srcCellIdx / nCellsPerCol);
  UInt cell = srcCellIdx - col * nCellsPerCol;
  std::cout << "[" << col << "," << cell << "]  ";
}

//----------------------------------------------------------------------
Segment::Segment(InSynapses _s, Real frequency, bool seqSegFlag,
                 Real permConnected, UInt iteration)
    : _totalActivations(1), _positiveActivations(1), _lastActiveIteration(0),
      _lastPosDutyCycle(1.0 / iteration), _lastPosDutyCycleIteration(iteration),
      _seqSegFlag(seqSegFlag), _frequency(frequency), _synapses(std::move(_s)),
      _nConnected(0) {
  for (UInt i = 0; i != _synapses.size(); ++i)
    if (_synapses[i].permanence() >= permConnected)
      ++_nConnected;

  std::sort(_synapses.begin(), _synapses.end(), InSynapseOrder());
  NTA_ASSERT(invariants());
}

//--------------------------------------------------------------------------------
Segment &Segment::operator=(const Segment &o) {
  if (&o != this) {
    _seqSegFlag = o._seqSegFlag;
    _frequency = o._frequency;
    _synapses = o._synapses;
    _nConnected = o._nConnected;
    _totalActivations = o._totalActivations;
    _positiveActivations = o._positiveActivations;
    _lastActiveIteration = o._lastActiveIteration;
    _lastPosDutyCycle = o._lastPosDutyCycle;
    _lastPosDutyCycleIteration = o._lastPosDutyCycleIteration;
  }
  NTA_ASSERT(invariants());
  return *this;
}

//--------------------------------------------------------------------------------
bool Segment::equals(const Segment &other) const {
  if (_totalActivations != other._totalActivations ||
      _positiveActivations != other._positiveActivations ||
      _lastActiveIteration != other._lastActiveIteration ||
      !nearlyEqual(_lastPosDutyCycle, other._lastPosDutyCycle) ||
      _lastPosDutyCycleIteration  !=  other._lastPosDutyCycleIteration ||
      _seqSegFlag != other._seqSegFlag ||
      !nearlyEqual(_frequency, other._frequency) ||
      _nConnected != other._nConnected) {
    return false;
  }
  return _synapses == other._synapses;
}

//--------------------------------------------------------------------------------
Segment::Segment(const Segment &o)
    : _totalActivations(o._totalActivations),
      _positiveActivations(o._positiveActivations),
      _lastActiveIteration(o._lastActiveIteration),
      _lastPosDutyCycle(o._lastPosDutyCycle),
      _lastPosDutyCycleIteration(o._lastPosDutyCycleIteration),
      _seqSegFlag(o._seqSegFlag), _frequency(o._frequency),
      _synapses(o._synapses), _nConnected(o._nConnected) {
  NTA_ASSERT(invariants());
}

bool Segment::isActive(const CState &activities, Real permConnected,
                       UInt activationThreshold) const {
  { NTA_ASSERT(invariants()); }
{'fraction_non_alphanumeric': 0.21883153715258083, 'fraction_numerical': 0.002949517867271696, 'mean_word_length': 2.4681353265145556, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '105106', 'n_tokens_mistral': 2575, 'n_tokens_neox': 2492, 'n_words': 601}