|
|
Регистрация Восстановить пароль |
Регистрация | Задать вопрос |
Заплачу за решение |
Новые сообщения |
Сообщения за день |
Расширенный поиск |
Правила |
Всё прочитано |
|
Опции темы | Поиск в этой теме |
18.04.2019, 22:19 | #1 |
Регистрация: 13.12.2018
Сообщений: 4
|
Механизм подсчёта ссылок для минимизации операций копирования
Привет всем,как мне реализовать механизм подсчета ссылок для минимизации операций копирования в классе(см.файл)? В интернете про этот механизм ничего не сказано. Напишите код для осуществления механизма и расскажите что он из себя представляет.
#include <string> #include <iostream> using namespace std; class SetOfChar { public: SetOfChar() { clear(); } SetOfChar(const SetOfChar &other) { copyFrom(other); } SetOfChar &operator=(const SetOfChar &other) { if (&other != this) copyFrom(other); return *this; } SetOfChar &operator<<(unsigned char value) { data_[value / 8] |= 1 << (value % 8); return *this; } SetOfChar &operator>>(unsigned char value) { data_[value / 8] &= ~(1 << (value % 8)); return *this; } bool operator[](unsigned char value) const { return has(value); } SetOfChar operator&(const SetOfChar &other) { return intercectionWith(other); } SetOfChar operator|(const SetOfChar &other) { return unionWith(other); } bool operator<(const SetOfChar &other) { return isSubsetOf(other); } bool operator>(const SetOfChar &other) { return other.isSubsetOf(*this); } SetOfChar intercectionWith(const SetOfChar &other) const { SetOfChar result; for (unsigned char i = 0; i < 32; ++i) result.data_[i] = data_[i] & other.data_[i]; return result; } SetOfChar unionWith(const SetOfChar &other) const { SetOfChar result; for (unsigned char i = 0; i < 32; ++i) result.data_[i] = data_[i] | other.data_[i]; return result; } bool isSubsetOf(const SetOfChar &other) const { for (unsigned char i = 0; i < 32; ++i) if (((data_[i] & other.data_[i]) ^ data_[i]) != 0) return false; return true; } bool operator==(const SetOfChar &other) const { return equalsTo(other); } bool equalsTo(const SetOfChar &other) const { for (unsigned char i = 0; i < 32; ++i) if (data_[i] != other.data_[i]) return false; return true; } bool has(unsigned char value) const { return ((data_[value / 8] & (1 << (value % 8))) != 0); } bool isEmpty() const { for (size_t i = 0; i < 32; ++i) if (data_[i] != 0) return false; return true; } void clear() { for (size_t i = 0; i < 32; ++i) data_[i] = 0; } void copyFrom(const SetOfChar &other) { for (size_t i = 0; i < 32; ++i) data_[i] = other.data_[i]; } private: unsigned char data_[32]; }; ostream &operator<<(ostream &stream, const SetOfChar &set) { for (unsigned char i = 0; i < 255; ++i) if (set[i]) stream << static_cast<int>(i) << " "; return stream; } int main(int argc, char *argv[]) { SetOfChar a, b, c; a << 1 << 2 << 4 << 6; b << 2 << 3 << 5 << 6; c = (a | b); cout << "a: " << a << endl << "b: " << b << endl << "c: " << c << endl; cout << "Union: " << (a | b) << endl; cout << "Intersection: " << (a & b) << endl; cout << "C includes A: " << ((c > a) ? "yes" : "no") << endl; cout << "C includes B: " << ((c > b) ? "yes" : "no") << endl; cout << "A includes B: " << ((a > b) ? "yes" : "no") << endl; cout << "A equals to B: " << ((a == b) ? "yes" : "no") << endl; system("pause"); return 0; } Последний раз редактировалось LichKin; 18.04.2019 в 23:48. |
Похожие темы | ||||
Тема | Автор | Раздел | Ответов | Последнее сообщение |
Платные снятия ссылок в sape - не сильно напрягаясь пролобировали оплату за снятие ссылок :) | Alar | Общие вопросы Web | 5 | 25.03.2016 16:23 |
Реализовать в виде модуля набор набор подпрограмм для следующих операций со строками : 1) Процедура подсчёта кол-ва букв к в пер | Richik123 | Паскаль, Turbo Pascal, PascalABC.NET | 12 | 23.09.2012 12:24 |
Макрос для копирования значений из нескольких файлов в один общий с определенным условием копирования | zenner | Microsoft Office Excel | 0 | 21.03.2011 14:48 |
Конструктор копирования, перегрузка операций | xPAL | Общие вопросы C/C++ | 1 | 15.10.2010 20:16 |
Реалирация подсчёта ссылок на объекты класса подобно объектом интерфейса | Altera | Общие вопросы Delphi | 2 | 26.05.2010 23:17 |