Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
operations_research::math_opt::IdNameBiMap Class Reference

#include <model_summary.h>

Public Member Functions

 IdNameBiMap (bool check_names=true)
 
 IdNameBiMap (const IdNameBiMap &other)
 
IdNameBiMapoperator= (const IdNameBiMap &other)
 
 IdNameBiMap (IdNameBiMap &&other)=default
 
IdNameBiMapoperator= (IdNameBiMap &&other)=default
 
 IdNameBiMap (std::initializer_list< std::pair< int64_t, absl::string_view > > ids)
 
absl::Status Insert (int64_t id, std::string name)
 
absl::Status Erase (int64_t id)
 Removes the given id, or returns an error if id is not present.
 
bool HasId (int64_t id) const
 
bool HasName (absl::string_view name) const
 Will always return false if name is empty or if check_names was false.
 
bool Empty () const
 
int Size () const
 
int64_t next_free_id () const
 
absl::Status SetNextFreeId (int64_t new_next_free_id)
 
const gtl::linked_hash_map< int64_t, std::string > & id_to_name () const
 Iteration order is in increasing id order.
 
const std::optional< absl::flat_hash_map< absl::string_view, int64_t > > & nonempty_name_to_id () const
 Is std::nullopt if check_names was false at construction.
 
absl::Status BulkUpdate (absl::Span< const int64_t > deleted_ids, absl::Span< const int64_t > new_ids, absl::Span< const std::string *const > names)
 

Detailed Description

Maintains a bidirectional mapping between names and ids, e.g. as used for variables and linear constraints.

The following invariants are enforced:

  • Ids must be unique and increasing (in insertion order).
  • Ids are non-negative.
  • Ids are not equal to std::numeric_limits<int64_t>::max()
  • Ids removed are never reused.
  • Names must be either empty or unique when built with check_names=true.

Definition at line 50 of file model_summary.h.

Constructor & Destructor Documentation

◆ IdNameBiMap() [1/4]

operations_research::math_opt::IdNameBiMap::IdNameBiMap ( bool check_names = true)
inlineexplicit

If check_names=false, the names need not be unique and the reverse mapping of name to id is not available.

Definition at line 54 of file model_summary.h.

◆ IdNameBiMap() [2/4]

operations_research::math_opt::IdNameBiMap::IdNameBiMap ( const IdNameBiMap & other)

Needs a custom copy constructor/assign because absl::string_view to internal data is held as a member. No custom move is needed.

Definition at line 70 of file model_summary.cc.

◆ IdNameBiMap() [3/4]

operations_research::math_opt::IdNameBiMap::IdNameBiMap ( IdNameBiMap && other)
default

◆ IdNameBiMap() [4/4]

operations_research::math_opt::IdNameBiMap::IdNameBiMap ( std::initializer_list< std::pair< int64_t, absl::string_view > > ids)

This constructor CHECKs that the input ids are sorted in increasing order. This constructor is expected to be used only for unit tests of validation code.

Definition at line 62 of file model_summary.cc.

Member Function Documentation

◆ BulkUpdate()

absl::Status operations_research::math_opt::IdNameBiMap::BulkUpdate ( absl::Span< const int64_t > deleted_ids,
absl::Span< const int64_t > new_ids,
absl::Span< const std::string *const > names )
Warning
this may be mutated (partially updated) if an error is returned.

Definition at line 95 of file model_summary.cc.

◆ Empty()

bool operations_research::math_opt::IdNameBiMap::Empty ( ) const
inline

Definition at line 209 of file model_summary.h.

◆ Erase()

absl::Status operations_research::math_opt::IdNameBiMap::Erase ( int64_t id)
inline

Removes the given id, or returns an error if id is not present.

CHECK OK, name_view being in nonempty_name_to_id_ when the above is met is a class invariant.

Definition at line 179 of file model_summary.h.

◆ HasId()

bool operations_research::math_opt::IdNameBiMap::HasId ( int64_t id) const
inline

Definition at line 196 of file model_summary.h.

◆ HasName()

bool operations_research::math_opt::IdNameBiMap::HasName ( absl::string_view name) const
inline

Will always return false if name is empty or if check_names was false.

Definition at line 199 of file model_summary.h.

◆ id_to_name()

const gtl::linked_hash_map< int64_t, std::string > & operations_research::math_opt::IdNameBiMap::id_to_name ( ) const
inline

Iteration order is in increasing id order.

Definition at line 102 of file model_summary.h.

◆ Insert()

absl::Status operations_research::math_opt::IdNameBiMap::Insert ( int64_t id,
std::string name )
inline

Inserts the provided id and associate the provided name to it.

An error is returned if:

  • id is negative
  • id is not at least next_free_id()
  • id is max(int64_t)
  • name is a duplicated and check_names was true at construction.

As a side effect it updates next_free_id to id + 1.

Inline function implementations

larger than everything in the map.

Definition at line 152 of file model_summary.h.

◆ next_free_id()

int64_t operations_research::math_opt::IdNameBiMap::next_free_id ( ) const
inline

The next id that has never been used (0 initially since ids are non-negative).

Definition at line 213 of file model_summary.h.

◆ nonempty_name_to_id()

const std::optional< absl::flat_hash_map< absl::string_view, int64_t > > & operations_research::math_opt::IdNameBiMap::nonempty_name_to_id ( ) const
inline

Is std::nullopt if check_names was false at construction.

Definition at line 108 of file model_summary.h.

◆ operator=() [1/2]

IdNameBiMap & operations_research::math_opt::IdNameBiMap::operator= ( const IdNameBiMap & other)

non nullopt nonempty_name_to_id_.

Definition at line 72 of file model_summary.cc.

◆ operator=() [2/2]

IdNameBiMap & operations_research::math_opt::IdNameBiMap::operator= ( IdNameBiMap && other)
default

◆ SetNextFreeId()

absl::Status operations_research::math_opt::IdNameBiMap::SetNextFreeId ( int64_t new_next_free_id)
inline

Updates next_free_id(). Succeeds when the provided id is greater than every exiting id and is non-negative.

Definition at line 215 of file model_summary.h.

◆ Size()

int operations_research::math_opt::IdNameBiMap::Size ( ) const
inline

Definition at line 211 of file model_summary.h.


The documentation for this class was generated from the following files: