/* result-set.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from result-set.vala, do not modify */

/*
* Copyright (c) 2010 Michal Hruby <michal.mhr@gmail.com>
*               2017 elementary LLC.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authored by: Michal Hruby <michal.mhr@gmail.com>
*/

#include "slingshot.h"
#include <glib-object.h>
#include <gee.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	SYNAPSE_RESULT_SET_0_PROPERTY,
	SYNAPSE_RESULT_SET_ELEMENT_TYPE_PROPERTY,
	SYNAPSE_RESULT_SET_SIZE_PROPERTY,
	SYNAPSE_RESULT_SET_KEYS_PROPERTY,
	SYNAPSE_RESULT_SET_ENTRIES_PROPERTY,
	SYNAPSE_RESULT_SET_NUM_PROPERTIES
};
static GParamSpec* synapse_result_set_properties[SYNAPSE_RESULT_SET_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

static gpointer synapse_result_set_parent_class = NULL;
static GeeTraversableIface * synapse_result_set_gee_traversable_parent_iface = NULL;
static GeeIterableIface * synapse_result_set_gee_iterable_parent_iface = NULL;

static GeeIterator* synapse_result_set_real_iterator (GeeIterable* base);
static gboolean synapse_result_set_real_foreach (GeeTraversable* base,
                                          GeeForallFunc func,
                                          gpointer func_target);
static gint __lambda4_ (SynapseResultSet* self,
                 GeeMapEntry* a,
                 GeeMapEntry* b);
static gint ___lambda4__gcompare_data_func (gconstpointer a,
                                     gconstpointer b,
                                     gpointer self);
static GObject * synapse_result_set_constructor (GType type,
                                          guint n_construct_properties,
                                          GObjectConstructParam * construct_properties);
static void synapse_result_set_finalize (GObject * obj);
static GType synapse_result_set_get_type_once (void);
static void _vala_synapse_result_set_get_property (GObject * object,
                                            guint property_id,
                                            GValue * value,
                                            GParamSpec * pspec);

SynapseResultSet*
synapse_result_set_construct (GType object_type)
{
	SynapseResultSet * self = NULL;
	self = (SynapseResultSet*) g_object_new (object_type, NULL);
	return self;
}

SynapseResultSet*
synapse_result_set_new (void)
{
	return synapse_result_set_construct (SYNAPSE_TYPE_RESULT_SET);
}

static GeeIterator*
synapse_result_set_real_iterator (GeeIterable* base)
{
	SynapseResultSet * self;
	GeeMap* _tmp0_;
	GeeIterator* _tmp1_;
	GeeIterator* result;
	self = (SynapseResultSet*) base;
	_tmp0_ = self->matches;
	_tmp1_ = gee_iterable_iterator ((GeeIterable*) _tmp0_);
	result = _tmp1_;
	return result;
}

static gboolean
synapse_result_set_real_foreach (GeeTraversable* base,
                                 GeeForallFunc func,
                                 gpointer func_target)
{
	SynapseResultSet * self;
	GeeMap* _tmp0_;
	GeeSet* _tmp1_;
	GeeSet* _tmp2_;
	GeeSet* _tmp3_;
	gboolean _tmp4_;
	gboolean result;
	self = (SynapseResultSet*) base;
	_tmp0_ = self->matches;
	_tmp1_ = gee_map_get_keys (_tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = _tmp2_;
	_tmp4_ = gee_traversable_foreach ((GeeTraversable*) _tmp3_, func, func_target);
	_g_object_unref0 (_tmp3_);
	result = _tmp4_;
	return result;
}

void
synapse_result_set_add (SynapseResultSet* self,
                        SynapseMatch* match,
                        gint relevancy)
{
	GeeMap* _tmp0_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (match != NULL);
	_tmp0_ = self->matches;
	gee_map_set (_tmp0_, match, (gpointer) ((gintptr) relevancy));
	if (SYNAPSE_IS_URI_MATCH (match)) {
		const gchar* uri = NULL;
		const gchar* _tmp1_;
		const gchar* _tmp2_;
		gboolean _tmp3_ = FALSE;
		const gchar* _tmp4_;
		_tmp1_ = synapse_uri_match_get_uri (G_TYPE_CHECK_INSTANCE_CAST (match, SYNAPSE_TYPE_URI_MATCH, SynapseUriMatch));
		_tmp2_ = _tmp1_;
		uri = _tmp2_;
		_tmp4_ = uri;
		if (_tmp4_ != NULL) {
			const gchar* _tmp5_;
			_tmp5_ = uri;
			_tmp3_ = g_strcmp0 (_tmp5_, "") != 0;
		} else {
			_tmp3_ = FALSE;
		}
		if (_tmp3_) {
			GeeSet* _tmp6_;
			const gchar* _tmp7_;
			_tmp6_ = self->uris;
			_tmp7_ = uri;
			gee_collection_add ((GeeCollection*) _tmp6_, _tmp7_);
		}
	}
}

void
synapse_result_set_add_all (SynapseResultSet* self,
                            SynapseResultSet* rs)
{
	GeeMap* _tmp0_;
	GeeMap* _tmp1_;
	GeeSet* _tmp2_;
	GeeSet* _tmp3_;
	g_return_if_fail (self != NULL);
	if (rs == NULL) {
		return;
	}
	_tmp0_ = self->matches;
	_tmp1_ = rs->matches;
	gee_map_set_all (_tmp0_, _tmp1_);
	_tmp2_ = self->uris;
	_tmp3_ = rs->uris;
	gee_collection_add_all ((GeeCollection*) _tmp2_, (GeeCollection*) _tmp3_);
}

gboolean
synapse_result_set_contains_uri (SynapseResultSet* self,
                                 const gchar* uri)
{
	GeeSet* _tmp0_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (uri != NULL, FALSE);
	_tmp0_ = self->uris;
	result = gee_collection_contains ((GeeCollection*) _tmp0_, uri);
	return result;
}

static gint
__lambda4_ (SynapseResultSet* self,
            GeeMapEntry* a,
            GeeMapEntry* b)
{
	GeeMapEntry* e1 = NULL;
	GeeMapEntry* e2 = NULL;
	gint relevancy_delta = 0;
	GeeMapEntry* _tmp0_;
	gconstpointer _tmp1_;
	gconstpointer _tmp2_;
	GeeMapEntry* _tmp3_;
	gconstpointer _tmp4_;
	gconstpointer _tmp5_;
	gint result;
	g_return_val_if_fail (a != NULL, 0);
	g_return_val_if_fail (b != NULL, 0);
	e1 = G_TYPE_CHECK_INSTANCE_CAST (a, GEE_MAP_TYPE_ENTRY, GeeMapEntry);
	e2 = G_TYPE_CHECK_INSTANCE_CAST (b, GEE_MAP_TYPE_ENTRY, GeeMapEntry);
	_tmp0_ = e2;
	_tmp1_ = gee_map_entry_get_value (_tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = e1;
	_tmp4_ = gee_map_entry_get_value (_tmp3_);
	_tmp5_ = _tmp4_;
	relevancy_delta = ((gint) ((gintptr) _tmp2_)) - ((gint) ((gintptr) _tmp5_));
	if (relevancy_delta != 0) {
		result = relevancy_delta;
		return result;
	} else {
		GeeMapEntry* _tmp6_;
		gconstpointer _tmp7_;
		gconstpointer _tmp8_;
		const gchar* _tmp9_;
		const gchar* _tmp10_;
		GeeMapEntry* _tmp11_;
		gconstpointer _tmp12_;
		gconstpointer _tmp13_;
		const gchar* _tmp14_;
		const gchar* _tmp15_;
		_tmp6_ = e1;
		_tmp7_ = gee_map_entry_get_key (_tmp6_);
		_tmp8_ = _tmp7_;
		_tmp9_ = synapse_match_get_title ((SynapseMatch*) _tmp8_);
		_tmp10_ = _tmp9_;
		_tmp11_ = e2;
		_tmp12_ = gee_map_entry_get_key (_tmp11_);
		_tmp13_ = _tmp12_;
		_tmp14_ = synapse_match_get_title ((SynapseMatch*) _tmp13_);
		_tmp15_ = _tmp14_;
		result = g_ascii_strcasecmp (_tmp10_, _tmp15_);
		return result;
	}
}

static gint
___lambda4__gcompare_data_func (gconstpointer a,
                                gconstpointer b,
                                gpointer self)
{
	gint result;
	result = __lambda4_ ((SynapseResultSet*) self, (GeeMapEntry*) a, (GeeMapEntry*) b);
	return result;
}

GeeList*
synapse_result_set_get_sorted_list (SynapseResultSet* self)
{
	GeeArrayList* l = NULL;
	GeeArrayList* _tmp0_;
	GeeArrayList* _tmp1_;
	GeeMap* _tmp2_;
	GeeSet* _tmp3_;
	GeeSet* _tmp4_;
	GeeSet* _tmp5_;
	GeeArrayList* _tmp6_;
	GeeArrayList* sorted_list = NULL;
	GeeArrayList* _tmp7_;
	GeeList* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = gee_array_list_new (GEE_MAP_TYPE_ENTRY, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	l = _tmp0_;
	_tmp1_ = l;
	_tmp2_ = self->matches;
	_tmp3_ = gee_map_get_entries (_tmp2_);
	_tmp4_ = _tmp3_;
	_tmp5_ = _tmp4_;
	gee_array_list_add_all (_tmp1_, (GeeCollection*) _tmp5_);
	_g_object_unref0 (_tmp5_);
	_tmp6_ = l;
	gee_list_sort ((GeeList*) _tmp6_, ___lambda4__gcompare_data_func, g_object_ref (self), g_object_unref);
	_tmp7_ = gee_array_list_new (SYNAPSE_TYPE_MATCH, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	sorted_list = _tmp7_;
	{
		GeeArrayList* _m_list = NULL;
		GeeArrayList* _tmp8_;
		gint _m_size = 0;
		GeeArrayList* _tmp9_;
		gint _tmp10_;
		gint _tmp11_;
		gint _m_index = 0;
		_tmp8_ = l;
		_m_list = _tmp8_;
		_tmp9_ = _m_list;
		_tmp10_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp9_);
		_tmp11_ = _tmp10_;
		_m_size = _tmp11_;
		_m_index = -1;
		while (TRUE) {
			gint _tmp12_;
			gint _tmp13_;
			GeeMapEntry* m = NULL;
			GeeArrayList* _tmp14_;
			gpointer _tmp15_;
			GeeArrayList* _tmp16_;
			GeeMapEntry* _tmp17_;
			gconstpointer _tmp18_;
			gconstpointer _tmp19_;
			_m_index = _m_index + 1;
			_tmp12_ = _m_index;
			_tmp13_ = _m_size;
			if (!(_tmp12_ < _tmp13_)) {
				break;
			}
			_tmp14_ = _m_list;
			_tmp15_ = gee_abstract_list_get ((GeeAbstractList*) _tmp14_, _m_index);
			m = (GeeMapEntry*) _tmp15_;
			_tmp16_ = sorted_list;
			_tmp17_ = m;
			_tmp18_ = gee_map_entry_get_key (_tmp17_);
			_tmp19_ = _tmp18_;
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp16_, (SynapseMatch*) _tmp19_);
			_g_object_unref0 (m);
		}
	}
	result = (GeeList*) sorted_list;
	_g_object_unref0 (l);
	return result;
}

static GType
synapse_result_set_real_get_element_type (GeeTraversable* base)
{
	GType result;
	SynapseResultSet* self;
	GeeMap* _tmp0_;
	GType _tmp1_;
	GType _tmp2_;
	self = (SynapseResultSet*) base;
	_tmp0_ = self->matches;
	_tmp1_ = gee_traversable_get_element_type ((GeeTraversable*) _tmp0_);
	_tmp2_ = _tmp1_;
	result = _tmp2_;
	return result;
}

gint
synapse_result_set_get_size (SynapseResultSet* self)
{
	gint result;
	GeeMap* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	g_return_val_if_fail (self != NULL, 0);
	_tmp0_ = self->matches;
	_tmp1_ = gee_map_get_size (_tmp0_);
	_tmp2_ = _tmp1_;
	result = _tmp2_;
	return result;
}

GeeSet*
synapse_result_set_get_keys (SynapseResultSet* self)
{
	GeeSet* result;
	GeeMap* _tmp0_;
	GeeSet* _tmp1_;
	GeeSet* _tmp2_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->matches;
	_tmp1_ = gee_map_get_keys (_tmp0_);
	_tmp2_ = _tmp1_;
	result = _tmp2_;
	return result;
}

GeeSet*
synapse_result_set_get_entries (SynapseResultSet* self)
{
	GeeSet* result;
	GeeMap* _tmp0_;
	GeeSet* _tmp1_;
	GeeSet* _tmp2_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->matches;
	_tmp1_ = gee_map_get_entries (_tmp0_);
	_tmp2_ = _tmp1_;
	result = _tmp2_;
	return result;
}

static GObject *
synapse_result_set_constructor (GType type,
                                guint n_construct_properties,
                                GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	SynapseResultSet * self;
	GeeHashMap* _tmp0_;
	GeeHashSet* _tmp1_;
	parent_class = G_OBJECT_CLASS (synapse_result_set_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SYNAPSE_TYPE_RESULT_SET, SynapseResultSet);
	_tmp0_ = gee_hash_map_new (SYNAPSE_TYPE_MATCH, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, G_TYPE_INT, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
	_g_object_unref0 (self->matches);
	self->matches = (GeeMap*) _tmp0_;
	_tmp1_ = gee_hash_set_new (G_TYPE_STRING, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
	_g_object_unref0 (self->uris);
	self->uris = (GeeSet*) _tmp1_;
	return obj;
}

static void
synapse_result_set_class_init (SynapseResultSetClass * klass,
                               gpointer klass_data)
{
	synapse_result_set_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->get_property = _vala_synapse_result_set_get_property;
	G_OBJECT_CLASS (klass)->constructor = synapse_result_set_constructor;
	G_OBJECT_CLASS (klass)->finalize = synapse_result_set_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), SYNAPSE_RESULT_SET_ELEMENT_TYPE_PROPERTY, synapse_result_set_properties[SYNAPSE_RESULT_SET_ELEMENT_TYPE_PROPERTY] = g_param_spec_gtype ("element-type", "element-type", "element-type", G_TYPE_NONE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), SYNAPSE_RESULT_SET_SIZE_PROPERTY, synapse_result_set_properties[SYNAPSE_RESULT_SET_SIZE_PROPERTY] = g_param_spec_int ("size", "size", "size", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), SYNAPSE_RESULT_SET_KEYS_PROPERTY, synapse_result_set_properties[SYNAPSE_RESULT_SET_KEYS_PROPERTY] = g_param_spec_object ("keys", "keys", "keys", GEE_TYPE_SET, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), SYNAPSE_RESULT_SET_ENTRIES_PROPERTY, synapse_result_set_properties[SYNAPSE_RESULT_SET_ENTRIES_PROPERTY] = g_param_spec_object ("entries", "entries", "entries", GEE_TYPE_SET, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
}

static GType
synapse_result_set_gee_traversable_get_g_type (SynapseResultSet* self)
{
	return (GType) SYNAPSE_TYPE_MATCH;
}

static GBoxedCopyFunc
synapse_result_set_gee_traversable_get_g_dup_func (SynapseResultSet* self)
{
	return (GBoxedCopyFunc) g_object_ref;
}

static GDestroyNotify
synapse_result_set_gee_traversable_get_g_destroy_func (SynapseResultSet* self)
{
	return (GDestroyNotify) g_object_unref;
}

static void
synapse_result_set_gee_traversable_interface_init (GeeTraversableIface * iface,
                                                   gpointer iface_data)
{
	synapse_result_set_gee_traversable_parent_iface = g_type_interface_peek_parent (iface);
	iface->foreach = (gboolean (*) (GeeTraversable*, GeeForallFunc, gpointer)) synapse_result_set_real_foreach;
	iface->get_g_type = (GType (*) (GeeTraversable *)) synapse_result_set_gee_traversable_get_g_type;
	iface->get_g_dup_func = (GBoxedCopyFunc (*) (GeeTraversable *)) synapse_result_set_gee_traversable_get_g_dup_func;
	iface->get_g_destroy_func = (GDestroyNotify (*) (GeeTraversable *)) synapse_result_set_gee_traversable_get_g_destroy_func;
	iface->get_element_type = synapse_result_set_real_get_element_type;
}

static GType
synapse_result_set_gee_iterable_get_g_type (SynapseResultSet* self)
{
	return (GType) GEE_MAP_TYPE_ENTRY;
}

static GBoxedCopyFunc
synapse_result_set_gee_iterable_get_g_dup_func (SynapseResultSet* self)
{
	return (GBoxedCopyFunc) g_object_ref;
}

static GDestroyNotify
synapse_result_set_gee_iterable_get_g_destroy_func (SynapseResultSet* self)
{
	return (GDestroyNotify) g_object_unref;
}

static void
synapse_result_set_gee_iterable_interface_init (GeeIterableIface * iface,
                                                gpointer iface_data)
{
	synapse_result_set_gee_iterable_parent_iface = g_type_interface_peek_parent (iface);
	iface->iterator = (GeeIterator* (*) (GeeIterable*)) synapse_result_set_real_iterator;
	iface->get_g_type = (GType (*) (GeeIterable *)) synapse_result_set_gee_iterable_get_g_type;
	iface->get_g_dup_func = (GBoxedCopyFunc (*) (GeeIterable *)) synapse_result_set_gee_iterable_get_g_dup_func;
	iface->get_g_destroy_func = (GDestroyNotify (*) (GeeIterable *)) synapse_result_set_gee_iterable_get_g_destroy_func;
}

static void
synapse_result_set_instance_init (SynapseResultSet * self,
                                  gpointer klass)
{
}

static void
synapse_result_set_finalize (GObject * obj)
{
	SynapseResultSet * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SYNAPSE_TYPE_RESULT_SET, SynapseResultSet);
	_g_object_unref0 (self->matches);
	_g_object_unref0 (self->uris);
	G_OBJECT_CLASS (synapse_result_set_parent_class)->finalize (obj);
}

static GType
synapse_result_set_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (SynapseResultSetClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) synapse_result_set_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (SynapseResultSet), 0, (GInstanceInitFunc) synapse_result_set_instance_init, NULL };
	static const GInterfaceInfo gee_traversable_info = { (GInterfaceInitFunc) synapse_result_set_gee_traversable_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	static const GInterfaceInfo gee_iterable_info = { (GInterfaceInitFunc) synapse_result_set_gee_iterable_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType synapse_result_set_type_id;
	synapse_result_set_type_id = g_type_register_static (G_TYPE_OBJECT, "SynapseResultSet", &g_define_type_info, 0);
	g_type_add_interface_static (synapse_result_set_type_id, GEE_TYPE_TRAVERSABLE, &gee_traversable_info);
	g_type_add_interface_static (synapse_result_set_type_id, GEE_TYPE_ITERABLE, &gee_iterable_info);
	return synapse_result_set_type_id;
}

GType
synapse_result_set_get_type (void)
{
	static volatile gsize synapse_result_set_type_id__once = 0;
	if (g_once_init_enter (&synapse_result_set_type_id__once)) {
		GType synapse_result_set_type_id;
		synapse_result_set_type_id = synapse_result_set_get_type_once ();
		g_once_init_leave (&synapse_result_set_type_id__once, synapse_result_set_type_id);
	}
	return synapse_result_set_type_id__once;
}

static void
_vala_synapse_result_set_get_property (GObject * object,
                                       guint property_id,
                                       GValue * value,
                                       GParamSpec * pspec)
{
	SynapseResultSet * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, SYNAPSE_TYPE_RESULT_SET, SynapseResultSet);
	switch (property_id) {
		case SYNAPSE_RESULT_SET_ELEMENT_TYPE_PROPERTY:
		g_value_set_gtype (value, gee_traversable_get_element_type ((GeeTraversable*) self));
		break;
		case SYNAPSE_RESULT_SET_SIZE_PROPERTY:
		g_value_set_int (value, synapse_result_set_get_size (self));
		break;
		case SYNAPSE_RESULT_SET_KEYS_PROPERTY:
		g_value_take_object (value, synapse_result_set_get_keys (self));
		break;
		case SYNAPSE_RESULT_SET_ENTRIES_PROPERTY:
		g_value_take_object (value, synapse_result_set_get_entries (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

