summaryrefslogtreecommitdiff
path: root/trunk/glagen/algo_distribue/Distribue.hh
blob: ff3b76db54b283be57579d5b4f888fa18239284d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Classe Distribue



#ifndef		DISTRIBUE_HH_
# define	DISTRIBUE_HH_

#include "tree/tree.hh"
#include "tree/node.hh"
#include "network/data/Data.hh"
#include "network/Server.hh"
#include "tools/matrix.hh"

template<class TYPENODE> class Distribue
{
public:
  Distribue(const Tree<TYPENODE>& tree)
    : _tree(tree),
      _size(tree.get_node_root().get_number_node()),
      _mat(new Matrix<int> (_size, _size))
  {
    unsigned int ptr1 = 0, ptr2 = 0;
    std::list< Node<TYPENODE> > ptr_node = *new std::list< Node<TYPENODE> >;
    ptr_node.push_back(tree.get_node_root());
    while (ptr_node.size())
    {
      std::list< Node<TYPENODE> >::iterator childs = ptr_node.begin();
      std::list< Node<TYPENODE> >::iterator child =
	childs->get_childs()->begin();
      for (; child != childs->get_childs()->end(); ++child)
	{
	  ptr_node.push_back(*child);
	  (*_mat)(++ptr2, ptr1) = 1;
	}
      ptr_node.pop_front();
      ptr1++;
    }
  }

  void	depend(std::list<TYPENODE>& data, const unsigned int& ptr)
  {
    data.push_back(_tree.get_node_root().get_node_course_width(ptr).get_data());
    for (unsigned int i = 0; i < _size; ++i)
      if ((*_mat)(i, ptr))
	depend(data, i);
  }

  void	transfer(Server& server)
  {
    // Calcul du nombre de client necessaire
    unsigned int j = 0;
    std::list< list<TYPENODE> > list_data =
      *new std::list< list<TYPENODE> >;
    std::list<TYPENODE> data = *new std::list<TYPENODE>;
    for (unsigned int i  = 1; i < _size; ++i)
      {
	if ((*_mat)(i, 0)) // Ne prend que les noeuds fils du noeud root
	  {
	    ++j;
	    this->depend(data, i);
	    list_data.push_back(data);
	    data = *new std::list<TYPENODE>;
	  }
      }
    if (server.get_nb_client() < j)
      {
	std::cout << "Need more client" << std::endl;
	exit(-1);
      }

    std::list<int>::const_iterator fd_client =
      server.get_list_fd()->begin();
    data = *new std::list<TYPENODE>;
    for (unsigned int i  = 1; i < _size; ++i)
      {
	if ((*_mat)(i, 0)) // Ne prend que les noeuds fils du noeud root
	  {
	    ++j;
	    this->depend(data, i);
	    server.send_data(*fd_client++, Data<TYPENODE>(&data));
	    data = *new std::list<TYPENODE>;
	  }
      }
  }

  Matrix<int>&	get_matrix()
  {
    return (*_mat);
  }

private:
  Tree<TYPENODE> _tree;
  unsigned int _size;
  Matrix<int> *_mat;
};

#endif	// DISTRIBUTE_HH_