Program Listing for File variable_constraints.hpp

Return to documentation for file (/tmp/ws/src/fuse/fuse_constraints/include/fuse_constraints/variable_constraints.hpp)

/*
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2019, Locus Robotics
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of the copyright holder nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef FUSE_CONSTRAINTS__VARIABLE_CONSTRAINTS_HPP_
#define FUSE_CONSTRAINTS__VARIABLE_CONSTRAINTS_HPP_

#include <algorithm>
#include <initializer_list>
#include <iterator>
#include <iostream>
#include <unordered_set>
#include <vector>


namespace fuse_constraints
{

class VariableConstraints
{
public:
  void reserve(const size_t variable_count);

  bool empty() const;

  size_t size() const;

  unsigned int nextVariableIndex() const;

  void insert(const unsigned int constraint, const unsigned int variable);

  void insert(const unsigned int constraint, std::initializer_list<unsigned int> variable_list);

  template<typename VariableIndexIterator>
  void insert(
    const unsigned int constraint, VariableIndexIterator first,
    VariableIndexIterator last);

  void insert(const unsigned int variable);

  template<typename OutputIterator>
  OutputIterator getConstraints(const unsigned int variable_id, OutputIterator result) const;

  void print(std::ostream & stream = std::cout) const;

private:
  using ConstraintCollection = std::unordered_set<unsigned int>;
  using ConstraintsByVariable = std::vector<ConstraintCollection>;

  ConstraintsByVariable variable_constraints_;
};

template<typename VariableIndexIterator>
void VariableConstraints::insert(
  const unsigned int constraint, VariableIndexIterator first,
  VariableIndexIterator last)
{
  for (; first != last; ++first) {
    insert(constraint, *first);
  }
}

template<class OutputIterator>
OutputIterator VariableConstraints::getConstraints(
  const unsigned int variable_id,
  OutputIterator result) const
{
  const auto & constraints = variable_constraints_[variable_id];
  return std::copy(std::begin(constraints), std::end(constraints), result);
}

std::ostream & operator<<(std::ostream & stream, const VariableConstraints & variable_constraints);

}  // namespace fuse_constraints

#endif  // FUSE_CONSTRAINTS__VARIABLE_CONSTRAINTS_HPP_