Combinations.hpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
44 #ifndef COMBINATIONS_INCLUDE
45 #define COMBINATIONS_INCLUDE
46 
47 #include "Exception.hpp"
48 
49 namespace gnsstk {
50 
51 
58 class Combinations {
59 public:
61  Combinations(void) noexcept
62  {
63  nc = n = k = 0;
64  Index = std::vector<int>(0);
65  }
66 
69  Combinations(int N, int K)
70  { init(N,K); }
71 
73  Combinations(const Combinations& right)
74  noexcept
75  {
76  *this = right;
77  }
78 
80  Combinations& operator=(const Combinations& right) noexcept
81  {
82  init(right.n,right.k);
83  nc = right.nc;
84  for(int j=0; j<k; j++) Index[j] = right.Index[j];
85  return *this;
86  }
87 
90  int Next(void) noexcept {
91  if(k < 1) return -1;
92  if(Increment(k-1) != -1) return ++nc;
93  return -1;
94  }
95 
98  int Selection(int j) noexcept
99  {
100  if(j < 0 || j >= k) return -1;
101  return Index[j];
102  }
103 
106  bool isSelected(int j) noexcept
107  {
108  for(int i=0; i<k; i++)
109  if(Index[i] == j) return true;
110  return false;
111  }
112 
113 private:
114 
117  void init(int N, int K)
118  {
119  if(K > N || N < 0 || K < 0) {
120  Exception e("Combinations(n,k) must have k <= n, with n,k >= 0");
121  GNSSTK_THROW(e);
122  }
123 
124  Index = std::vector<int>(K);
125  nc = 0;
126  k = K;
127  n = N;
128  for(int j=0; j<k; j++)
129  Index[j] = j;
130  }
131 
133  int Increment(int j) noexcept
134  {
135  if(Index[j] < n-k+j) { // can this index be incremented?
136  Index[j]++;
137  for(int m=j+1; m<k; m++)
138  Index[m]=Index[m-1]+1;
139  return 0;
140  }
141  // is this the last index?
142  if(j-1 < 0) return -1;
143  // increment the next lower index
144  return Increment(j-1);
145  }
146 
147 
148  int nc;
149  int k,n;
150  std::vector<int> Index;
151 
153 }; // end class Combinations
154 
155 } // end namespace gnsstk
156 
157 #endif // COMBINATIONS_INCLUDE
gnsstk::Combinations::init
void init(int N, int K)
Definition: Combinations.hpp:117
gnsstk::Combinations
Definition: Combinations.hpp:58
gnsstk::Combinations::Combinations
Combinations(void) noexcept
Default constructor.
Definition: Combinations.hpp:61
gnsstk::Combinations::Increment
int Increment(int j) noexcept
Recursive function to increment Index[j].
Definition: Combinations.hpp:133
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::Combinations::Next
int Next(void) noexcept
Definition: Combinations.hpp:90
gnsstk::Exception
Definition: Exception.hpp:151
gnsstk::Combinations::k
int k
Definition: Combinations.hpp:149
gnsstk::Combinations::Combinations
Combinations(int N, int K)
Definition: Combinations.hpp:69
gnsstk::Combinations::nc
int nc
number of combinations computed so far
Definition: Combinations.hpp:148
Exception.hpp
gnsstk::Combinations::Selection
int Selection(int j) noexcept
Definition: Combinations.hpp:98
gnsstk::Combinations::operator=
Combinations & operator=(const Combinations &right) noexcept
Assignment operator.
Definition: Combinations.hpp:80
GNSSTK_THROW
#define GNSSTK_THROW(exc)
Definition: Exception.hpp:366
gnsstk::Combinations::Index
std::vector< int > Index
Definition: Combinations.hpp:150
gnsstk::Combinations::n
int n
combinations of n things taken k at a time
Definition: Combinations.hpp:149
gnsstk::Combinations::isSelected
bool isSelected(int j) noexcept
Definition: Combinations.hpp:106
gnsstk::Combinations::Combinations
Combinations(const Combinations &right) noexcept
copy constructor
Definition: Combinations.hpp:73


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:38