Скачиваний:
17
Добавлен:
01.05.2014
Размер:
6.23 Кб
Скачать
#pragma once
#include<iostream>
#include<vector>
#include<map>
#include<list>
#include<new>

#include "GetError.h"
#include "InsertError.h"
#include "DelError.h"

using namespace std;

template <class T>
class CGraph
{	
	list<int> l;					//список номеров вершин
	map<int, T> elems;				//список пар номер - элемент
	multimap<pair<int, int>, int> lines;		//список ребер

	bool checkList(int _index)        //Проверить, существует ли вершина с заданным номером в списке
	{
		list<int> :: iterator lit;
		lit = l.begin();

		while (lit != l.end())
		{
			if (*lit == _index)
			{
				return true;
			}
			lit++;
		}
		return false;
	}
	public :
		CGraph();
		void AddElement(T _elem, int _index);
		void AddLine(int _first, int _second);
		void delLine(int _first,int _second);
		void delElem(int _index);
		int getCount();
		int getNext(int _index);                    //Перейти к следующему элементу
		T getElement(int _index);                   
		list<int> getNextWays(int _index);			//вернуть список всех вершин
		T getElementByNom(int number);				//вернуть элемент по порядковуму номеру
		

		void printElems()
		{	
			list<int> :: iterator lit;
			lit = l.begin();

			while (lit != l.end())
			{
				cout << elems.find(*lit)->first << "  " << elems.find(*lit)->second << endl;
				lit++;
			}
			
		}

		void printLines()
		{	
			list<int> :: iterator lit1;
			list<int> :: iterator lit2;
			
			lit1 = l.begin();

			while(lit1 != l.end())
			{	
				lit2 = l.begin();
				while ( lit2 != l.end() )
				{
					if (*lit1 != *lit2)
					{
					   pair<int,int> rib(*lit1,*lit2);
					   cout << *lit1 << "  " << *lit2 << "  ";
    				   cout << lines.find(rib)->second << endl;
					}
					lit2++;
				}
				lit1++;
			}

		}

		void printList()
		{
			list<int> :: iterator lit;
			lit = l.begin();

			while(lit != l.end())
			{
				cout << *lit << " ";
				lit++;
			}
			cout << endl;
			
		}
};

template<class T>
CGraph<T> :: CGraph()
{
}

template<class T>
void CGraph<T> :: AddElement(T _elem, int _index)
{
	if(!checkList(_index))
	{
		l.push_back(_index);
		list<int> :: iterator lit;
		lit = l.begin();

		elems.insert(pair<int, T>(_index, _elem));

		while(lit != l.end())
		{
			pair<int,int> rib1(_index , elems.find(*lit)->first);
			pair<int,int> rib2(elems.find(*lit)->first , _index);
			lines.insert(pair<pair<int,int>,int>(rib1,0));
			lines.insert(pair<pair<int,int>,int>(rib2,0));
			lit++;
		}

		cout << "inserted element N" << _index << endl;
	}
else
	{	
	    int newelem=0;
		list<int> :: iterator lit;
		lit = l.begin();
		while(lit != l.end())
		{
			if (*lit > newelem)
			{
				newelem = *lit + 1;
			}
			lit++;
		}

		throw InsertError("AddElement","Insert not unique index",_index,newelem);//вставка элемента, уже имеющегося
	}
}

template<class T>
void CGraph<T> :: AddLine(int _first, int _second)
{	
	if(!checkList(_first))
	{
		throw InsertError("AddLine","No elements for this index",_first,-1);   
		                       //вставка ребра, ведущего из несуществующей вершины
	}
	if(!checkList(_second))
	{
		throw InsertError("AddLine","No elements for this index",_second,-1);   
		                      //вставка ребра, ведущего в несуществующую вершину
	}

	pair<int,int> rib1(_first , _second);
	pair<int,int> rib2(_second , _first);

	lines.find(rib1)->second = 1;
	lines.find(rib2)->second = -1;
}

template<class T>
void CGraph<T> :: delLine(int _first,int _second)
{
	if(!checkList(_first))
	{
		throw DelError("delLine","No elements for this index",_first,l.size()); 
		                     //Удаление ребра, ведущего из несуществующей вершины
	}
	if(!checkList(_second))
	{
		throw DelError("delLine","No elements for this index",_second,l.size());  
		                     //удаление ребра, ведущего в несуществующую вершину
	}


	pair<int,int> rib1(_first , _second);
	pair<int,int> rib2(_second , _first);

	if (lines.find(rib1)->second == 1)
	{
		lines.find(rib1)->second = 0;
		lines.find(rib2)->second = 0;
	}
        cout << "The rib (" << _first << " , " << _second << " is deleted";
}

template<class T>
void CGraph<T> ::delElem(int _index)
{	
	int oldsize = l.size();
	elems.erase(_index);
	l.remove(_index);


	list<int> :: iterator lit1;

	lit1 = l.begin();

	while (lit1 != l.end())
	{
		pair<int,int> rib1(_index , *lit1);
		pair<int,int> rib2(*lit1 , _index);
		lines.erase(rib1);
		lines.erase(rib2);
		lit1++;
	}

	if (oldsize == l.size())
	{
		throw DelError("delElem" , "not deleted" , _index,oldsize);
		                      //неправильное удаление элемента
	}
}

template<class T>
int CGraph<T> :: getNext(int _index)
{
	list<int> :: iterator lit;
	lit = l.begin();

	while(lit != l.end())
	{
		if (*lit == _index)
		{
			lit++;
			return *lit;
		}
		lit++;
	}
	
	throw GetError("getNext" , "No Next element" , _index);
	                           //последний элемент списка
}

template<class T>
int CGraph<T> :: getCount()
{
	return l.size();
}

template<class T>
T CGraph<T> ::getElement(int _index)
{
	if (checkList(_index))
	{
		return elems.find(_index)->second;
	}
	else
	{
		throw GetError("getElement","No element for this index",_index);
		                       //запрос элемента по несуществующему индексу
	}

}


template<class T>
T CGraph<T> :: getElementByNom(int number)
{
			if (number < l.size())
			{
				list<int> :: iterator lit;
				lit = l.begin();
		
				for (int i = 0 ; i < number ; i++)
				{
					lit++;
				}

				return getElement(*lit);		
			}
			else
			{
				throw GetError("getElementByNom" , "No element for this number" , number);
				                     //запрос элемента по несуществующему индексу
			}

}

template<class T>
list<int> CGraph<T> :: getNextWays(int _index)
{
	list<int> rlist;
	list<int> :: iterator lit1;
	lit1 = l.begin();

	while(lit1 != l.end())
	{
		pair<int,int> rib1(_index , *lit1);

		if (lines.find(rib1)->second == 1)
		{
			rlist.push_back(*lit1);
		}
		lit1++;
	}

	return rlist;
}
Соседние файлы в папке lab1t