<commit_before>//
// RoadMap.cpp
//
// Copyright (c) 2001-2006 Virtual Terrain Project
// Free for all uses, see license.txt for details.
//

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "RoadMap.h"
#include "vtLog.h"
#include "FilePath.h"


//
// Helpers
//
void fwriteIP2(FPoint2 &p, FILE *fp)
{
	/*  FIXME:  Ahoy, there be byte order issues here.  See below in this routine.  */
	fwrite(&p, 8, 1, fp);
}
void freadIP2(FPoint2 &p, FILE *fp)
{
	/*  FIXME:  Ahoy, there be byte order issues here.  See below in this routine.  */
	fread(&p, 8, 1, fp);
}

//diff a - b.  result between PI and -PI.
float diffAngle(float a, float b)
{
	float result = a - b;
	while (result > PIf) {
		result -= PI2f;
	}
	while (result < -PIf) {
		result += PI2f;
	}
	return result;
}

//
// Nodes
//
TNode::TNode()
{
	m_pNext = NULL;
	m_iLinks = 0;
	m_id = -1;
	m_r = NULL;
	m_IntersectTypes = NULL;
	m_Lights = NULL;
	m_fLinkAngle = NULL;
}

TNode::~TNode()
{
	if (m_r) delete m_r;
	if (m_fLinkAngle) delete m_fLinkAngle;
	if (m_IntersectTypes) delete m_IntersectTypes;
	if (m_Lights) delete m_Lights;
}

bool TNode::operator==(TNode &ref)
{
	if (m_iLinks != ref.m_iLinks)
		return false;
	for (int i = 0; i < m_iLinks; i++)
	{
		if (m_IntersectTypes[i] != ref.m_IntersectTypes[i])
			return false;
		if (m_Lights[i] != ref.m_Lights[i])
			return false;
	}
	return true;
}

void TNode::Copy(TNode *node)
{
	m_p = node->m_p;
	m_iLinks = node->m_iLinks;
	m_id = node->m_id;

	if (node->m_fLinkAngle)
		m_fLinkAngle = new float[m_iLinks];

	m_IntersectTypes = new IntersectionType[m_iLinks];
	m_Lights = new LightStatus[m_iLinks];
	m_r = new TLink*[m_iLinks];
	for (int i = 0; i < m_iLinks; i++)
	{
		if (node->m_fLinkAngle)
			m_fLinkAngle[i] = node->m_fLinkAngle[i];

		m_IntersectTypes[i] = node->m_IntersectTypes[i];
		m_Lights[i] = node->m_Lights[i];
		m_r[i] = node->m_r[i];
	}

	//don't copy this
	m_pNext = NULL;
}

TLink *TNode::GetLink(int n)
{
	if (m_r && n < m_iLinks)	// safety check
		return m_r[n];
	else
		return NULL;
}

int TNode::FindLink(int roadID)
{
	for (int i = 0; i < m_iLinks; i++) {
		if (m_r[i]->m_id == roadID) {
			return i;
		}
	}
	return -1;
}

void TNode::AddLink(TLink *pR)
{
	int i;

	m_iLinks++;

	LinkPtr *old_roads = m_r;
	IntersectionType *iTypes = m_IntersectTypes;
	LightStatus *lights = m_Lights;

	m_r = new LinkPtr[m_iLinks];
	m_IntersectTypes = new IntersectionType[m_iLinks];
	m_Lights = new LightStatus[m_iLinks];

	// copy data from previous array
	for (i = 0; i < m_iLinks-1; i++) {
		m_r[i] = old_roads[i];
		m_IntersectTypes[i] = iTypes[i];
		m_Lights[i] = lights[i];
	}

	// fill in the entry for the new road
	m_r[i] = pR;
	m_IntersectTypes[i] = IT_NONE;
	m_Lights[i] = LT_INVALID;

	if (old_roads) delete old_roads;
	delete [] iTypes;
	delete [] lights;
}

