Last active
August 29, 2015 14:14
-
-
Save zrbecker/a3a3cef69466b41bd540 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Combinations.h" | |
namespace ZB | |
{ | |
Combinations::Combinations(int n, int k) | |
: m_n(n) | |
, m_k(k) | |
{ | |
} | |
Combinations::iterator Combinations::begin() const | |
{ | |
return Combinations::iterator(m_n, m_k); | |
} | |
Combinations::iterator Combinations::end() const | |
{ | |
return Combinations::iterator(); | |
} | |
CombinationsIterator::CombinationsIterator() | |
: m_size(0) | |
, m_current() | |
{ | |
} | |
CombinationsIterator::CombinationsIterator(int n, int k) | |
: m_size(n) | |
, m_current() | |
{ | |
if (n > 0 && (k >= 0 && k <= n)) | |
{ | |
for (int i = 0; i < k; ++i) | |
m_current.push_back(i); | |
} | |
} | |
std::vector<int> &CombinationsIterator::operator*() | |
{ | |
return m_current; | |
} | |
std::vector<int> *CombinationsIterator::operator->() | |
{ | |
return &m_current; | |
} | |
CombinationsIterator &CombinationsIterator::operator++() | |
{ | |
if (m_current.size() != 0) | |
{ | |
unsigned i = 0; | |
while (i + 1 < m_current.size() && m_current[i] + 1 == m_current[i + 1]) | |
i += 1; | |
if (m_current[i] + 1 < m_size) | |
{ | |
m_current[i] += 1; | |
for (unsigned j = 0; j < i; ++j) | |
m_current[j] = j; | |
} | |
else | |
{ | |
m_size = 0; | |
m_current.clear(); | |
} | |
} | |
return *this; | |
} | |
CombinationsIterator CombinationsIterator::operator++(int) | |
{ | |
CombinationsIterator last = *this; | |
++(*this); | |
return last; | |
} | |
bool CombinationsIterator::operator==(const CombinationsIterator &other) | |
{ | |
if (m_size != other.m_size || m_current.size() != other.m_current.size()) | |
return false; | |
for (int i = 0; i < m_current.size(); ++i) | |
{ | |
if (m_current[i] != other.m_current[i]) | |
return false; | |
} | |
return true; | |
} | |
bool CombinationsIterator::operator!=(const CombinationsIterator &other) | |
{ | |
return !(*this == other); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef ZB_COMBINATIONS_H | |
#define ZB_COMBINATIONS_H | |
#include <vector> | |
namespace ZB | |
{ | |
class CombinationsIterator | |
{ | |
public: | |
CombinationsIterator(); | |
CombinationsIterator(int n, int k); | |
std::vector<int> &operator*(); | |
std::vector<int> *operator->(); | |
CombinationsIterator &operator++(); | |
CombinationsIterator operator++(int); | |
bool operator==(const CombinationsIterator &other); | |
bool operator!=(const CombinationsIterator &other); | |
private: | |
int m_size; | |
std::vector<int> m_current; | |
}; | |
class Combinations | |
{ | |
public: | |
typedef CombinationsIterator iterator; | |
Combinations(int n, int k); | |
iterator begin() const; | |
iterator end() const; | |
private: | |
int m_n; | |
int m_k; | |
}; | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "Combinations.h" | |
int main() | |
{ | |
std::string message = "ABCDEF"; | |
for (unsigned i = 1; i <= message.length(); ++i) | |
{ | |
ZB::Combinations combs(message.length(), i); | |
for (auto &c : combs) | |
{ | |
for (int j = 0; j < i; ++j) | |
std::cout << message[c[j]]; | |
std::cout << " "; | |
} | |
} | |
std::cout << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment