<commit_before>/*
 * TTAddressItem
 * Copyright © 2012, <NAME>
 *
 * License: This code is licensed under the terms of the "New BSD License"
 * http://creativecommons.org/licenses/BSD/
 */

#ifndef DISABLE_NODELIB

#include "TTFoundationAPI.h"
#include "TTAddressItem.h"

TTAddressItem::TTAddressItem(TTSymbol aSymbol, TTAddressItemPtr aParent, TTBoolean aSelection):
	symbol(kTTSymEmpty),
	parent(NULL),
	selection(NO)
{
	if (aSymbol)
		this->symbol = aSymbol;
	else
		this->symbol = kTTSymEmpty;
	
	this->parent = aParent;
	this->selection = aSelection;
}

TTAddressItem::~TTAddressItem()
{
	this->clear();
}

void TTAddressItem::setSelection(const TTBoolean newSelectionState, TTBoolean recursively)
{
	// filter repetitions
	if (newSelectionState != this->selection)
		this->selection = newSelectionState;
	
	// propagate below if needed
	if (recursively)
		for (this->begin(); this->end(); this->next())
			this->current()->setSelection(newSelectionState, recursively);
	
	// propagate to parent if selection is YES
	if (newSelectionState && this->parent)
		this->parent->setSelection(newSelectionState, NO);
}

void TTAddressItem::setParent(const TTAddressItemPtr newParent)
{
	this->parent = newParent;
}

TTSymbol TTAddressItem::getSymbol()
{
	return this->symbol;
}

TTAddressItemPtr TTAddressItem::getParent()
{
	return this->parent;
}

TTBoolean TTAddressItem::getSelection()
{
	return this->selection;
}

TTAddressItemPtr TTAddressItem::getItem(TTSymbol aSymbol)
{
	TTAddressItemPtr anItem = NULL;
	TTValue found;
	
	TTErr err = ((TTListPtr)this)->find(&TTAddressItemFind, aSymbol.rawpointer(), found);
	
	if (!err)
		found.get(0, (TTPtr*)&anItem);
	
	return anItem;
}

void TTAddressItem::clear()
{
	// delete all items below
	for (this->begin(); this->end(); this->next())
		delete this->current();
	
	((TTListPtr)this)->clear();
}

TTErr TTAddressItem::append(TTAddress addressToAppend, TTAddressItemPtr *returnedItem)
{
	TTAddressItemPtr	anItem = this;
	TTAddressItemPtr	nextItem;
	TTList				nameInstanceList;
	TTSymbol			nameInstance(kTTSymEmpty);
	
	addressToAppend.listNameInstance(nameInstanceList);
	
	for (nameInstanceList.begin(); nameInstanceList.end(); nameInstanceList.next()) {
		nameInstanceList.current().get(0, nameInstance);
		
		nextItem = anItem->getItem(nameInstance);
		
		if (!nextItem) {
			nextItem = new TTAddressItem(nameInstance, anItem);
			((TTListPtr)anItem)->append((TTPtr)nextItem);
		}
		
		anItem = nextItem;
	}
	
	*returnedItem = anItem;
	return kTTErrNone;
}

TTErr TTAddressItem::remove(TTAddress addressToRemove)
{
	TTAddressItemPtr	anItem;
	TTAddressItemPtr	parentItem;
	
	// if the item exist
	if (!this->find(addressToRemove, &anItem)) {
		
		do {
			parentItem = anItem->getParent();
			((TTListPtr)parentItem)->remove((TTPtr)anItem);
			delete anItem;
			
			if (!parentItem) 
				break;
			
		} while (parentItem->isEmpty());
			
		return kTTErrNone;
	}
	
	return kTTErrGeneric;
}

TTErr TTAddressItem::find(TTAddress addressToFind, TTAddressItemPtr *returnedItem)
{
	TTAddressItemPtr	anItem = this;
	TTAddressItemPtr	nextItem;
	TTList				nameInstanceList;
	TTSymbol			nameInstance(kTTSymEmpty);
	
	addressToFind.listNameInstance(nameInstanceList);
	
	for (nameInstanceList.begin(); nameInstanceList.end(); nameInstanceList.next()) {
		
		nameInstanceList.current().get(0, nameInstance);
		
		nextItem = anItem->getItem(nameInstance);
		
		if (!nextItem)
			return kTTErrGeneric;
		else
			anItem = nextItem;
	}
	
	if (anItem != this) {
		*returnedItem = anItem;
		return kTTErrNone;
	}
	
	return kTTErrGeneric;
}

TTAddressItemPtr TTAddressItem::current()
{
	TTAddressItemPtr anItem;
	
	((TTListPtr)this)->current().get(0, (TTPtr*)&anItem);
	
	return anItem;
}

