static void
etsv_add_all   (ETableSubsetVariable *etssv)
{
	ETableModel *etm = E_TABLE_MODEL(etssv);
	ETableSubset *etss = E_TABLE_SUBSET(etssv);
	ETableSortedVariable *etsv = E_TABLE_SORTED_VARIABLE (etssv);
	int rows;
	int i;

	e_table_model_pre_change(etm);

	rows = e_table_model_row_count(etss->source);

	if (etss->n_map + rows > etssv->n_vals_allocated){
		etssv->n_vals_allocated += MAX(INCREMENT_AMOUNT, rows);
		etss->map_table = g_realloc (etss->map_table, etssv->n_vals_allocated * sizeof(int));
	}
	for (i = 0; i < rows; i++)
		etss->map_table[etss->n_map++] = i;

	if (etsv->sort_idle_id == 0) {
		etsv->sort_idle_id = g_idle_add_full(50, (GSourceFunc) etsv_sort_idle, etsv, NULL);
	}

	e_table_model_changed (etm);
}
static void
etssv_add_all (ETableSubsetVariable *etssv)
{
	ETableModel *etm = E_TABLE_MODEL (etssv);
	ETableSubset *etss = E_TABLE_SUBSET (etssv);
	ETableModel *source_model;
	gint rows;
	gint i;

	e_table_model_pre_change (etm);

	source_model = e_table_subset_get_source_model (etss);
	rows = e_table_model_row_count (source_model);

	if (etss->n_map + rows > etssv->n_vals_allocated) {
		etssv->n_vals_allocated += MAX (INCREMENT_AMOUNT, rows);
		etss->map_table = g_realloc (
			etss->map_table,
			etssv->n_vals_allocated * sizeof (gint));
	}
	for (i = 0; i < rows; i++)
		etss->map_table[etss->n_map++] = i;

	e_table_model_changed (etm);
}
static void
etsv_sort(ETableSortedVariable *etsv)
{
	ETableSubset *etss = E_TABLE_SUBSET(etsv);
	static int reentering = 0;
	if (reentering)
		return;
	reentering = 1;

	e_table_model_pre_change(E_TABLE_MODEL(etsv));

	e_table_sorting_utils_sort(etss->source, etsv->sort_info, etsv->full_header, etss->map_table, etss->n_map);

	e_table_model_changed (E_TABLE_MODEL(etsv));
	reentering = 0;
}
static void
etssv_add (ETableSubsetVariable *etssv,
           gint row)
{
	ETableModel *etm = E_TABLE_MODEL (etssv);
	ETableSubset *etss = E_TABLE_SUBSET (etssv);

	e_table_model_pre_change (etm);

	if (etss->n_map + 1 > etssv->n_vals_allocated) {
		etssv->n_vals_allocated += INCREMENT_AMOUNT;
		etss->map_table = g_realloc (
			etss->map_table,
			etssv->n_vals_allocated * sizeof (gint));
	}

	etss->map_table[etss->n_map++] = row;

	e_table_model_row_inserted (etm, etss->n_map - 1);
}
static void
etssv_add_array (ETableSubsetVariable *etssv,
                 const gint *array,
                 gint count)
{
	ETableModel *etm = E_TABLE_MODEL (etssv);
	ETableSubset *etss = E_TABLE_SUBSET (etssv);
	gint i;

	e_table_model_pre_change (etm);

	if (etss->n_map + count > etssv->n_vals_allocated) {
		etssv->n_vals_allocated += MAX (INCREMENT_AMOUNT, count);
		etss->map_table = g_realloc (
			etss->map_table,
			etssv->n_vals_allocated * sizeof (gint));
	}
	for (i = 0; i < count; i++)
		etss->map_table[etss->n_map++] = array[i];

	e_table_model_changed (etm);
}
static gboolean
etssv_remove (ETableSubsetVariable *etssv,
              gint row)
{
	ETableModel *etm = E_TABLE_MODEL (etssv);
	ETableSubset *etss = E_TABLE_SUBSET (etssv);
	gint i;

	for (i = 0; i < etss->n_map; i++) {
		if (etss->map_table[i] == row) {
			e_table_model_pre_change (etm);
			memmove (
				etss->map_table + i,
				etss->map_table + i + 1,
				(etss->n_map - i - 1) * sizeof (gint));
			etss->n_map--;

			e_table_model_row_deleted (etm, i);
			return TRUE;
		}
	}
	return FALSE;
}
static void
etsv_add       (ETableSubsetVariable *etssv,
		gint                  row)
{
	ETableModel *etm = E_TABLE_MODEL(etssv);
	ETableSubset *etss = E_TABLE_SUBSET(etssv);
	ETableSortedVariable *etsv = E_TABLE_SORTED_VARIABLE (etssv);
	int i;

	e_table_model_pre_change (etm);

	if (etss->n_map + 1 > etssv->n_vals_allocated) {
		etssv->n_vals_allocated += INCREMENT_AMOUNT;
		etss->map_table = g_realloc (etss->map_table, (etssv->n_vals_allocated) * sizeof(int));
	}
	i = etss->n_map;
	if (etsv->sort_idle_id == 0) {
		/* this is to see if we're inserting a lot of things between idle loops.
		   If we are, we're busy, its faster to just append and perform a full sort later */
		etsv->insert_count++;
		if (etsv->insert_count > ETSV_INSERT_MAX) {
			/* schedule a sort, and append instead */
			etsv->sort_idle_id = g_idle_add_full(50, (GSourceFunc) etsv_sort_idle, etsv, NULL);
		} else {
			/* make sure we have an idle handler to reset the count every now and then */
			if (etsv->insert_idle_id == 0) {
				etsv->insert_idle_id = g_idle_add_full(40, (GSourceFunc) etsv_insert_idle, etsv, NULL);
			}
			i = e_table_sorting_utils_insert(etss->source, etsv->sort_info, etsv->full_header, etss->map_table, etss->n_map, row);
			memmove(etss->map_table + i + 1, etss->map_table + i, (etss->n_map - i) * sizeof(int));
		}
	}
	etss->map_table[i] = row;
	etss->n_map++;

	e_table_model_row_inserted (etm, i);
}