<commit_before>/* This file is part of Ingen.
 * Copyright (C) 2007 <NAME> <http://drobilla.net>
 * 
 * Ingen 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.
 * 
 * Ingen 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 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.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <utility>
#include <vector>
#include <raul/List.hpp>
#include <raul/PathTable.hpp>
#include <raul/TableImpl.hpp>
#include "ObjectStore.hpp"
#include "PatchImpl.hpp"
#include "NodeImpl.hpp"
#include "PortImpl.hpp"
#include "ThreadManager.hpp"

using namespace std;
using namespace Raul;

namespace Ingen {


/** Find the Patch at the given path.
 */
PatchImpl*
ObjectStore::find_patch(const Path& path) 
{
	GraphObjectImpl* const object = find_object(path);
	return dynamic_cast<PatchImpl*>(object);
}


/** Find the Node at the given path.
 */
NodeImpl*
ObjectStore::find_node(const Path& path) 
{
	GraphObjectImpl* const object = find_object(path);
	return dynamic_cast<NodeImpl*>(object);
}


/** Find the Port at the given path.
 */
PortImpl*
ObjectStore::find_port(const Path& path) 
{
	GraphObjectImpl* const object = find_object(path);
	return dynamic_cast<PortImpl*>(object);
}


/** Find the Object at the given path.
 */
GraphObjectImpl*
ObjectStore::find_object(const Path& path)
{
	Objects::iterator i = _objects.find(path);
	return ((i == _objects.end()) ? NULL : dynamic_cast<GraphObjectImpl*>(i->second.get()));
}


/** Add an object to the store. Not realtime safe.
 */
void
ObjectStore::add(GraphObjectImpl* o)
{
	assert(ThreadManager::current_thread_id() == THREAD_PRE_PROCESS);

	//cerr << "[ObjectStore] Adding " << o->path() << endl;
	_objects.insert(make_pair(o->path(), o));

	NodeImpl* node = dynamic_cast<NodeImpl*>(o);
	if (node) {
		for (uint32_t i=0; i < node->num_ports(); ++i) {
			add(node->port_impl(i));
		}
	}
}


/** Add a family of objects to the store. Not realtime safe.
 */
void
ObjectStore::add(const Table<Path, SharedPtr<Shared::GraphObject> >& table)
{
	assert(ThreadManager::current_thread_id() == THREAD_PRE_PROCESS);

	//cerr << "[ObjectStore] Adding " << o[0].second->path() << endl;
	_objects.cram(table);
	
	/*cerr << "[ObjectStore] Adding Table:" << endl;
	for (Objects::const_iterator i = table.begin(); i != table.end(); ++i) {
		cerr << i->first << " = " << i->second->path() << endl;
	}*/
}


/** Remove an object from the store.
 *
 * Returned is a vector containing all descendants of the object removed
 * including the object itself, in lexicographically sorted order by Path.
 */
SharedPtr< Table<Path, SharedPtr<Shared::GraphObject> > >
ObjectStore::remove(const Path& path)
{
	return remove(_objects.find(path));
}


/** Remove an object from the store.
 *
 * Returned is a vector containing all descendants of the object removed
 * including the object itself, in lexicographically sorted order by Path.
 */
SharedPtr< Table<Path, SharedPtr<Shared::GraphObject> > >
ObjectStore::remove(Objects::iterator object)
{
	assert(ThreadManager::current_thread_id() == THREAD_PRE_PROCESS);
	
	if (object != _objects.end()) {
		Objects::iterator descendants_end = _objects.find_descendants_end(object);
		//cout << "[ObjectStore] Removing " << object->first << " {" << endl;
		SharedPtr< Table<Path, SharedPtr<Shared::GraphObject> > > removed
				= _objects.yank(object, descendants_end);

		for (Objects::iterator i = removed->begin(); i != removed->end(); ++i) {
			cout << "\t" << i->first << endl;
		}
		cout << "}" << endl;
	
		return removed;

	} else {
		cerr << "[ObjectStore] WARNING: Removing " << object->first << " failed." << endl;
		return SharedPtr<Objects>();
	}
}

	
/** Remove all children of an object from the store.<commit_msg>Don't dump paths to console when deleting objects.<commit_after>/* This file is part of Ingen.
 * Copyright (C) 2007 <NAME> <http://drobilla.net>
 * 
 * Ingen 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.
 * 
 * Ingen 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 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.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <utility>
#include <vector>
#include <raul/List.hpp>
#include <raul/PathTable.hpp>
#include <raul/TableImpl.hpp>
#include "ObjectStore.hpp"
#include "PatchImpl.hpp"
#include "NodeImpl.hpp"
#include "PortImpl.hpp"
#include "ThreadManager.hpp"

using namespace std;
using namespace Raul;

namespace Ingen {


/** Find the Patch at the given path.
 */
PatchImpl*
ObjectStore::find_patch(const Path& path) 
{
	GraphObjectImpl* const object = find_object(path);
	return dynamic_cast<PatchImpl*>(object);
}


/** Find the Node at the given path.
 */
NodeImpl*
ObjectStore::find_node(const Path& path) 
{
	GraphObjectImpl* const object = find_object(path);
	return dynamic_cast<NodeImpl*>(object);
}


/** Find the Port at the given path.
 */
PortImpl*
ObjectStore::find_port(const Path& path) 
{
	GraphObjectImpl* const object = find_object(path);
	return dynamic_cast<PortImpl*>(object);
}


/** Find the Object at the given path.
 */
GraphObjectImpl*
ObjectStore::find_object(const Path& path)
{
	Objects::iterator i = _objects.find(path);
	return ((i == _objects.end()) ? NULL : dynamic_cast<GraphObjectImpl*>(i->second.get()));
}


/** Add an object to the store. Not realtime safe.
 */
void
ObjectStore::add(GraphObjectImpl* o)
{
	assert(ThreadManager::current_thread_id() == THREAD_PRE_PROCESS);

	//cerr << "[ObjectStore] Adding " << o->path() << endl;
	_objects.insert(make_pair(o->path(), o));

	NodeImpl* node = dynamic_cast<NodeImpl*>(o);
	if (node) {
		for (uint32_t i=0; i < node->num_ports(); ++i) {
			add(node->port_impl(i));
		}
	}
}


/** Add a family of objects to the store. Not realtime safe.
 */
void
ObjectStore::add(const Table<Path, SharedPtr<Shared::GraphObject> >& table)
{
	assert(ThreadManager::current_thread_id() == THREAD_PRE_PROCESS);

	//cerr << "[ObjectStore] Adding " << o[0].second->path() << endl;
	_objects.cram(table);
	
	/*cerr << "[ObjectStore] Adding Table:" << endl;
	for (Objects::const_iterator i = table.begin(); i != table.end(); ++i) {
		cerr << i->first << " = " << i->second->path() << endl;
	}*/
}


/** Remove an object from the store.
 *
 * Returned is a vector containing all descendants of the object removed
 * including the object itself, in lexicographically sorted order by Path.
 */
SharedPtr< Table<Path, SharedPtr<Shared::GraphObject> > >
ObjectStore::remove(const Path& path)
{
	return remove(_objects.find(path));
}


/** Remove an object from the store.
 *
 * Returned is a vector containing all descendants of the object removed
 * including the object itself, in lexicographically sorted order by Path.
 */
SharedPtr< Table<Path, SharedPtr<Shared::GraphObject> > >
ObjectStore::remove(Objects::iterator object)
{
	assert(ThreadManager::current_thread_id() == THREAD_PRE_PROCESS);
	
	if (object != _objects.end()) {
		Objects::iterator descendants_end = _objects.find_descendants_end(object);
		//cout << "[ObjectStore] Removing " << object->first << " {" << endl;
		SharedPtr< Table<Path, SharedPtr<Shared::GraphObject> > > removed
				= _objects.yank(object, descendants_end);
		/*for (Objects::iterator i = removed->begin(); i != removed->end(); ++i) {
			cout << "\t" << i->first << endl;
		}
		cout << "}" << endl;*/
	
		return removed;

	} else {
		cerr << "[ObjectStore] WARNING: Removing " << object->first << " failed." << endl;
		return SharedPtr<Objects>();
	}
}

	
/** Remove all children of an object from the store.
{'fraction_non_alphanumeric': 0.14221486038807382, 'fraction_numerical': 0.00473260766682442, 'mean_word_length': 4.539318479685452, 'pattern_counts': {'":': 0, '<': 153, '<?xml version=': 0, '>': 79, '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': '4309672', 'n_tokens_mistral': 2739, 'n_tokens_neox': 2591, 'n_words': 832}