TTErr TTAddressItem::merge(const TTAddressItemPtr anItemToMerge)
{
	TTAddressItemPtr	anItem;
	//TTErr					err;
	
	if (!anItemToMerge)
		return kTTErrGeneric;
	
	anItem = this->getItem(anItemToMerge->symbol);
	
	if (!anItem) {<commit_msg>TTAddressItem::find return kTTErrValueNotFound instead of KTTErrGeneric<commit_after>/*
 * TTAddressItem
 * Copyright © 2012, <NAME>
 *
 * License: This code is licensed under the terms of the "New BSD License"
 * http://creativecommons.org/licenses/BSD/
 */

#ifndef DISABLE_NODELIB

#include "TTFoundationAPI.h"
#include "TTAddressItem.h"

TTAddressItem::TTAddressItem(TTSymbol aSymbol, TTAddressItemPtr aParent, TTBoolean aSelection):
	symbol(kTTSymEmpty),
	parent(NULL),
	selection(NO)
{
	if (aSymbol)
		this->symbol = aSymbol;
	else
		this->symbol = kTTSymEmpty;
	
	this->parent = aParent;
	this->selection = aSelection;
}

TTAddressItem::~TTAddressItem()
{
	this->clear();
}

void TTAddressItem::setSelection(const TTBoolean newSelectionState, TTBoolean recursively)
{
	// filter repetitions
	if (newSelectionState != this->selection)
		this->selection = newSelectionState;
	
	// propagate below if needed
	if (recursively)
		for (this->begin(); this->end(); this->next())
			this->current()->setSelection(newSelectionState, recursively);
	
	// propagate to parent if selection is YES
	if (newSelectionState && this->parent)
		this->parent->setSelection(newSelectionState, NO);
}

void TTAddressItem::setParent(const TTAddressItemPtr newParent)
{
	this->parent = newParent;
}

TTSymbol TTAddressItem::getSymbol()
{
	return this->symbol;
}

TTAddressItemPtr TTAddressItem::getParent()
{
	return this->parent;
}

TTBoolean TTAddressItem::getSelection()
{
	return this->selection;
}

TTAddressItemPtr TTAddressItem::getItem(TTSymbol aSymbol)
{
	TTAddressItemPtr anItem = NULL;
	TTValue found;
	
	TTErr err = ((TTListPtr)this)->find(&TTAddressItemFind, aSymbol.rawpointer(), found);
	
	if (!err)
		found.get(0, (TTPtr*)&anItem);
	
	return anItem;
}

void TTAddressItem::clear()
{
	// delete all items below
	for (this->begin(); this->end(); this->next())
		delete this->current();
	
	((TTListPtr)this)->clear();
}

TTErr TTAddressItem::append(TTAddress addressToAppend, TTAddressItemPtr *returnedItem)
{
	TTAddressItemPtr	anItem = this;
	TTAddressItemPtr	nextItem;
	TTList				nameInstanceList;
	TTSymbol			nameInstance(kTTSymEmpty);
	
	addressToAppend.listNameInstance(nameInstanceList);
	
	for (nameInstanceList.begin(); nameInstanceList.end(); nameInstanceList.next()) {
		nameInstanceList.current().get(0, nameInstance);
		
		nextItem = anItem->getItem(nameInstance);
		
		if (!nextItem) {
			nextItem = new TTAddressItem(nameInstance, anItem);
			((TTListPtr)anItem)->append((TTPtr)nextItem);
		}
		
		anItem = nextItem;
	}
	
	*returnedItem = anItem;
	return kTTErrNone;
}

TTErr TTAddressItem::remove(TTAddress addressToRemove)
{
	TTAddressItemPtr	anItem;
	TTAddressItemPtr	parentItem;
	
	// if the item exist
	if (!this->find(addressToRemove, &anItem)) {
		
		do {
			parentItem = anItem->getParent();
			((TTListPtr)parentItem)->remove((TTPtr)anItem);
			delete anItem;
			
			if (!parentItem) 
				break;
			
		} while (parentItem->isEmpty());
			
		return kTTErrNone;
	}
	
	return kTTErrGeneric;
}

TTErr TTAddressItem::find(TTAddress addressToFind, TTAddressItemPtr *returnedItem)
{
	TTAddressItemPtr	anItem = this;
	TTAddressItemPtr	nextItem;
	TTList				nameInstanceList;
	TTSymbol			nameInstance(kTTSymEmpty);
	
	addressToFind.listNameInstance(nameInstanceList);
	
	for (nameInstanceList.begin(); nameInstanceList.end(); nameInstanceList.next()) {
		
		nameInstanceList.current().get(0, nameInstance);
		
		nextItem = anItem->getItem(nameInstance);
		
		if (!nextItem)
			return kTTErrValueNotFound;
		else
			anItem = nextItem;
	}
	
	if (anItem != this) {
		*returnedItem = anItem;
		return kTTErrNone;
	}
	
	return kTTErrValueNotFound;
}

TTAddressItemPtr TTAddressItem::current()
{
	TTAddressItemPtr anItem;
	
	((TTListPtr)this)->current().get(0, (TTPtr*)&anItem);
	
	return anItem;
}

TTErr TTAddressItem::merge(const TTAddressItemPtr anItemToMerge)
{
	TTAddressItemPtr	anItem;
	//TTErr					err;
	
	if (!anItemToMerge)
		return kTTErrGeneric;
	
	anItem = this->getItem(anItemToMerge->symbol);
	
	if (!anItem) {
{'fraction_non_alphanumeric': 0.12647132481843226, 'fraction_numerical': 0.0020035061357375405, 'mean_word_length': 5.0876524390243905, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 75, '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': '4218383', 'n_tokens_mistral': 2951, 'n_tokens_neox': 2622, 'n_words': 531}