/* sorted-bigram-language-model.c generated by valac 0.56.19, the Vala compiler
 * generated from sorted-bigram-language-model.vala, do not modify */

/*
 * Copyright (C) 2012-2014 Daiki Ueno <ueno@gnu.org>
 * Copyright (C) 2012-2014 Red Hat, Inc.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "libkkc/libkkc.h"
#include <marisa-glib/marisa-glib.h>
#include <glib-object.h>
#include <glib.h>
#include <gee.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.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

#define KKC_TYPE_INDEX_FILE (kkc_index_file_get_type ())
#define KKC_INDEX_FILE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), KKC_TYPE_INDEX_FILE, KkcIndexFile))
#define KKC_IS_INDEX_FILE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), KKC_TYPE_INDEX_FILE))
#define KKC_INDEX_FILE_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), KKC_TYPE_INDEX_FILE, KkcIndexFileIface))

typedef struct _KkcIndexFile KkcIndexFile;
typedef struct _KkcIndexFileIface KkcIndexFileIface;

#define KKC_TYPE_BLOOM_FILTER (kkc_bloom_filter_get_type ())
#define KKC_BLOOM_FILTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), KKC_TYPE_BLOOM_FILTER, KkcBloomFilter))
#define KKC_BLOOM_FILTER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), KKC_TYPE_BLOOM_FILTER, KkcBloomFilterClass))
#define KKC_IS_BLOOM_FILTER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), KKC_TYPE_BLOOM_FILTER))
#define KKC_IS_BLOOM_FILTER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), KKC_TYPE_BLOOM_FILTER))
#define KKC_BLOOM_FILTER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), KKC_TYPE_BLOOM_FILTER, KkcBloomFilterClass))

typedef struct _KkcBloomFilter KkcBloomFilter;
typedef struct _KkcBloomFilterClass KkcBloomFilterClass;
enum  {
	KKC_SORTED_BIGRAM_LANGUAGE_MODEL_0_PROPERTY,
	KKC_SORTED_BIGRAM_LANGUAGE_MODEL_BOS_PROPERTY,
	KKC_SORTED_BIGRAM_LANGUAGE_MODEL_EOS_PROPERTY,
	KKC_SORTED_BIGRAM_LANGUAGE_MODEL_MIN_COST_PROPERTY,
	KKC_SORTED_BIGRAM_LANGUAGE_MODEL_NUM_PROPERTIES
};
static GParamSpec* kkc_sorted_bigram_language_model_properties[KKC_SORTED_BIGRAM_LANGUAGE_MODEL_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

#define KKC_TYPE_MAPPED_INDEX_FILE (kkc_mapped_index_file_get_type ())
#define KKC_MAPPED_INDEX_FILE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), KKC_TYPE_MAPPED_INDEX_FILE, KkcMappedIndexFile))
#define KKC_MAPPED_INDEX_FILE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), KKC_TYPE_MAPPED_INDEX_FILE, KkcMappedIndexFileClass))
#define KKC_IS_MAPPED_INDEX_FILE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), KKC_TYPE_MAPPED_INDEX_FILE))
#define KKC_IS_MAPPED_INDEX_FILE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), KKC_TYPE_MAPPED_INDEX_FILE))
#define KKC_MAPPED_INDEX_FILE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), KKC_TYPE_MAPPED_INDEX_FILE, KkcMappedIndexFileClass))

typedef struct _KkcMappedIndexFile KkcMappedIndexFile;
typedef struct _KkcMappedIndexFileClass KkcMappedIndexFileClass;

#define KKC_TYPE_LOADED_INDEX_FILE (kkc_loaded_index_file_get_type ())
#define KKC_LOADED_INDEX_FILE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), KKC_TYPE_LOADED_INDEX_FILE, KkcLoadedIndexFile))
#define KKC_LOADED_INDEX_FILE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), KKC_TYPE_LOADED_INDEX_FILE, KkcLoadedIndexFileClass))
#define KKC_IS_LOADED_INDEX_FILE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), KKC_TYPE_LOADED_INDEX_FILE))
#define KKC_IS_LOADED_INDEX_FILE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), KKC_TYPE_LOADED_INDEX_FILE))
#define KKC_LOADED_INDEX_FILE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), KKC_TYPE_LOADED_INDEX_FILE, KkcLoadedIndexFileClass))

typedef struct _KkcLoadedIndexFile KkcLoadedIndexFile;
typedef struct _KkcLoadedIndexFileClass KkcLoadedIndexFileClass;
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _kkc_language_model_entry_free0(var) ((var == NULL) ? NULL : (var = (kkc_language_model_entry_free (var), NULL)))

struct _KkcIndexFileIface {
	GTypeInterface parent_iface;
	gchar* (*get_contents) (KkcIndexFile* self);
	gsize (*get_length) (KkcIndexFile* self);
};

struct _KkcSortedBigramLanguageModelPrivate {
	KkcLanguageModelEntry _bos;
	KkcLanguageModelEntry _eos;
	MarisaTrie* input_trie;
	MarisaTrie* unigram_trie;
	KkcIndexFile* unigram_index;
	KkcIndexFile* bigram_index;
	KkcBloomFilter* bigram_filter;
	guint32 last_value;
	guint32 last_pvalue;
	glong last_offset;
};

static gint KkcSortedBigramLanguageModel_private_offset;
static gpointer kkc_sorted_bigram_language_model_parent_class = NULL;
static KkcUnigramLanguageModelIface * kkc_sorted_bigram_language_model_kkc_unigram_language_model_parent_iface = NULL;
static KkcBigramLanguageModelIface * kkc_sorted_bigram_language_model_kkc_bigram_language_model_parent_iface = NULL;

VALA_EXTERN GType kkc_index_file_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GType kkc_bloom_filter_get_type (void) G_GNUC_CONST ;
static GeeCollection* kkc_sorted_bigram_language_model_real_unigram_entries (KkcLanguageModel* base,
                                                                      const gchar* prefix);
static GeeCollection* kkc_sorted_bigram_language_model_real_entries (KkcLanguageModel* base,
                                                              const gchar* input);
static KkcLanguageModelEntry* kkc_sorted_bigram_language_model_real_get (KkcLanguageModel* base,
                                                                  const gchar* input,
                                                                  const gchar* output);
VALA_EXTERN gboolean kkc_bloom_filter_contains (KkcBloomFilter* self,
                                    guint32 b0,
                                    guint32 b1);
VALA_EXTERN glong kkc_language_model_utils_bsearch_ngram (void* memory,
                                              glong start_offset,
                                              glong end_offset,
                                              glong record_size,
                                              guint8* needle,
                                              gint needle_length1);
VALA_EXTERN gchar* kkc_index_file_get_contents (KkcIndexFile* self);
VALA_EXTERN gsize kkc_index_file_get_length (KkcIndexFile* self);
static gdouble kkc_sorted_bigram_language_model_real_unigram_cost (KkcUnigramLanguageModel* base,
                                                            KkcLanguageModelEntry* entry);
VALA_EXTERN gdouble kkc_language_model_utils_decode_cost (guint16 cost,
                                              gdouble min_cost);
static gdouble kkc_sorted_bigram_language_model_real_unigram_backoff (KkcUnigramLanguageModel* base,
                                                               KkcLanguageModelEntry* entry);
static gboolean kkc_sorted_bigram_language_model_real_has_bigram (KkcBigramLanguageModel* base,
                                                           KkcLanguageModelEntry* pentry,
                                                           KkcLanguageModelEntry* entry);
static gdouble kkc_sorted_bigram_language_model_real_bigram_cost (KkcBigramLanguageModel* base,
                                                           KkcLanguageModelEntry* pentry,
                                                           KkcLanguageModelEntry* entry);
static gdouble kkc_sorted_bigram_language_model_real_bigram_backoff (KkcBigramLanguageModel* base,
                                                              KkcLanguageModelEntry* pentry,
                                                              KkcLanguageModelEntry* entry);
static gboolean kkc_sorted_bigram_language_model_real_parse (KkcLanguageModel* base,
                                                      GError** error);
VALA_EXTERN GType kkc_mapped_index_file_get_type (void) G_GNUC_CONST ;
VALA_EXTERN KkcMappedIndexFile* kkc_mapped_index_file_new (const gchar* filename,
                                               GError** error);
VALA_EXTERN KkcMappedIndexFile* kkc_mapped_index_file_construct (GType object_type,
                                                     const gchar* filename,
                                                     GError** error);
VALA_EXTERN GType kkc_loaded_index_file_get_type (void) G_GNUC_CONST ;
VALA_EXTERN KkcLoadedIndexFile* kkc_loaded_index_file_new (const gchar* filename,
                                               GError** error);
VALA_EXTERN KkcLoadedIndexFile* kkc_loaded_index_file_construct (GType object_type,
                                                     const gchar* filename,
                                                     GError** error);
VALA_EXTERN KkcBloomFilter* kkc_bloom_filter_new (const gchar* filename,
                                      GError** error);
VALA_EXTERN KkcBloomFilter* kkc_bloom_filter_construct (GType object_type,
                                            const gchar* filename,
                                            GError** error);
static void kkc_sorted_bigram_language_model_finalize (GObject * obj);
static GType kkc_sorted_bigram_language_model_get_type_once (void);
static void _vala_kkc_sorted_bigram_language_model_get_property (GObject * object,
                                                          guint property_id,
                                                          GValue * value,
                                                          GParamSpec * pspec);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static gssize _vala_array_length (gpointer array);

static inline gpointer
kkc_sorted_bigram_language_model_get_instance_private (KkcSortedBigramLanguageModel* self)
{
	return G_STRUCT_MEMBER_P (self, KkcSortedBigramLanguageModel_private_offset);
}

static guint8*
string_get_data (const gchar* self,
                 gint* result_length1)
{
	guint8* result;
	guint8* res = NULL;
	gint res_length1;
	gint _res_size_;
	gint _tmp0_;
	gint _tmp1_;
	guint8* _tmp2_;
	gint _tmp2__length1;
	g_return_val_if_fail (self != NULL, NULL);
	res = (guint8*) self;
	res_length1 = -1;
	_res_size_ = res_length1;
	_tmp0_ = strlen (self);
	_tmp1_ = _tmp0_;
	res_length1 = (gint) _tmp1_;
	_tmp2_ = res;
	_tmp2__length1 = res_length1;
	if (result_length1) {
		*result_length1 = _tmp2__length1;
	}
	result = _tmp2_;
	return result;
}

static GeeCollection*
kkc_sorted_bigram_language_model_real_unigram_entries (KkcLanguageModel* base,
                                                       const gchar* prefix)
{
	KkcSortedBigramLanguageModel * self;
	GeeArrayList* entries = NULL;
	GeeArrayList* _tmp0_;
	MarisaAgent* agent = NULL;
	MarisaAgent* _tmp1_;
	gchar* query = NULL;
	gchar* _tmp2_;
	MarisaAgent* _tmp3_;
	const gchar* _tmp4_;
	guint8* _tmp5_;
	gint _tmp5__length1;
	gint _tmp6_ = 0;
	guint8* _tmp7_;
	gint _tmp7__length1;
	GeeCollection* result;
	self = (KkcSortedBigramLanguageModel*) base;
	g_return_val_if_fail (prefix != NULL, NULL);
	_tmp0_ = gee_array_list_new (KKC_TYPE_LANGUAGE_MODEL_ENTRY, (GBoxedCopyFunc) kkc_language_model_entry_dup, (GDestroyNotify) kkc_language_model_entry_free, NULL, NULL, NULL);
	entries = _tmp0_;
	_tmp1_ = marisa_agent_new ();
	agent = _tmp1_;
	_tmp2_ = g_strconcat (prefix, "/", NULL);
	query = _tmp2_;
	_tmp3_ = agent;
	_tmp4_ = query;
	_tmp5_ = string_get_data (_tmp4_, &_tmp6_);
	_tmp5__length1 = _tmp6_;
	_tmp7_ = _tmp5_;
	_tmp7__length1 = _tmp5__length1;
	marisa_agent_set_query (_tmp3_, (const gchar*) _tmp7_, (gsize) _tmp7__length1);
	while (TRUE) {
		MarisaTrie* _tmp8_;
		MarisaAgent* _tmp9_;
		MarisaKey* key = NULL;
		MarisaAgent* _tmp10_;
		MarisaKey* _tmp11_;
		gchar** input_output = NULL;
		MarisaKey* _tmp12_;
		gchar* _tmp13_;
		gchar* _tmp14_;
		gchar** _tmp15_;
		gchar** _tmp16_;
		gchar** _tmp17_;
		gint _tmp17__length1;
		gint input_output_length1;
		gint _input_output_size_;
		guint id = 0U;
		MarisaKey* _tmp18_;
		KkcLanguageModelEntry entry = {0};
		gchar** _tmp19_;
		gint _tmp19__length1;
		const gchar* _tmp20_;
		gchar* _tmp21_;
		gchar** _tmp22_;
		gint _tmp22__length1;
		const gchar* _tmp23_;
		gchar* _tmp24_;
		KkcLanguageModelEntry _tmp25_ = {0};
		GeeArrayList* _tmp26_;
		KkcLanguageModelEntry _tmp27_;
		_tmp8_ = self->priv->unigram_trie;
		_tmp9_ = agent;
		if (!marisa_trie_predictive_search (_tmp8_, _tmp9_)) {
			break;
		}
		_tmp10_ = agent;
		_tmp11_ = marisa_agent_get_key (_tmp10_);
		key = _tmp11_;
		_tmp12_ = key;
		_tmp13_ = marisa_key_get_string (_tmp12_, NULL);
		_tmp14_ = _tmp13_;
		_tmp16_ = _tmp15_ = g_strsplit (_tmp14_, "/", 0);
		_tmp17_ = _tmp16_;
		_tmp17__length1 = _vala_array_length (_tmp15_);
		_g_free0 (_tmp14_);
		input_output = _tmp17_;
		input_output_length1 = _tmp17__length1;
		_input_output_size_ = input_output_length1;
		_tmp18_ = key;
		id = (guint) marisa_key_get_id (_tmp18_);
		_tmp19_ = input_output;
		_tmp19__length1 = input_output_length1;
		_tmp20_ = _tmp19_[0];
		_tmp21_ = g_strdup (_tmp20_);
		_tmp22_ = input_output;
		_tmp22__length1 = input_output_length1;
		_tmp23_ = _tmp22_[1];
		_tmp24_ = g_strdup (_tmp23_);
		_g_free0 (_tmp25_.input);
		_tmp25_.input = _tmp21_;
		_g_free0 (_tmp25_.output);
		_tmp25_.output = _tmp24_;
		_tmp25_.id = id;
		entry = _tmp25_;
		_tmp26_ = entries;
		_tmp27_ = entry;
		gee_abstract_collection_add ((GeeAbstractCollection*) _tmp26_, &_tmp27_);
		kkc_language_model_entry_destroy (&entry);
		input_output = (_vala_array_free (input_output, input_output_length1, (GDestroyNotify) g_free), NULL);
		_g_object_unref0 (key);
	}
	result = (GeeCollection*) entries;
	_g_free0 (query);
	_g_object_unref0 (agent);
	return result;
}

static GeeCollection*
kkc_sorted_bigram_language_model_real_entries (KkcLanguageModel* base,
                                               const gchar* input)
{
	KkcSortedBigramLanguageModel * self;
	GeeArrayList* entries = NULL;
	GeeArrayList* _tmp0_;
	MarisaAgent* agent = NULL;
	MarisaAgent* _tmp1_;
	MarisaAgent* _tmp2_;
	guint8* _tmp3_;
	gint _tmp3__length1;
	gint _tmp4_ = 0;
	guint8* _tmp5_;
	gint _tmp5__length1;
	GeeCollection* result;
	self = (KkcSortedBigramLanguageModel*) base;
	g_return_val_if_fail (input != NULL, NULL);
	_tmp0_ = gee_array_list_new (KKC_TYPE_LANGUAGE_MODEL_ENTRY, (GBoxedCopyFunc) kkc_language_model_entry_dup, (GDestroyNotify) kkc_language_model_entry_free, NULL, NULL, NULL);
	entries = _tmp0_;
	_tmp1_ = marisa_agent_new ();
	agent = _tmp1_;
	_tmp2_ = agent;
	_tmp3_ = string_get_data (input, &_tmp4_);
	_tmp3__length1 = _tmp4_;
	_tmp5_ = _tmp3_;
	_tmp5__length1 = _tmp3__length1;
	marisa_agent_set_query (_tmp2_, (const gchar*) _tmp5_, (gsize) _tmp5__length1);
	while (TRUE) {
		MarisaTrie* _tmp6_;
		MarisaAgent* _tmp7_;
		gchar* prefix = NULL;
		MarisaAgent* _tmp8_;
		MarisaKey* _tmp9_;
		MarisaKey* _tmp10_;
		gchar* _tmp11_;
		gchar* _tmp12_;
		GeeArrayList* _tmp13_;
		const gchar* _tmp14_;
		GeeCollection* _tmp15_;
		GeeCollection* _tmp16_;
		_tmp6_ = self->priv->input_trie;
		_tmp7_ = agent;
		if (!marisa_trie_common_prefix_search (_tmp6_, _tmp7_)) {
			break;
		}
		_tmp8_ = agent;
		_tmp9_ = marisa_agent_get_key (_tmp8_);
		_tmp10_ = _tmp9_;
		_tmp11_ = marisa_key_get_string (_tmp10_, NULL);
		_tmp12_ = _tmp11_;
		_g_object_unref0 (_tmp10_);
		prefix = _tmp12_;
		_tmp13_ = entries;
		_tmp14_ = prefix;
		_tmp15_ = kkc_language_model_unigram_entries ((KkcLanguageModel*) self, _tmp14_);
		_tmp16_ = _tmp15_;
		gee_array_list_add_all (_tmp13_, _tmp16_);
		_g_object_unref0 (_tmp16_);
		_g_free0 (prefix);
	}
	result = (GeeCollection*) entries;
	_g_object_unref0 (agent);
	return result;
}

static gpointer
_kkc_language_model_entry_dup0 (gpointer self)
{
	return self ? kkc_language_model_entry_dup (self) : NULL;
}

static KkcLanguageModelEntry*
kkc_sorted_bigram_language_model_real_get (KkcLanguageModel* base,
                                           const gchar* input,
                                           const gchar* output)
{
	KkcSortedBigramLanguageModel * self;
	MarisaAgent* agent = NULL;
	MarisaAgent* _tmp0_;
	gchar* query = NULL;
	MarisaAgent* _tmp3_;
	const gchar* _tmp4_;
	guint8* _tmp5_;
	gint _tmp5__length1;
	gint _tmp6_ = 0;
	guint8* _tmp7_;
	gint _tmp7__length1;
	MarisaTrie* _tmp8_;
	MarisaAgent* _tmp9_;
	KkcLanguageModelEntry* result;
	self = (KkcSortedBigramLanguageModel*) base;
	g_return_val_if_fail (input != NULL, NULL);
	g_return_val_if_fail (output != NULL, NULL);
	_tmp0_ = marisa_agent_new ();
	agent = _tmp0_;
	if (g_strcmp0 (input, " ") != 0) {
		gchar* _tmp1_;
		_tmp1_ = g_strdup_printf ("%s/%s", input, output);
		_g_free0 (query);
		query = _tmp1_;
	} else {
		gchar* _tmp2_;
		_tmp2_ = g_strdup (output);
		_g_free0 (query);
		query = _tmp2_;
	}
	_tmp3_ = agent;
	_tmp4_ = query;
	_tmp5_ = string_get_data (_tmp4_, &_tmp6_);
	_tmp5__length1 = _tmp6_;
	_tmp7_ = _tmp5_;
	_tmp7__length1 = _tmp5__length1;
	marisa_agent_set_query (_tmp3_, (const gchar*) _tmp7_, (gsize) _tmp7__length1);
	_tmp8_ = self->priv->unigram_trie;
	_tmp9_ = agent;
	if (marisa_trie_lookup (_tmp8_, _tmp9_)) {
		gsize id = 0UL;
		MarisaAgent* _tmp10_;
		MarisaKey* _tmp11_;
		MarisaKey* _tmp12_;
		gsize _tmp13_;
		KkcLanguageModelEntry entry = {0};
		gchar* _tmp14_;
		gchar* _tmp15_;
		KkcLanguageModelEntry _tmp16_ = {0};
		KkcLanguageModelEntry _tmp17_;
		KkcLanguageModelEntry* _tmp18_;
		KkcLanguageModelEntry* _tmp19_;
		_tmp10_ = agent;
		_tmp11_ = marisa_agent_get_key (_tmp10_);
		_tmp12_ = _tmp11_;
		_tmp13_ = marisa_key_get_id (_tmp12_);
		_g_object_unref0 (_tmp12_);
		id = _tmp13_;
		_tmp14_ = g_strdup (input);
		_tmp15_ = g_strdup (output);
		_g_free0 (_tmp16_.input);
		_tmp16_.input = _tmp14_;
		_g_free0 (_tmp16_.output);
		_tmp16_.output = _tmp15_;
		_tmp16_.id = (guint) id;
		entry = _tmp16_;
		_tmp17_ = entry;
		_tmp18_ = _kkc_language_model_entry_dup0 (&_tmp17_);
		_tmp19_ = _tmp18_;
		kkc_language_model_entry_destroy (&_tmp17_);
		result = _tmp19_;
		_g_free0 (query);
		_g_object_unref0 (agent);
		return result;
	}
	result = NULL;
	_g_free0 (query);
	_g_object_unref0 (agent);
	return result;
}

glong
kkc_sorted_bigram_language_model_bigram_offset (KkcSortedBigramLanguageModel* self,
                                                KkcLanguageModelEntry* pentry,
                                                KkcLanguageModelEntry* entry)
{
	gboolean _tmp0_ = FALSE;
	KkcLanguageModelEntry _tmp1_;
	gboolean _tmp3_ = FALSE;
	KkcBloomFilter* _tmp4_;
	guint8* buffer = NULL;
	guint8* _tmp8_;
	gint buffer_length1;
	gint _buffer_size_;
	guint8* p = NULL;
	guint8* _tmp9_;
	gint _tmp9__length1;
	guint32 value = 0U;
	KkcLanguageModelEntry _tmp10_;
	guint8* _tmp11_;
	guint8* _tmp12_;
	guint32 pvalue = 0U;
	KkcLanguageModelEntry _tmp13_;
	guint8* _tmp14_;
	gint record_size = 0;
	glong offset = 0L;
	KkcIndexFile* _tmp15_;
	gchar* _tmp16_;
	KkcIndexFile* _tmp17_;
	guint8* _tmp18_;
	gint _tmp18__length1;
	KkcLanguageModelEntry _tmp19_;
	KkcLanguageModelEntry _tmp20_;
	glong result;
	g_return_val_if_fail (self != NULL, -1L);
	g_return_val_if_fail (pentry != NULL, -1L);
	g_return_val_if_fail (entry != NULL, -1L);
	_tmp1_ = *pentry;
	if (_tmp1_.id == ((guint) self->priv->last_pvalue)) {
		KkcLanguageModelEntry _tmp2_;
		_tmp2_ = *entry;
		_tmp0_ = _tmp2_.id == ((guint) self->priv->last_value);
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		result = self->priv->last_offset;
		return result;
	}
	_tmp4_ = self->priv->bigram_filter;
	if (_tmp4_ != NULL) {
		KkcBloomFilter* _tmp5_;
		KkcLanguageModelEntry _tmp6_;
		KkcLanguageModelEntry _tmp7_;
		_tmp5_ = self->priv->bigram_filter;
		_tmp6_ = *entry;
		_tmp7_ = *pentry;
		_tmp3_ = !kkc_bloom_filter_contains (_tmp5_, (guint32) _tmp6_.id, (guint32) _tmp7_.id);
	} else {
		_tmp3_ = FALSE;
	}
	if (_tmp3_) {
		result = (glong) -1;
		return result;
	}
	_tmp8_ = g_new0 (guint8, 8);
	buffer = _tmp8_;
	buffer_length1 = 8;
	_buffer_size_ = buffer_length1;
	_tmp9_ = buffer;
	_tmp9__length1 = buffer_length1;
	p = _tmp9_;
	_tmp10_ = *entry;
	value = (guint32) _tmp10_.id;
	_tmp11_ = p;
	memcpy (_tmp11_, &value, (gsize) sizeof (guint32));
	_tmp12_ = p;
	p = _tmp12_ + 4;
	_tmp13_ = *pentry;
	pvalue = (guint32) _tmp13_.id;
	_tmp14_ = p;
	memcpy (_tmp14_, &pvalue, (gsize) sizeof (guint32));
	record_size = 12;
	_tmp15_ = self->priv->bigram_index;
	_tmp16_ = kkc_index_file_get_contents (_tmp15_);
	_tmp17_ = self->priv->bigram_index;
	_tmp18_ = buffer;
	_tmp18__length1 = buffer_length1;
	offset = kkc_language_model_utils_bsearch_ngram (_tmp16_, (glong) 0, ((glong) kkc_index_file_get_length (_tmp17_)) / record_size, (glong) record_size, _tmp18_, (gint) _tmp18__length1);
	_tmp19_ = *entry;
	self->priv->last_value = (guint32) _tmp19_.id;
	_tmp20_ = *pentry;
	self->priv->last_pvalue = (guint32) _tmp20_.id;
	self->priv->last_offset = offset;
	result = offset;
	buffer = (g_free (buffer), NULL);
	return result;
}

static gdouble
kkc_sorted_bigram_language_model_real_unigram_cost (KkcUnigramLanguageModel* base,
                                                    KkcLanguageModelEntry* entry)
{
	KkcSortedBigramLanguageModel * self;
	KkcLanguageModelEntry _tmp0_;
	KkcIndexFile* _tmp1_;
	guint8* p = NULL;
	KkcIndexFile* _tmp2_;
	gchar* _tmp3_;
	KkcLanguageModelEntry _tmp4_;
	guint16 cost = 0U;
	guint8* _tmp5_;
	gdouble _tmp6_;
	gdouble _tmp7_;
	gdouble result;
	self = (KkcSortedBigramLanguageModel*) base;
	g_return_val_if_fail (entry != NULL, -1.0);
	_tmp0_ = *entry;
	_tmp1_ = self->priv->unigram_index;
	if (((gsize) _tmp0_.id) >= kkc_index_file_get_length (_tmp1_)) {
		result = (gdouble) 0;
		return result;
	}
	_tmp2_ = self->priv->unigram_index;
	_tmp3_ = kkc_index_file_get_contents (_tmp2_);
	_tmp4_ = *entry;
	p = ((guint8*) _tmp3_) + (_tmp4_.id * 6);
	_tmp5_ = p;
	cost = *((guint16*) _tmp5_);
	_tmp6_ = kkc_sorted_bigram_language_model_get_min_cost (self);
	_tmp7_ = _tmp6_;
	result = kkc_language_model_utils_decode_cost (cost, _tmp7_);
	return result;
}

static gdouble
kkc_sorted_bigram_language_model_real_unigram_backoff (KkcUnigramLanguageModel* base,
                                                       KkcLanguageModelEntry* entry)
{
	KkcSortedBigramLanguageModel * self;
	KkcLanguageModelEntry _tmp0_;
	KkcIndexFile* _tmp1_;
	guint8* p = NULL;
	KkcIndexFile* _tmp2_;
	gchar* _tmp3_;
	KkcLanguageModelEntry _tmp4_;
	guint16 backoff = 0U;
	guint8* _tmp5_;
	gdouble _tmp6_;
	gdouble _tmp7_;
	gdouble result;
	self = (KkcSortedBigramLanguageModel*) base;
	g_return_val_if_fail (entry != NULL, -1.0);
	_tmp0_ = *entry;
	_tmp1_ = self->priv->unigram_index;
	if (((gsize) _tmp0_.id) >= kkc_index_file_get_length (_tmp1_)) {
		result = (gdouble) 0;
		return result;
	}
	_tmp2_ = self->priv->unigram_index;
	_tmp3_ = kkc_index_file_get_contents (_tmp2_);
	_tmp4_ = *entry;
	p = (((guint8*) _tmp3_) + (_tmp4_.id * 6)) + 2;
	_tmp5_ = p;
	backoff = *((guint16*) _tmp5_);
	_tmp6_ = kkc_sorted_bigram_language_model_get_min_cost (self);
	_tmp7_ = _tmp6_;
	result = kkc_language_model_utils_decode_cost (backoff, _tmp7_);
	return result;
}

static gboolean
kkc_sorted_bigram_language_model_real_has_bigram (KkcBigramLanguageModel* base,
                                                  KkcLanguageModelEntry* pentry,
                                                  KkcLanguageModelEntry* entry)
{
	KkcSortedBigramLanguageModel * self;
	KkcLanguageModelEntry _tmp0_;
	KkcLanguageModelEntry _tmp1_;
	gboolean result;
	self = (KkcSortedBigramLanguageModel*) base;
	g_return_val_if_fail (pentry != NULL, FALSE);
	g_return_val_if_fail (entry != NULL, FALSE);
	_tmp0_ = *pentry;
	_tmp1_ = *entry;
	result = kkc_sorted_bigram_language_model_bigram_offset (self, &_tmp0_, &_tmp1_) >= ((glong) 0);
	return result;
}

static gdouble
kkc_sorted_bigram_language_model_real_bigram_cost (KkcBigramLanguageModel* base,
                                                   KkcLanguageModelEntry* pentry,
                                                   KkcLanguageModelEntry* entry)
{
	KkcSortedBigramLanguageModel * self;
	glong offset = 0L;
	KkcLanguageModelEntry _tmp0_;
	KkcLanguageModelEntry _tmp1_;
	guint8* p = NULL;
	KkcIndexFile* _tmp2_;
	gchar* _tmp3_;
	guint16 cost = 0U;
	guint8* _tmp4_;
	gdouble _tmp5_;
	gdouble _tmp6_;
	gdouble result;
	self = (KkcSortedBigramLanguageModel*) base;
	g_return_val_if_fail (pentry != NULL, -1.0);
	g_return_val_if_fail (entry != NULL, -1.0);
	_tmp0_ = *pentry;
	_tmp1_ = *entry;
	offset = kkc_sorted_bigram_language_model_bigram_offset (self, &_tmp0_, &_tmp1_);
	if (offset < ((glong) 0)) {
		result = (gdouble) 0;
		return result;
	}
	_tmp2_ = self->priv->bigram_index;
	_tmp3_ = kkc_index_file_get_contents (_tmp2_);
	p = (((guint8*) _tmp3_) + (offset * 12)) + 8;
	_tmp4_ = p;
	cost = *((guint16*) _tmp4_);
	_tmp5_ = kkc_sorted_bigram_language_model_get_min_cost (self);
	_tmp6_ = _tmp5_;
	result = kkc_language_model_utils_decode_cost (cost, _tmp6_);
	return result;
}

static gdouble
kkc_sorted_bigram_language_model_real_bigram_backoff (KkcBigramLanguageModel* base,
                                                      KkcLanguageModelEntry* pentry,
                                                      KkcLanguageModelEntry* entry)
{
	KkcSortedBigramLanguageModel * self;
	glong offset = 0L;
	KkcLanguageModelEntry _tmp0_;
	KkcLanguageModelEntry _tmp1_;
	guint8* p = NULL;
	KkcIndexFile* _tmp2_;
	gchar* _tmp3_;
	guint16 backoff = 0U;
	guint8* _tmp4_;
	gdouble _tmp5_;
	gdouble _tmp6_;
	gdouble result;
	self = (KkcSortedBigramLanguageModel*) base;
	g_return_val_if_fail (pentry != NULL, -1.0);
	g_return_val_if_fail (entry != NULL, -1.0);
	_tmp0_ = *pentry;
	_tmp1_ = *entry;
	offset = kkc_sorted_bigram_language_model_bigram_offset (self, &_tmp0_, &_tmp1_);
	if (offset < ((glong) 0)) {
		result = (gdouble) 0;
		return result;
	}
	_tmp2_ = self->priv->bigram_index;
	_tmp3_ = kkc_index_file_get_contents (_tmp2_);
	p = (((guint8*) _tmp3_) + (offset * 12)) + 10;
	_tmp4_ = p;
	backoff = *((guint16*) _tmp4_);
	_tmp5_ = kkc_sorted_bigram_language_model_get_min_cost (self);
	_tmp6_ = _tmp5_;
	result = kkc_language_model_utils_decode_cost (backoff, _tmp6_);
	return result;
}

static gboolean
kkc_sorted_bigram_language_model_real_parse (KkcLanguageModel* base,
                                             GError** error)
{
	KkcSortedBigramLanguageModel * self;
	gchar* prefix = NULL;
	KkcLanguageModelMetadata* _tmp0_;
	KkcLanguageModelMetadata* _tmp1_;
	const gchar* _tmp2_;
	const gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	gchar* input_trie_filename = NULL;
	const gchar* _tmp8_;
	gchar* _tmp9_;
	MarisaTrie* _tmp10_;
	const gchar* _tmp11_;
	gchar* unigram_trie_filename = NULL;
	const gchar* _tmp13_;
	gchar* _tmp14_;
	MarisaTrie* _tmp15_;
	const gchar* _tmp16_;
	gchar* bigram_filter_filename = NULL;
	const gchar* _tmp50_;
	gchar* _tmp51_;
	KkcLanguageModelEntry* _tmp60_;
	KkcLanguageModelEntry* _tmp61_;
	KkcLanguageModelEntry _tmp62_;
	KkcLanguageModelEntry _tmp63_ = {0};
	KkcLanguageModelEntry* _tmp64_;
	KkcLanguageModelEntry* _tmp65_;
	KkcLanguageModelEntry _tmp66_;
	KkcLanguageModelEntry _tmp67_ = {0};
	GError* _inner_error0_ = NULL;
	gboolean result;
	self = (KkcSortedBigramLanguageModel*) base;
	_tmp0_ = kkc_language_model_get_metadata ((KkcLanguageModel*) self);
	_tmp1_ = _tmp0_;
	_tmp2_ = kkc_metadata_file_get_filename ((KkcMetadataFile*) _tmp1_);
	_tmp3_ = _tmp2_;
	_tmp4_ = g_path_get_dirname (_tmp3_);
	_tmp5_ = _tmp4_;
	_tmp6_ = g_build_filename (_tmp5_, "data", NULL);
	_tmp7_ = _tmp6_;
	_g_free0 (_tmp5_);
	prefix = _tmp7_;
	_tmp8_ = prefix;
	_tmp9_ = g_strconcat (_tmp8_, ".input", NULL);
	input_trie_filename = _tmp9_;
	_tmp10_ = self->priv->input_trie;
	_tmp11_ = input_trie_filename;
	marisa_trie_mmap (_tmp10_, _tmp11_, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gboolean _tmp12_ = FALSE;
		g_propagate_error (error, _inner_error0_);
		_g_free0 (input_trie_filename);
		_g_free0 (prefix);
		return _tmp12_;
	}
	_tmp13_ = prefix;
	_tmp14_ = g_strconcat (_tmp13_, ".1gram.index", NULL);
	unigram_trie_filename = _tmp14_;
	_tmp15_ = self->priv->unigram_trie;
	_tmp16_ = unigram_trie_filename;
	marisa_trie_mmap (_tmp15_, _tmp16_, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gboolean _tmp17_ = FALSE;
		g_propagate_error (error, _inner_error0_);
		_g_free0 (unigram_trie_filename);
		_g_free0 (input_trie_filename);
		_g_free0 (prefix);
		return _tmp17_;
	}
	if (kkc_use_mapped_index_file) {
		KkcMappedIndexFile* _tmp18_ = NULL;
		const gchar* _tmp19_;
		gchar* _tmp20_;
		gchar* _tmp21_;
		KkcMappedIndexFile* _tmp22_;
		KkcMappedIndexFile* _tmp23_;
		KkcMappedIndexFile* _tmp25_;
		KkcMappedIndexFile* _tmp26_ = NULL;
		const gchar* _tmp27_;
		gchar* _tmp28_;
		gchar* _tmp29_;
		KkcMappedIndexFile* _tmp30_;
		KkcMappedIndexFile* _tmp31_;
		KkcMappedIndexFile* _tmp33_;
		_tmp19_ = prefix;
		_tmp20_ = g_strconcat (_tmp19_, ".1gram", NULL);
		_tmp21_ = _tmp20_;
		_tmp22_ = kkc_mapped_index_file_new (_tmp21_, &_inner_error0_);
		_tmp23_ = _tmp22_;
		_g_free0 (_tmp21_);
		_tmp18_ = _tmp23_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			gboolean _tmp24_ = FALSE;
			g_propagate_error (error, _inner_error0_);
			_g_free0 (unigram_trie_filename);
			_g_free0 (input_trie_filename);
			_g_free0 (prefix);
			return _tmp24_;
		}
		_tmp25_ = _tmp18_;
		_tmp18_ = NULL;
		_g_object_unref0 (self->priv->unigram_index);
		self->priv->unigram_index = (KkcIndexFile*) _tmp25_;
		_tmp27_ = prefix;
		_tmp28_ = g_strconcat (_tmp27_, ".2gram", NULL);
		_tmp29_ = _tmp28_;
		_tmp30_ = kkc_mapped_index_file_new (_tmp29_, &_inner_error0_);
		_tmp31_ = _tmp30_;
		_g_free0 (_tmp29_);
		_tmp26_ = _tmp31_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			gboolean _tmp32_ = FALSE;
			g_propagate_error (error, _inner_error0_);
			_g_object_unref0 (_tmp18_);
			_g_free0 (unigram_trie_filename);
			_g_free0 (input_trie_filename);
			_g_free0 (prefix);
			return _tmp32_;
		}
		_tmp33_ = _tmp26_;
		_tmp26_ = NULL;
		_g_object_unref0 (self->priv->bigram_index);
		self->priv->bigram_index = (KkcIndexFile*) _tmp33_;
		_g_object_unref0 (_tmp26_);
		_g_object_unref0 (_tmp18_);
	} else {
		KkcLoadedIndexFile* _tmp34_ = NULL;
		const gchar* _tmp35_;
		gchar* _tmp36_;
		gchar* _tmp37_;
		KkcLoadedIndexFile* _tmp38_;
		KkcLoadedIndexFile* _tmp39_;
		KkcLoadedIndexFile* _tmp41_;
		KkcLoadedIndexFile* _tmp42_ = NULL;
		const gchar* _tmp43_;
		gchar* _tmp44_;
		gchar* _tmp45_;
		KkcLoadedIndexFile* _tmp46_;
		KkcLoadedIndexFile* _tmp47_;
		KkcLoadedIndexFile* _tmp49_;
		_tmp35_ = prefix;
		_tmp36_ = g_strconcat (_tmp35_, ".1gram", NULL);
		_tmp37_ = _tmp36_;
		_tmp38_ = kkc_loaded_index_file_new (_tmp37_, &_inner_error0_);
		_tmp39_ = _tmp38_;
		_g_free0 (_tmp37_);
		_tmp34_ = _tmp39_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			gboolean _tmp40_ = FALSE;
			g_propagate_error (error, _inner_error0_);
			_g_free0 (unigram_trie_filename);
			_g_free0 (input_trie_filename);
			_g_free0 (prefix);
			return _tmp40_;
		}
		_tmp41_ = _tmp34_;
		_tmp34_ = NULL;
		_g_object_unref0 (self->priv->unigram_index);
		self->priv->unigram_index = (KkcIndexFile*) _tmp41_;
		_tmp43_ = prefix;
		_tmp44_ = g_strconcat (_tmp43_, ".2gram", NULL);
		_tmp45_ = _tmp44_;
		_tmp46_ = kkc_loaded_index_file_new (_tmp45_, &_inner_error0_);
		_tmp47_ = _tmp46_;
		_g_free0 (_tmp45_);
		_tmp42_ = _tmp47_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			gboolean _tmp48_ = FALSE;
			g_propagate_error (error, _inner_error0_);
			_g_object_unref0 (_tmp34_);
			_g_free0 (unigram_trie_filename);
			_g_free0 (input_trie_filename);
			_g_free0 (prefix);
			return _tmp48_;
		}
		_tmp49_ = _tmp42_;
		_tmp42_ = NULL;
		_g_object_unref0 (self->priv->bigram_index);
		self->priv->bigram_index = (KkcIndexFile*) _tmp49_;
		_g_object_unref0 (_tmp42_);
		_g_object_unref0 (_tmp34_);
	}
	_tmp50_ = prefix;
	_tmp51_ = g_strconcat (_tmp50_, ".2gram.filter", NULL);
	bigram_filter_filename = _tmp51_;
	{
		KkcBloomFilter* _tmp52_ = NULL;
		const gchar* _tmp53_;
		KkcBloomFilter* _tmp54_;
		KkcBloomFilter* _tmp55_;
		_tmp53_ = bigram_filter_filename;
		_tmp54_ = kkc_bloom_filter_new (_tmp53_, &_inner_error0_);
		_tmp52_ = _tmp54_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp55_ = _tmp52_;
		_tmp52_ = NULL;
		_g_object_unref0 (self->priv->bigram_filter);
		self->priv->bigram_filter = _tmp55_;
		_g_object_unref0 (_tmp52_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		const gchar* _tmp56_;
		GError* _tmp57_;
		const gchar* _tmp58_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp56_ = bigram_filter_filename;
		_tmp57_ = e;
		_tmp58_ = _tmp57_->message;
		g_warning ("sorted-bigram-language-model.vala:200: can't load %s: %s", _tmp56_, _tmp58_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gboolean _tmp59_ = FALSE;
		g_propagate_error (error, _inner_error0_);
		_g_free0 (bigram_filter_filename);
		_g_free0 (unigram_trie_filename);
		_g_free0 (input_trie_filename);
		_g_free0 (prefix);
		return _tmp59_;
	}
	_tmp60_ = kkc_language_model_get ((KkcLanguageModel*) self, " ", "<s>");
	_tmp61_ = _tmp60_;
	_tmp62_ = *_tmp61_;
	kkc_language_model_entry_copy (&_tmp62_, &_tmp63_);
	kkc_language_model_entry_destroy (&self->priv->_bos);
	self->priv->_bos = _tmp63_;
	_kkc_language_model_entry_free0 (_tmp61_);
	_tmp64_ = kkc_language_model_get ((KkcLanguageModel*) self, " ", "</s>");
	_tmp65_ = _tmp64_;
	_tmp66_ = *_tmp65_;
	kkc_language_model_entry_copy (&_tmp66_, &_tmp67_);
	kkc_language_model_entry_destroy (&self->priv->_eos);
	self->priv->_eos = _tmp67_;
	_kkc_language_model_entry_free0 (_tmp65_);
	result = TRUE;
	_g_free0 (bigram_filter_filename);
	_g_free0 (unigram_trie_filename);
	_g_free0 (input_trie_filename);
	_g_free0 (prefix);
	return result;
}

KkcSortedBigramLanguageModel*
kkc_sorted_bigram_language_model_construct (GType object_type,
                                            KkcLanguageModelMetadata* metadata,
                                            GError** error)
{
	KkcSortedBigramLanguageModel * self = NULL;
	GError* _inner_error0_ = NULL;
	g_return_val_if_fail (metadata != NULL, NULL);
	self = (KkcSortedBigramLanguageModel*) kkc_language_model_construct (object_type, metadata, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (self);
		return NULL;
	}
	return self;
}

KkcSortedBigramLanguageModel*
kkc_sorted_bigram_language_model_new (KkcLanguageModelMetadata* metadata,
                                      GError** error)
{
	return kkc_sorted_bigram_language_model_construct (KKC_TYPE_SORTED_BIGRAM_LANGUAGE_MODEL, metadata, error);
}

static void
kkc_sorted_bigram_language_model_real_get_bos (KkcLanguageModel* base,
                                               KkcLanguageModelEntry * result)
{
	KkcSortedBigramLanguageModel* self;
	KkcLanguageModelEntry _tmp0_;
	self = (KkcSortedBigramLanguageModel*) base;
	_tmp0_ = self->priv->_bos;
	*result = _tmp0_;
	return;
}

static void
kkc_sorted_bigram_language_model_real_get_eos (KkcLanguageModel* base,
                                               KkcLanguageModelEntry * result)
{
	KkcSortedBigramLanguageModel* self;
	KkcLanguageModelEntry _tmp0_;
	self = (KkcSortedBigramLanguageModel*) base;
	_tmp0_ = self->priv->_eos;
	*result = _tmp0_;
	return;
}

gdouble
kkc_sorted_bigram_language_model_get_min_cost (KkcSortedBigramLanguageModel* self)
{
	gdouble result;
	g_return_val_if_fail (self != NULL, -1.0);
	result = -8.0;
	return result;
}

static void
kkc_sorted_bigram_language_model_class_init (KkcSortedBigramLanguageModelClass * klass,
                                             gpointer klass_data)
{
	kkc_sorted_bigram_language_model_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &KkcSortedBigramLanguageModel_private_offset);
	((KkcLanguageModelClass *) klass)->unigram_entries = (GeeCollection* (*) (KkcLanguageModel*, const gchar*)) kkc_sorted_bigram_language_model_real_unigram_entries;
	((KkcLanguageModelClass *) klass)->entries = (GeeCollection* (*) (KkcLanguageModel*, const gchar*)) kkc_sorted_bigram_language_model_real_entries;
	((KkcLanguageModelClass *) klass)->get = (KkcLanguageModelEntry* (*) (KkcLanguageModel*, const gchar*, const gchar*)) kkc_sorted_bigram_language_model_real_get;
	((KkcLanguageModelClass *) klass)->parse = (gboolean (*) (KkcLanguageModel*, GError**)) kkc_sorted_bigram_language_model_real_parse;
	KKC_LANGUAGE_MODEL_CLASS (klass)->get_bos = (void (*) (KkcLanguageModel*, KkcLanguageModelEntry*)) kkc_sorted_bigram_language_model_real_get_bos;
	KKC_LANGUAGE_MODEL_CLASS (klass)->get_eos = (void (*) (KkcLanguageModel*, KkcLanguageModelEntry*)) kkc_sorted_bigram_language_model_real_get_eos;
	G_OBJECT_CLASS (klass)->get_property = _vala_kkc_sorted_bigram_language_model_get_property;
	G_OBJECT_CLASS (klass)->finalize = kkc_sorted_bigram_language_model_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), KKC_SORTED_BIGRAM_LANGUAGE_MODEL_BOS_PROPERTY, kkc_sorted_bigram_language_model_properties[KKC_SORTED_BIGRAM_LANGUAGE_MODEL_BOS_PROPERTY] = g_param_spec_boxed ("bos", "bos", "bos", KKC_TYPE_LANGUAGE_MODEL_ENTRY, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), KKC_SORTED_BIGRAM_LANGUAGE_MODEL_EOS_PROPERTY, kkc_sorted_bigram_language_model_properties[KKC_SORTED_BIGRAM_LANGUAGE_MODEL_EOS_PROPERTY] = g_param_spec_boxed ("eos", "eos", "eos", KKC_TYPE_LANGUAGE_MODEL_ENTRY, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), KKC_SORTED_BIGRAM_LANGUAGE_MODEL_MIN_COST_PROPERTY, kkc_sorted_bigram_language_model_properties[KKC_SORTED_BIGRAM_LANGUAGE_MODEL_MIN_COST_PROPERTY] = g_param_spec_double ("min-cost", "min-cost", "min-cost", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
}

static void
kkc_sorted_bigram_language_model_kkc_unigram_language_model_interface_init (KkcUnigramLanguageModelIface * iface,
                                                                            gpointer iface_data)
{
	kkc_sorted_bigram_language_model_kkc_unigram_language_model_parent_iface = g_type_interface_peek_parent (iface);
	iface->unigram_cost = (gdouble (*) (KkcUnigramLanguageModel*, KkcLanguageModelEntry*)) kkc_sorted_bigram_language_model_real_unigram_cost;
	iface->unigram_backoff = (gdouble (*) (KkcUnigramLanguageModel*, KkcLanguageModelEntry*)) kkc_sorted_bigram_language_model_real_unigram_backoff;
}

static void
kkc_sorted_bigram_language_model_kkc_bigram_language_model_interface_init (KkcBigramLanguageModelIface * iface,
                                                                           gpointer iface_data)
{
	kkc_sorted_bigram_language_model_kkc_bigram_language_model_parent_iface = g_type_interface_peek_parent (iface);
	iface->has_bigram = (gboolean (*) (KkcBigramLanguageModel*, KkcLanguageModelEntry*, KkcLanguageModelEntry*)) kkc_sorted_bigram_language_model_real_has_bigram;
	iface->bigram_cost = (gdouble (*) (KkcBigramLanguageModel*, KkcLanguageModelEntry*, KkcLanguageModelEntry*)) kkc_sorted_bigram_language_model_real_bigram_cost;
	iface->bigram_backoff = (gdouble (*) (KkcBigramLanguageModel*, KkcLanguageModelEntry*, KkcLanguageModelEntry*)) kkc_sorted_bigram_language_model_real_bigram_backoff;
}

static void
kkc_sorted_bigram_language_model_instance_init (KkcSortedBigramLanguageModel * self,
                                                gpointer klass)
{
	MarisaTrie* _tmp0_;
	MarisaTrie* _tmp1_;
	self->priv = kkc_sorted_bigram_language_model_get_instance_private (self);
	_tmp0_ = marisa_trie_new ();
	self->priv->input_trie = _tmp0_;
	_tmp1_ = marisa_trie_new ();
	self->priv->unigram_trie = _tmp1_;
	self->priv->bigram_filter = NULL;
	self->priv->last_value = (guint32) 0;
	self->priv->last_pvalue = (guint32) 0;
	self->priv->last_offset = (glong) 0;
}

static void
kkc_sorted_bigram_language_model_finalize (GObject * obj)
{
	KkcSortedBigramLanguageModel * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, KKC_TYPE_SORTED_BIGRAM_LANGUAGE_MODEL, KkcSortedBigramLanguageModel);
	kkc_language_model_entry_destroy (&self->priv->_bos);
	kkc_language_model_entry_destroy (&self->priv->_eos);
	_g_object_unref0 (self->priv->input_trie);
	_g_object_unref0 (self->priv->unigram_trie);
	_g_object_unref0 (self->priv->unigram_index);
	_g_object_unref0 (self->priv->bigram_index);
	_g_object_unref0 (self->priv->bigram_filter);
	G_OBJECT_CLASS (kkc_sorted_bigram_language_model_parent_class)->finalize (obj);
}

static GType
kkc_sorted_bigram_language_model_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (KkcSortedBigramLanguageModelClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) kkc_sorted_bigram_language_model_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (KkcSortedBigramLanguageModel), 0, (GInstanceInitFunc) kkc_sorted_bigram_language_model_instance_init, NULL };
	static const GInterfaceInfo kkc_unigram_language_model_info = { (GInterfaceInitFunc) kkc_sorted_bigram_language_model_kkc_unigram_language_model_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	static const GInterfaceInfo kkc_bigram_language_model_info = { (GInterfaceInitFunc) kkc_sorted_bigram_language_model_kkc_bigram_language_model_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType kkc_sorted_bigram_language_model_type_id;
	kkc_sorted_bigram_language_model_type_id = g_type_register_static (KKC_TYPE_LANGUAGE_MODEL, "KkcSortedBigramLanguageModel", &g_define_type_info, 0);
	g_type_add_interface_static (kkc_sorted_bigram_language_model_type_id, KKC_TYPE_UNIGRAM_LANGUAGE_MODEL, &kkc_unigram_language_model_info);
	g_type_add_interface_static (kkc_sorted_bigram_language_model_type_id, KKC_TYPE_BIGRAM_LANGUAGE_MODEL, &kkc_bigram_language_model_info);
	KkcSortedBigramLanguageModel_private_offset = g_type_add_instance_private (kkc_sorted_bigram_language_model_type_id, sizeof (KkcSortedBigramLanguageModelPrivate));
	return kkc_sorted_bigram_language_model_type_id;
}

GType
kkc_sorted_bigram_language_model_get_type (void)
{
	static volatile gsize kkc_sorted_bigram_language_model_type_id__once = 0;
	if (g_once_init_enter (&kkc_sorted_bigram_language_model_type_id__once)) {
		GType kkc_sorted_bigram_language_model_type_id;
		kkc_sorted_bigram_language_model_type_id = kkc_sorted_bigram_language_model_get_type_once ();
		g_once_init_leave (&kkc_sorted_bigram_language_model_type_id__once, kkc_sorted_bigram_language_model_type_id);
	}
	return kkc_sorted_bigram_language_model_type_id__once;
}

static void
_vala_kkc_sorted_bigram_language_model_get_property (GObject * object,
                                                     guint property_id,
                                                     GValue * value,
                                                     GParamSpec * pspec)
{
	KkcSortedBigramLanguageModel * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, KKC_TYPE_SORTED_BIGRAM_LANGUAGE_MODEL, KkcSortedBigramLanguageModel);
	switch (property_id) {
		case KKC_SORTED_BIGRAM_LANGUAGE_MODEL_BOS_PROPERTY:
		{
			KkcLanguageModelEntry boxed;
			kkc_language_model_get_bos ((KkcLanguageModel*) self, &boxed);
			g_value_set_boxed (value, &boxed);
		}
		break;
		case KKC_SORTED_BIGRAM_LANGUAGE_MODEL_EOS_PROPERTY:
		{
			KkcLanguageModelEntry boxed;
			kkc_language_model_get_eos ((KkcLanguageModel*) self, &boxed);
			g_value_set_boxed (value, &boxed);
		}
		break;
		case KKC_SORTED_BIGRAM_LANGUAGE_MODEL_MIN_COST_PROPERTY:
		g_value_set_double (value, kkc_sorted_bigram_language_model_get_min_cost (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

static gssize
_vala_array_length (gpointer array)
{
	gssize length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}

