Index: /applications/robust/robustlib.h
===================================================================
--- /applications/robust/robustlib.h (revision 1213)
+++ /applications/robust/robustlib.h (revision 1214)
@@ -71,4 +71,8 @@
 	polyhedron* negativeparent;
 
+	polyhedron* next_poly;
+
+	polyhedron* prev_poly;
+
 	int message_counter;
 
@@ -222,4 +226,150 @@
 };
 
+class c_statistic
+{
+	polyhedron* end_poly;
+	polyhedron* start_poly;
+
+public:
+	vector<polyhedron*> rows;
+
+	vector<polyhedron*> row_ends;
+
+	c_statistic()
+	{
+		end_poly   = new polyhedron();
+		start_poly = new polyhedron();
+	};
+
+	void append_polyhedron(int row, polyhedron* appended_start, polyhedron* appended_end)
+	{
+		if(row>((int)rows.size())-1)
+		{
+			if(row>rows.size())
+			{
+				throw new exception("You are trying to append a polyhedron whose children are not in the statistic yet!");
+				return;
+			}
+
+			rows.push_back(end_poly);
+			row_ends.push_back(end_poly);
+		}
+
+		// POSSIBLE FAILURE: the function is not checking if start and end are connected
+
+		if(rows[row] != end_poly)
+		{
+			appended_start->prev_poly = row_ends[row];
+			row_ends[row]->next_poly = appended_start;			
+						
+		}
+		else if((row>0 && rows[row-1]!=end_poly)||row==0)
+		{
+			appended_start->prev_poly = start_poly;
+			rows[row]= appended_start;			
+		}
+		else
+		{
+			throw new exception("Wrong polyhedron insertion into statistic: missing intermediary polyhedron!");
+		}
+
+		appended_end->next_poly = end_poly;
+		row_ends[row] = appended_end;
+	}
+
+	void append_polyhedron(int row, polyhedron* appended_poly)
+	{
+		append_polyhedron(row,appended_poly,appended_poly);
+	}
+
+	void insert_polyhedron(int row, polyhedron* inserted_poly, polyhedron* following_poly)
+	{		
+		if(following_poly != end_poly)
+		{
+			inserted_poly->next_poly = following_poly;
+			inserted_poly->prev_poly = following_poly->prev_poly;
+
+			if(following_poly->prev_poly == start_poly)
+			{
+				rows[row] = inserted_poly;
+			}
+			else
+			{				
+				inserted_poly->prev_poly->next_poly = inserted_poly;								
+			}
+
+			following_poly->prev_poly = inserted_poly;
+		}
+		else
+		{
+			this->append_polyhedron(row, inserted_poly);
+		}		
+	
+	}
+
+
+	
+
+	void delete_polyhedron(int row, polyhedron* deleted_poly)
+	{
+		if(deleted_poly->prev_poly != start_poly)
+		{
+			deleted_poly->prev_poly->next_poly = deleted_poly->next_poly;
+		}
+		else
+		{
+			rows[row] = deleted_poly->next_poly;
+		}
+
+		if(deleted_poly->next_poly!=end_poly)
+		{
+			deleted_poly->next_poly->prev_poly = deleted_poly->prev_poly;
+		}
+		else
+		{
+			row_ends[row] = deleted_poly->prev_poly;
+		}
+
+		
+
+		deleted_poly->next_poly = NULL;
+		deleted_poly->prev_poly = NULL;					
+	}
+
+	int size()
+	{
+		return rows.size();
+	}
+
+	polyhedron* get_end()
+	{
+		return end_poly;
+	}
+
+	polyhedron* get_start()
+	{
+		return start_poly;
+	}
+
+	int row_size(int row)
+	{
+		if(this->size()>row && row>=0)
+		{
+			int row_size = 0;
+			
+			for(polyhedron* row_poly = rows[row]; row_poly!=end_poly; row_poly=row_poly->next_poly)
+			{
+				row_size++;
+			}
+
+			return row_size;
+		}
+		else
+		{
+			throw new exception("There is no row to obtain size from!");
+		}
+	}
+};
+
 
 //! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density
@@ -229,5 +379,5 @@
 	/// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result
 	/// of data update from Bayesian estimation or set by the user if this emlig is a prior density
-	vector<list<polyhedron*>> statistic;
+	c_statistic statistic;
 
 	vector<list<polyhedron*>> for_splitting;
@@ -241,9 +391,9 @@
 	void alter_toprow_conditions(vec condition, bool should_be_added)
 	{
-		for(list<polyhedron*>::iterator horiz_ref = (statistic.end()--)->begin();horiz_ref!=(statistic.end()--)->end();horiz_ref++)
+		for(polyhedron* horiz_ref = statistic.rows[statistic.size()-1];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly)
 		{
 			double product = 0;
 
-			list<vertex*>::iterator vertex_ref = (*horiz_ref)->vertices.begin();
+			list<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin();
 
 			do
@@ -255,10 +405,10 @@
 			if((product>0 && should_be_added)||(product<0 && !should_be_added))
 			{
-				((toprow*) (*horiz_ref))->condition += condition;
+				((toprow*) horiz_ref)->condition += condition;
 			}
 			else
 			{
-				((toprow*) (*horiz_ref))->condition -= condition;
-			}							
+				((toprow*) horiz_ref)->condition -= condition;
+			}				
 		}
 	}
@@ -427,8 +577,9 @@
 	/// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor.
 	emlig(int number_of_parameters)
-	{
+	{	
+
 		create_statistic(number_of_parameters);
 
-		for(vector<list<polyhedron*>>::iterator local_iter = statistic.begin();local_iter<statistic.end();local_iter++)
+		for(int i = 0;i<statistic.size();i++)
 		{
 			list<polyhedron*> empty_split;
@@ -442,5 +593,5 @@
 	/// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a
 	/// statistic is needed outside the constructor. Used for a user defined prior distribution on the parameters.
-	emlig(vector<list<polyhedron*>> statistic)
+	emlig(c_statistic statistic)
 	{
 		this->statistic = statistic;
@@ -519,8 +670,8 @@
 
 		
-
-		for(list<polyhedron*>::iterator horizontal_position = statistic.begin()->begin();horizontal_position!=statistic.begin()->end();horizontal_position++)
+		
+		for(polyhedron* horizontal_position = statistic.rows[0];horizontal_position!=statistic.get_end();horizontal_position=horizontal_position->next_poly)
 		{		
-			vertex* current_vertex = (vertex*)(*horizontal_position);
+			vertex* current_vertex = (vertex*)horizontal_position;
 			
 			if(should_add||should_remove)
@@ -556,10 +707,15 @@
 			}
 
-			send_state_message(current_vertex, toadd, toremove, 0);			
+			send_state_message(current_vertex, toadd, toremove, 0);		
+			
 		}
 
 		if(should_add)
 		{
-			for(vector<list<polyhedron*>>::iterator vert_ref = for_splitting.begin();vert_ref<for_splitting.end();vert_ref++)
+			int k = 1;
+
+			vector<list<polyhedron*>>::iterator beginning_ref = ++for_splitting.begin();
+
+			for(vector<list<polyhedron*>>::iterator vert_ref = beginning_ref;vert_ref<for_splitting.end();vert_ref++)
 			{			
 
@@ -570,5 +726,5 @@
 					polyhedron* current_polyhedron = (*split_ref);
 					
-					if(vert_ref == for_splitting.begin())
+					if(vert_ref == beginning_ref)
 					{
 						vec coordinates1 = ((vertex*)(*(current_polyhedron->children.begin())))->get_coordinates();						
@@ -617,14 +773,21 @@
 															current_polyhedron->negativechildren.end());
 
+					statistic.append_polyhedron(k-1, new_totally_neutral_child);
 					
-					
-
-
-					
+					statistic.insert_polyhedron(k, positive_poly, current_polyhedron);
+					statistic.insert_polyhedron(k, negative_poly, current_polyhedron);					
+
+					statistic.delete_polyhedron(k, current_polyhedron);				
 				}
-			}
-		}
-
-
+
+				k++;
+			}
+		}
+
+		vector<int> sizevector;
+		for(int s = 0;s<statistic.size();s++)
+		{
+			sizevector.push_back(statistic.row_size(s));
+		}
 	}
 
@@ -644,4 +807,5 @@
 		origin->vertices.push_back(origin);
 
+		/*
 		// As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse
 		// diagram. First we create a vector of polyhedrons..
@@ -653,4 +817,9 @@
 		// ..and we fill the statistic with the created vector.
 		statistic.push_back(origin_vec);
+		*/
+
+		statistic = *(new c_statistic());
+
+		statistic.append_polyhedron(0, origin);
 
 		// Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to 
@@ -680,7 +849,12 @@
 
 
+			/*
 			// Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram
 			vector<vector<polyhedron*>> new_statistic1;
 			vector<vector<polyhedron*>> new_statistic2;
+			*/
+
+			c_statistic* new_statistic1 = new c_statistic();
+			c_statistic* new_statistic2 = new c_statistic();
 
 			
@@ -693,4 +867,5 @@
 				int element_number = 0;
 
+				/*
 				vector<polyhedron*> supportnew_1;
 				vector<polyhedron*> supportnew_2;
@@ -698,7 +873,8 @@
 				new_statistic1.push_back(supportnew_1);
 				new_statistic2.push_back(supportnew_2);
+				*/
 
 				// for each polyhedron in the given row
-				for(list<polyhedron*>::iterator horiz_ref = (statistic.begin()+j)->begin();horiz_ref!=(statistic.begin()+j)->end();horiz_ref++)
+				for(polyhedron* horiz_ref = statistic.rows[j];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly)
 				{	
 					// Append an extra zero coordinate to each of the vertices for the new dimension
@@ -707,5 +883,5 @@
 					{
 						// cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates
-						((vertex*) (*horiz_ref))->push_coordinate(0);
+						((vertex*) horiz_ref)->push_coordinate(0);
 					}
 					/*
@@ -716,10 +892,10 @@
 
 					// if it has parents
-					if(!(*horiz_ref)->parents.empty())
+					if(!horiz_ref->parents.empty())
 					{
 						// save the relative address of this child in a vector kids_rel_addresses of all its parents.
 						// This information will later be used for copying the whole Hasse diagram with each of the
 						// relations contained within.
-						for(list<polyhedron*>::iterator parent_ref = (*horiz_ref)->parents.begin();parent_ref != (*horiz_ref)->parents.end();parent_ref++)
+						for(list<polyhedron*>::iterator parent_ref = horiz_ref->parents.begin();parent_ref != horiz_ref->parents.end();parent_ref++)
 						{
 							(*parent_ref)->kids_rel_addresses.push_back(element_number);							
@@ -749,5 +925,5 @@
 					// between each polyhedron and its offspring (comming from the copy) and a parent has all the
 					// vertices of its child plus more.
-					for(list<vertex*>::iterator vertex_ref = (*horiz_ref)->vertices.begin();vertex_ref!=(*horiz_ref)->vertices.end();vertex_ref++)
+					for(list<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin();vertex_ref!=horiz_ref->vertices.end();vertex_ref++)
 					{
 						current_copy1->vertices.push_back(*vertex_ref);
@@ -768,20 +944,34 @@
 					// kids and when we do that and know the child, in the child we will remember the parent we came from.
 					// This way all the parents/children relations are saved in both the parent and the child.
-					if(!(*horiz_ref)->kids_rel_addresses.empty())
-					{
-						for(list<int>::iterator kid_ref = (*horiz_ref)->kids_rel_addresses.begin();kid_ref!=(*horiz_ref)->kids_rel_addresses.end();kid_ref++)
+					if(!horiz_ref->kids_rel_addresses.empty())
+					{
+						for(list<int>::iterator kid_ref = horiz_ref->kids_rel_addresses.begin();kid_ref!=horiz_ref->kids_rel_addresses.end();kid_ref++)
 						{	
+							polyhedron* new_kid1 = new_statistic1->rows[j-1];
+							polyhedron* new_kid2 = new_statistic2->rows[j-1];
+
+							// THIS IS NOT EFFECTIVE: It could be improved by having the list indexed for new_statistic, but
+							// not indexed for statistic. Hopefully this will not cause a big slowdown - happens only offline.
+							if(*kid_ref)
+							{
+								for(int k = 1;k<=(*kid_ref);k++)
+								{
+									new_kid1=new_kid1->next_poly;
+									new_kid2=new_kid2->next_poly;
+								}
+							}
+							
 							// find the child and save the relation to the parent
-							current_copy1->children.push_back(new_statistic1[j-1][(*kid_ref)]);
-							current_copy2->children.push_back(new_statistic2[j-1][(*kid_ref)]);
+							current_copy1->children.push_back(new_kid1);
+							current_copy2->children.push_back(new_kid2);
 
 							// in the child save the parents' address
-							new_statistic1[j-1][(*kid_ref)]->parents.push_back(current_copy1);
-							new_statistic2[j-1][(*kid_ref)]->parents.push_back(current_copy2);
+							new_kid1->parents.push_back(current_copy1);
+							new_kid2->parents.push_back(current_copy2);
 						}						
 
 						// Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the
 						// Hasse diagram again)
-						(*horiz_ref)->kids_rel_addresses.clear();
+						horiz_ref->kids_rel_addresses.clear();
 					}
 					// If there were no children previously, we are copying a polyhedron that has been a vertex before.
@@ -800,19 +990,19 @@
 
 					// Save the mother in its offspring
-					current_copy1->children.push_back(*horiz_ref);
-					current_copy2->children.push_back(*horiz_ref);
+					current_copy1->children.push_back(horiz_ref);
+					current_copy2->children.push_back(horiz_ref);
 
 					// Save the offspring in its mother
-					(*horiz_ref)->parents.push_back(current_copy1);
-					(*horiz_ref)->parents.push_back(current_copy2);	
+					horiz_ref->parents.push_back(current_copy1);
+					horiz_ref->parents.push_back(current_copy2);	
 								
 					
 					// Add the copies into the relevant statistic. The statistic will later be appended to the previous
 					// Hasse diagram
-					new_statistic1[j].push_back(current_copy1);
-					new_statistic2[j].push_back(current_copy2);
+					new_statistic1->append_polyhedron(j,current_copy1);
+					new_statistic2->append_polyhedron(j,current_copy2);
 					
 					// Raise the count in the vector of polyhedrons
-					element_number++;
+					element_number++;			
 					
 				}
@@ -820,11 +1010,17 @@
 			}
 
+			/*
 			statistic.begin()->push_back(new_point1);
 			statistic.begin()->push_back(new_point2);
+			*/
+
+			statistic.append_polyhedron(0, new_point1);
+			statistic.append_polyhedron(0, new_point2);
 
 			// Merge the new statistics into the old one. This will either be the final statistic or we will
 			// reenter the widening loop. 
-			for(int j=0;j<new_statistic1.size();j++)
-			{
+			for(int j=0;j<new_statistic1->size();j++)
+			{
+				/*
 				if(j+1==statistic.size())
 				{
@@ -835,4 +1031,7 @@
 				(statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic1[j].begin(),new_statistic1[j].end());
 				(statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic2[j].begin(),new_statistic2[j].end());
+				*/
+				statistic.append_polyhedron(j+1,new_statistic1->rows[j],new_statistic1->row_ends[j]);
+				statistic.append_polyhedron(j+1,new_statistic2->rows[j],new_statistic2->row_ends[j]);
 			}
 
@@ -840,4 +1039,5 @@
 		}
 
+		/*
 		vector<list<toprow*>> toprow_statistic;
 		int line_count = 0;
@@ -856,5 +1056,13 @@
 
 			line_count++;
-		}
+		}*/
+
+		
+		vector<int> sizevector;
+		for(int s = 0;s<statistic.size();s++)
+		{
+			sizevector.push_back(statistic.row_size(s));
+		}
+		
 	}
 
