ClutterListModel: Implement get_n_rows for improved performance

The default implementation in ClutterModel iterates through all
the elements.

http://bugzilla.clutter-project.org/show_bug.cgi?id=2511
This commit is contained in:
Tomeu Vizoso 2011-01-10 19:13:03 +01:00 committed by Emmanuele Bassi
parent 850f956d4e
commit 3625adb9d8
2 changed files with 41 additions and 28 deletions

View File

@ -621,6 +621,18 @@ clutter_list_model_resort (ClutterModel *model,
&sort_closure); &sort_closure);
} }
static guint
clutter_list_model_get_n_rows (ClutterModel *model)
{
ClutterListModel *list_model = CLUTTER_LIST_MODEL (model);
/* short-circuit in case we don't have a filter in place */
if (!clutter_model_get_filter_set (model))
return g_sequence_get_length (list_model->priv->sequence);
return CLUTTER_MODEL_CLASS (clutter_list_model_parent_class)->get_n_rows (model);
}
static void static void
clutter_list_model_row_removed (ClutterModel *model, clutter_list_model_row_removed (ClutterModel *model,
ClutterModelIter *iter) ClutterModelIter *iter)
@ -686,6 +698,7 @@ clutter_list_model_class_init (ClutterListModelClass *klass)
model_class->insert_row = clutter_list_model_insert_row; model_class->insert_row = clutter_list_model_insert_row;
model_class->remove_row = clutter_list_model_remove_row; model_class->remove_row = clutter_list_model_remove_row;
model_class->resort = clutter_list_model_resort; model_class->resort = clutter_list_model_resort;
model_class->get_n_rows = clutter_list_model_get_n_rows;
model_class->row_removed = clutter_list_model_row_removed; model_class->row_removed = clutter_list_model_row_removed;
} }

View File

@ -252,6 +252,32 @@ clutter_model_real_get_n_columns (ClutterModel *model)
return priv->n_columns; return priv->n_columns;
} }
static guint
clutter_model_real_get_n_rows (ClutterModel *model)
{
ClutterModelIter *iter;
guint row_count;
g_return_val_if_fail (CLUTTER_IS_MODEL (model), 0);
iter = clutter_model_get_first_iter (model);
if (iter == NULL)
return 0;
row_count = 0;
while (!clutter_model_iter_is_last (iter))
{
if (clutter_model_filter_iter (model, iter))
row_count += 1;
iter = clutter_model_iter_next (iter);
}
g_object_unref (iter);
return row_count;
}
static void static void
clutter_model_finalize (GObject *object) clutter_model_finalize (GObject *object)
{ {
@ -314,6 +340,7 @@ clutter_model_class_init (ClutterModelClass *klass)
klass->get_column_name = clutter_model_real_get_column_name; klass->get_column_name = clutter_model_real_get_column_name;
klass->get_column_type = clutter_model_real_get_column_type; klass->get_column_type = clutter_model_real_get_column_type;
klass->get_n_columns = clutter_model_real_get_n_columns; klass->get_n_columns = clutter_model_real_get_n_columns;
klass->get_n_rows = clutter_model_real_get_n_rows;
/** /**
* ClutterModel:filter-set: * ClutterModel:filter-set:
@ -1373,38 +1400,11 @@ clutter_model_get_last_iter (ClutterModel *model)
guint guint
clutter_model_get_n_rows (ClutterModel *model) clutter_model_get_n_rows (ClutterModel *model)
{ {
ClutterModelClass *klass;
guint row_count;
g_return_val_if_fail (CLUTTER_IS_MODEL (model), 0); g_return_val_if_fail (CLUTTER_IS_MODEL (model), 0);
klass = CLUTTER_MODEL_GET_CLASS (model); return CLUTTER_MODEL_GET_CLASS (model)->get_n_rows (model);
if (klass->get_n_rows)
row_count = klass->get_n_rows (model);
else
{
ClutterModelIter *iter;
iter = clutter_model_get_first_iter (model);
if (iter == NULL)
return 0;
row_count = 0;
while (!clutter_model_iter_is_last (iter))
{
if (clutter_model_filter_iter (model, iter))
row_count += 1;
iter = clutter_model_iter_next (iter);
}
g_object_unref (iter);
}
return row_count;
} }
/** /**
* clutter_model_set_sorting_column: * clutter_model_set_sorting_column:
* @model: a #ClutterModel * @model: a #ClutterModel