Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.cpp
- #include <iostream>
- #include "numseq.h"
- using namespace std;
- int main() {
- int y[10]={1,2,4,7,17,32,64,127,200,228};
- NumSeq<int> x(y,y+5);
- for (auto z:x){
- cout << z << endl;
- }
- for (auto it=x.begin();it!=x.end();++it){
- *it=2;
- }
- for (auto z:x){
- cout << z << endl;
- }
- for (int i=0;i<5;++i){
- cout << x[i] << endl;
- }
- return 0;
- }
- //numseq.hpp
- #include <vector>
- #include <iterator>
- using namespace std;
- template<class T>
- class BitIterator;
- template<class T>
- class Number;
- template<class T>
- class NumSeq {
- private:
- vector<T> seq;
- friend class Number<T>;
- friend class BitIterator<T>;
- public:
- BitIterator<T> begin();
- BitIterator<T> end();
- T &operator[](int i);
- NumSeq();
- template<typename _InputIterator,
- typename = std::_RequireInputIter<_InputIterator>>
- NumSeq(_InputIterator first, _InputIterator last): seq(first, last) {}
- };
- template<class T>
- BitIterator<T> NumSeq<T>::begin() {
- return BitIterator<T>(this);
- }
- template<class T>
- BitIterator<T> NumSeq<T>::end() {
- return BitIterator<T>(this, seq.size());
- }
- template<class T>
- T &NumSeq<T>::operator[](int i) {
- return seq[i];
- }
- template<class T>
- class Number {
- private:
- NumSeq<T> *seq;
- int pos, bits;
- public:
- Number(NumSeq<T> *seq, int pos);
- Number<T> &operator=(int x);
- Number<T> &operator=(const Number<T> &x);
- operator int();
- };
- template<class T>
- Number<T>::operator int() {
- for (int i = 0, size = sizeof(T) * 8, x = seq->seq[pos]; i < size && x != 0; ++i) {
- if (x & 1) ++this->bits;
- x >>= 1;
- }
- return bits;
- }
- template<class T>
- Number<T>::Number(NumSeq<T> *seq, int pos) {
- this->seq = seq;
- this->pos = pos;
- this->bits = 0;
- }
- template<class T>
- Number<T> &Number<T>::operator=(int x) {
- if (x > sizeof(T) * 8) {
- throw "you want too much bits, подумой";
- }
- this->bits = int(*this);
- if (x > bits) {
- T y = (T) 1;
- for (int diff = x - bits, num = seq->seq[pos]; diff; y <<= 1, num >>= 1) {
- if (!(num & 1)) {
- seq->seq[pos] |= y;
- --diff;
- }
- }
- } else if (bits > x) {
- T y = (T) 1;
- for (int diff = bits - x, num = seq->seq[pos]; diff; y <<= 1, num >>= 1) {
- if (num & 1) {
- seq->seq[pos] ^= y;
- --diff;
- }
- }
- }
- this->bits = x;
- return *this;
- }
- template<class T>
- Number<T> &Number<T>::operator=(const Number<T> &x) {
- *this = (int) x;
- return *this;
- }
- template<class T>
- class BitIterator : public iterator<forward_iterator_tag, int, ptrdiff_t, Number<T> *, Number<T> > {
- private:
- NumSeq<T> *seq;
- int pos;
- bool is_default;
- bool is_end() const { return pos > seq->seq.size(); }
- public:
- BitIterator() : is_default(true) {};
- BitIterator(NumSeq<T> *seq, int pos) : seq(seq), is_default(false), pos(pos) {}
- BitIterator(NumSeq<T> *seq) : BitIterator(seq, 0) {}
- bool operator==(const BitIterator<T> &y) const {
- return (is_default && y.is_default) ||
- (is_default && y.is_end()) ||
- (is_end() && y.is_default) ||
- (seq == y.seq && pos == y.pos);
- }
- bool operator!=(const BitIterator<T> &y) const {
- return !(*this == y);
- }
- Number<T> operator*() {
- if (is_default) {
- throw "i don't like this iterator, sorry";
- }
- return Number<T>(seq, pos);
- }
- BitIterator<T> &operator++() {
- if (is_default) {
- throw "i don't like this iterator, sorry";
- }
- ++pos;
- return *this;
- };
- BitIterator<T> &operator++(int) {
- BitIterator copy(*this);
- operator++();
- return copy;
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement