⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 routingtable.cc

📁 In this implementation of AntNet-3.0 one could get the behavior of both algorithms through a simple
💻 CC
字号:
// -*- C++ -*-
// Copyright (C) 2003 Leherstuh f黵 Betrieb System/ Verteilte System, 
// Universitaet Dortmund 
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

// Author: Muddassar Farooq
// Informatik III, Universitaet Dortmund
// Germany

//-------------------------------------------------------------
// file: routingTable.cpp
//        (part of AntNet Routing Simulation)
//-------------------------------------------------------------



#include "routingTable.h"

Register_Class( routingTable )

routingTable::routingTable(const char* name):cObject()
{
	map = NULL;
	rTable = NULL;
}

routingTable::routingTable(int nodes, int neighbors, const char* name):cObject()
{
	rTable = new rEntry[ nodes * neighbors ];
	map = new int[neighbors];
	this->nodes = nodes;
	this->neighbors = neighbors;

	for(int i = 0; i < (nodes * neighbors); i++)
	{
		rTable[i].neighborGoodnessToDestination = 0.0;
		rTable[i].backUp = 0.0;
	}
}

routingTable::~routingTable()
{
	delete[] map;
	delete[] rTable;
}

routingTable::routingTable(const routingTable& rhs)
{
	this->rTable = NULL;
	this->map = NULL;
	this->nodes = 0;
	this->neighbors = 0;
	operator=(rhs);
}

routingTable& routingTable::operator=(const routingTable& rhs)
{
	if(this == &rhs)
	{
		return *this;
	}
	else
	{
		delete[] map;
		delete[] rTable;

		cObject::operator=(rhs);

		this->nodes = rhs.nodes;
		this->neighbors = rhs.neighbors;
		this->map = new int[rhs.neighbors];
		this->rTable = new rEntry[rhs.nodes * rhs.neighbors];

		for(int i = 0; i < rhs.neighbors; i++)
		{
			this->map[i] = rhs.map[i];
		}

		for(int j = 0; j < (rhs.neighbors * rhs.nodes); j++)
		{
			this->rTable[j] = rhs.rTable[j];
		}
		return *this;
	}
}


cObject* routingTable::dup() const
{
	return new routingTable(*this);
}

void routingTable::info(char *buf)
{
	cObject::info(buf);
	sprintf( buf+strlen(buf), "Neighbors=%d Nodes=%d", neighbors,nodes);
}

void routingTable::writeContents(ostream& os)
{
	os << "********************" << endl;
	os << "    Routing Table   " << endl;
	os << "*********************" << endl;
	os << endl;

	for(int i = 0; i < nodes; i++)
	{
		for(int k = 0; k < neighbors; k++)
		{
			int sIndex = k * nodes + i;
			os<< "Probal to destination: " << i << "   Via neighbor: " 
				<< map[k] << " = " << rTable[sIndex].neighborGoodnessToDestination;
			os << endl;
		}
	}
}


void routingTable::mapNeighborsToArrayIndex(int *neighbor)
{
	for(int i = 0; i < neighbors; i++)
	{
		map[i] = neighbor[i];
	}
}


int routingTable::findNeighborIndex(int neighbor)
{
	for(int i = 0; i < neighbors; i++)
	{
		if( map[i] == neighbor)
		{
			return i;
		}
	}
	return -1;
}

double routingTable::getDestViaThisNeighborProb(int dest, int neighbor)
{
	int index = findNeighborIndex(neighbor);

	if( index != -1)
	{
		int sIndex = index * nodes + dest;
		return rTable[sIndex].neighborGoodnessToDestination;
	}
	else
	{
		throw new cException("Could not find index of neighbor %d", neighbor);

	}
}

void routingTable::setDestViaThisNeighborProb(int dest, int neighbor, double prob)
{
	int index = findNeighborIndex(neighbor);

	if( index != -1)
	{
		int sIndex = index * nodes + dest;
		rTable[sIndex].neighborGoodnessToDestination = prob;
	}
	else
	{
		throw new cException("Could not find index of neighbor %d", neighbor);
	}
}

ostream& operator<<(ostream& os, routingTable& table)
{
	os << "********************" << endl;
	os << "    Routing Table   " << endl;
	os << "*********************" << endl;
	os << endl;

	for(int i = 0; i < table.nodes; i++)
	{
		for(int k = 0; k < table.neighbors; k++)
		{
			int sIndex = k * table.nodes + i;
			os<< "Probal to destination: " << i << "   Via neighbor: " 
				<< table.map[k] << " = " << table.rTable[sIndex].neighborGoodnessToDestination;
			os << endl;
		}
	}
	return os;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -