Created
January 28, 2016 10:14
-
-
Save nesteruk/af319d5d9b7fa13103be to your computer and use it in GitHub Desktop.
Ховнокод с попыткой реализовать паттерн Композит в плюсах
This file contains hidden or 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 <cstdio> | |
#include <vector> | |
#include <iostream> | |
using namespace std; | |
struct Neuron | |
{ | |
vector<Neuron*> in, out; | |
unsigned int id; | |
Neuron() | |
{ | |
static int id = 1; | |
this->id = id++; | |
} | |
template <typename T> void connect_to(T& other) | |
{ | |
for (Neuron& to : other) | |
connect_to(other); | |
} | |
template<> void connect_to<Neuron>(Neuron& other) | |
{ | |
out.push_back(&other); | |
other.in.push_back(this); | |
} | |
// connect_to(vector<Neuron>&) | |
Neuron* begin() { return this; } | |
Neuron* end() { return this + 1; } | |
friend ostream& operator<<(ostream& os, const Neuron& obj) | |
{ | |
for (Neuron* n : obj.in) | |
{ | |
os << n->id << "\t-->\t[" << obj.id << "]" << endl; | |
} | |
for (Neuron* n : obj.out) | |
{ | |
os << "[" << obj.id << "]\t-->\t" << n->id << endl; | |
} | |
return os; | |
} | |
}; | |
struct NeuronLayer : vector<Neuron> | |
{ | |
NeuronLayer(int count) | |
{ | |
while (count-- > 0) | |
emplace_back(Neuron{}); | |
} | |
template <typename T> void connect_to(T& other) | |
{ | |
for (Neuron& from : *this) // not this | |
for (Neuron& to : other) | |
from.connect_to(to); | |
} | |
friend ostream& operator<<(ostream& os, const NeuronLayer& obj) | |
{ | |
for (auto& n : obj) os << n; | |
return os; | |
} | |
}; | |
int main() | |
{ | |
Neuron n1, n2; | |
n1.connect_to(n2); | |
cout << n1 << n2 << endl; | |
NeuronLayer l1{5}; | |
Neuron n3; | |
l1.connect_to(n3); | |
cout << "Neuron " << n3.id << endl << n3 << endl; | |
cout << "Layer " << endl << l1 << endl; | |
NeuronLayer l2{ 2 }, l3{ 3 }; | |
l2.connect_to(l3); | |
cout << "Layer l2" << endl << l2; | |
cout << "Layer l3" << endl << l3; | |
getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment