A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
utils/containers/sorted_vector/operators.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_CONTAINERS_SORTED_VECTOR_OPERATORS_H_
2 #define GENESIS_UTILS_CONTAINERS_SORTED_VECTOR_OPERATORS_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2017 Lucas Czech
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
35 
36 namespace genesis {
37 namespace utils {
38 
39 // =================================================================================================
40 // Sorted Vector
41 // =================================================================================================
42 
43 template< class T>
44 bool operator == ( SortedVector<T> const& lhs, SortedVector<T> const& rhs )
45 {
46  if( lhs.size() != rhs.size() ) {
47  return false;
48  }
49  for( size_t i = 0; i < lhs.size(); ++i ) {
50  if( lhs[i] != rhs[i] ) {
51  return false;
52  }
53  }
54  return true;
55 }
56 
57 template< class T>
58 bool operator != ( SortedVector<T> const& lhs, SortedVector<T> const& rhs )
59 {
60  return !( lhs == rhs );
61 }
62 
63 template< class T>
64 bool equals( SortedVector<T> const& lhs, SortedVector<T> const& rhs )
65 {
66  return lhs == rhs;
67 }
68 
69 template< class T>
70 bool is_subset_of( SortedVector<T> const& subset, SortedVector<T> const& superset )
71 {
72  if( subset.size() >= superset.size() ) {
73  return false;
74  }
75 
76  // Simple, but slow.
77  // for( auto const& elem : subset ) {
78  // if( ! superset.contains( elem )) {
79  // return false;
80  // }
81  // }
82 
83  // Iterate the subset values, and in parallel, iterate the superset to check if all values are
84  // contained in it.
85  auto it_sup = superset.begin();
86  for( auto sub_elem : subset ) {
87  while( it_sup != superset.end() && *it_sup != sub_elem ) {
88  ++it_sup;
89  }
90  if( it_sup == superset.end() ) {
91  return false;
92  }
93  }
94  return true;
95 }
96 
97 template< class T>
98 bool is_subset_or_equal( SortedVector<T> const& subset, SortedVector<T> const& superset )
99 {
100  if( subset.size() == superset.size() ) {
101  return ( subset == superset );
102  } else {
103  return is_subset_of( subset, superset );
104  }
105 }
106 
107 } // namespace utils
108 } // namespace genesis
109 
110 #endif // include guard
bool is_subset_of(SortedVector< T > const &subset, SortedVector< T > const &superset)
bool operator!=(SortedVector< T > const &lhs, SortedVector< T > const &rhs)
Sorted vector of unique elements.
bool is_subset_or_equal(SortedVector< T > const &subset, SortedVector< T > const &superset)
bool operator==(SortedVector< T > const &lhs, SortedVector< T > const &rhs)
bool equals(SortedVector< T > const &lhs, SortedVector< T > const &rhs)