void TNode::DetachLink(TLink *pR)
{
	for (int i = 0; i < m_iLinks; i++)
	{
		if (m_r[i] == pR)
		{
			for (int j = i; j < m_iLinks-1; j++) {
				m_r[j] = m_r[j+1];
				m_IntersectTypes[j] = m_IntersectTypes[j+1];
				m_Lights[j] = m_Lights[j+1];
			}<commit_msg>added [] on delete (<NAME>)<commit_after>//
// RoadMap.cpp
//
// Copyright (c) 2001-2006 Virtual Terrain Project
// Free for all uses, see license.txt for details.
//

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "RoadMap.h"
#include "vtLog.h"
#include "FilePath.h"


//
// Helpers
//
void fwriteIP2(FPoint2 &p, FILE *fp)
{
	/*  FIXME:  Ahoy, there be byte order issues here.  See below in this routine.  */
	fwrite(&p, 8, 1, fp);
}
void freadIP2(FPoint2 &p, FILE *fp)
{
	/*  FIXME:  Ahoy, there be byte order issues here.  See below in this routine.  */
	fread(&p, 8, 1, fp);
}

//diff a - b.  result between PI and -PI.
float diffAngle(float a, float b)
{
	float result = a - b;
	while (result > PIf) {
		result -= PI2f;
	}
	while (result < -PIf) {
		result += PI2f;
	}
	return result;
}

//
// Nodes
//
TNode::TNode()
{
	m_pNext = NULL;
	m_iLinks = 0;
	m_id = -1;
	m_r = NULL;
	m_IntersectTypes = NULL;
	m_Lights = NULL;
	m_fLinkAngle = NULL;
}

TNode::~TNode()
{
	if (m_r) delete m_r;
	if (m_fLinkAngle) delete m_fLinkAngle;
	if (m_IntersectTypes) delete m_IntersectTypes;
	if (m_Lights) delete m_Lights;
}

bool TNode::operator==(TNode &ref)
{
	if (m_iLinks != ref.m_iLinks)
		return false;
	for (int i = 0; i < m_iLinks; i++)
	{
		if (m_IntersectTypes[i] != ref.m_IntersectTypes[i])
			return false;
		if (m_Lights[i] != ref.m_Lights[i])
			return false;
	}
	return true;
}

void TNode::Copy(TNode *node)
{
	m_p = node->m_p;
	m_iLinks = node->m_iLinks;
	m_id = node->m_id;

	if (node->m_fLinkAngle)
		m_fLinkAngle = new float[m_iLinks];

	m_IntersectTypes = new IntersectionType[m_iLinks];
	m_Lights = new LightStatus[m_iLinks];
	m_r = new TLink*[m_iLinks];
	for (int i = 0; i < m_iLinks; i++)
	{
		if (node->m_fLinkAngle)
			m_fLinkAngle[i] = node->m_fLinkAngle[i];

		m_IntersectTypes[i] = node->m_IntersectTypes[i];
		m_Lights[i] = node->m_Lights[i];
		m_r[i] = node->m_r[i];
	}

	//don't copy this
	m_pNext = NULL;
}

TLink *TNode::GetLink(int n)
{
	if (m_r && n < m_iLinks)	// safety check
		return m_r[n];
	else
		return NULL;
}

int TNode::FindLink(int roadID)
{
	for (int i = 0; i < m_iLinks; i++) {
		if (m_r[i]->m_id == roadID) {
			return i;
		}
	}
	return -1;
}

void TNode::AddLink(TLink *pR)
{
	int i;

	m_iLinks++;

	LinkPtr *old_roads = m_r;
	IntersectionType *iTypes = m_IntersectTypes;
	LightStatus *lights = m_Lights;

	m_r = new LinkPtr[m_iLinks];
	m_IntersectTypes = new IntersectionType[m_iLinks];
	m_Lights = new LightStatus[m_iLinks];

	// copy data from previous array
	for (i = 0; i < m_iLinks-1; i++) {
		m_r[i] = old_roads[i];
		m_IntersectTypes[i] = iTypes[i];
		m_Lights[i] = lights[i];
	}

	// fill in the entry for the new road
	m_r[i] = pR;
	m_IntersectTypes[i] = IT_NONE;
	m_Lights[i] = LT_INVALID;

	if (old_roads) delete [] old_roads;
	delete [] iTypes;
	delete [] lights;
}

void TNode::DetachLink(TLink *pR)
{
	for (int i = 0; i < m_iLinks; i++)
	{
		if (m_r[i] == pR)
		{
			for (int j = i; j < m_iLinks-1; j++) {
				m_r[j] = m_r[j+1];
				m_IntersectTypes[j] = m_IntersectTypes[j+1];
				m_Lights[j] = m_Lights[j+1];
			}
{'fraction_non_alphanumeric': 0.16285482562854825, 'fraction_numerical': 0.010056772100567721, 'mean_word_length': 3.4941690962099123, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5082418', 'n_tokens_mistral': 2949, 'n_tokens_neox': 2798, 'n_words': 657}