[anjuta] sourceview: Optimize iterator to avoid usage of GtkTextMark



commit dcb94ce70bc62e048c0413101873cd54a468c5a0
Author: Johannes Schmid <jhs gnome org>
Date:   Sun Mar 7 22:53:09 2010 +0100

    sourceview: Optimize iterator to avoid usage of GtkTextMark
    
    This should about double the speed of iterators

 plugins/sourceview/sourceview-cell.c |  131 ++++++++++++++++------------------
 1 files changed, 63 insertions(+), 68 deletions(-)
---
diff --git a/plugins/sourceview/sourceview-cell.c b/plugins/sourceview/sourceview-cell.c
index 588ede9..b48aa04 100644
--- a/plugins/sourceview/sourceview-cell.c
+++ b/plugins/sourceview/sourceview-cell.c
@@ -39,13 +39,40 @@ static void sourceview_cell_instance_init(SourceviewCell *sp);
 static void sourceview_cell_finalize(GObject *object);
 
 struct _SourceviewCellPrivate {
-	GtkTextMark* mark;
 	GtkTextView* view;
 	GtkTextBuffer* buffer;
+
+	gint offset;
 };
 
 static gpointer sourceview_cell_parent_class = NULL;
 
+/**
+ * sourceview_cell_update_offset:
+ * @cell: self
+ *
+ * Update the internal offset
+ */
+static void
+sourceview_cell_update_offset (SourceviewCell* cell, GtkTextIter* iter)
+{
+	cell->priv->offset = gtk_text_iter_get_offset (iter);
+}
+
+/**
+ * sourceview_cell_get_iter:
+ * @cell: self
+ * @iter: iterator to set
+ * Get the iterator for cell
+ */
+void
+sourceview_cell_get_iter (SourceviewCell* cell, GtkTextIter* iter)
+{
+	gtk_text_buffer_get_iter_at_offset (cell->priv->buffer,
+	                                    iter,
+	                                    cell->priv->offset);
+}
+
 static void
 sourceview_cell_class_init(SourceviewCellClass *klass)
 {
@@ -59,7 +86,7 @@ sourceview_cell_class_init(SourceviewCellClass *klass)
 static void
 sourceview_cell_instance_init(SourceviewCell *obj)
 {
-	obj->priv = g_slice_new(SourceviewCellPrivate);
+	obj->priv = g_slice_new0(SourceviewCellPrivate);
 	
 	/* Initialize private members, etc. */	
 }
@@ -70,7 +97,6 @@ sourceview_cell_finalize(GObject *object)
 	SourceviewCell *cobj;
 	cobj = SOURCEVIEW_CELL(object);
 
-	gtk_text_buffer_delete_mark (cobj->priv->buffer, cobj->priv->mark);
 	g_slice_free(SourceviewCellPrivate, cobj->priv);
 	G_OBJECT_CLASS(sourceview_cell_parent_class)->finalize(object);
 }
@@ -81,26 +107,20 @@ sourceview_cell_new(GtkTextIter* iter, GtkTextView* view)
 	SourceviewCell *obj;
 	
 	obj = SOURCEVIEW_CELL(g_object_new(SOURCEVIEW_TYPE_CELL, NULL));
-	
+
 	obj->priv->buffer = gtk_text_view_get_buffer(view);
-	obj->priv->mark = gtk_text_buffer_create_mark (obj->priv->buffer, NULL, iter, FALSE);
 	obj->priv->view = view;
+	obj->priv->offset = gtk_text_iter_get_offset (iter);
 	
 	return obj;
 }
 
-void
-sourceview_cell_get_iter (SourceviewCell* cell, GtkTextIter* iter)
-{
-	gtk_text_buffer_get_iter_at_mark (cell->priv->buffer, iter, cell->priv->mark);
-}
-
 static gchar*
 icell_get_character(IAnjutaEditorCell* icell, GError** e)
 {
 	SourceviewCell* cell = SOURCEVIEW_CELL(icell);
 	GtkTextIter iter;
-	gtk_text_buffer_get_iter_at_mark (cell->priv->buffer, &iter, cell->priv->mark);	
+	sourceview_cell_get_iter (cell, &iter);
 	gunichar c = gtk_text_iter_get_char (&iter);
 	gchar* outbuf = g_new0(gchar, 6);
 	g_unichar_to_utf8 (c, outbuf);
@@ -123,7 +143,7 @@ icell_get_char(IAnjutaEditorCell* icell, gint index, GError** e)
 {
 	SourceviewCell* cell = SOURCEVIEW_CELL(icell);
 	GtkTextIter iter;
-	gtk_text_buffer_get_iter_at_mark (cell->priv->buffer, &iter, cell->priv->mark);
+	sourceview_cell_get_iter (cell, &iter);
 	gunichar c = gtk_text_iter_get_char (&iter);
 	gchar* outbuf = g_new0(gchar, 6);
 	gint len = g_unichar_to_utf8 (c, outbuf);
@@ -140,9 +160,10 @@ static IAnjutaEditorAttribute
 icell_get_attribute (IAnjutaEditorCell* icell, GError **e)
 {
 	IAnjutaEditorAttribute attrib = IANJUTA_EDITOR_TEXT;
-	SourceviewCell* cell = SOURCEVIEW_CELL(icell);
 	GtkTextIter iter;
-	gtk_text_buffer_get_iter_at_mark (cell->priv->buffer, &iter, cell->priv->mark);
+	SourceviewCell* cell = SOURCEVIEW_CELL(icell);
+
+	sourceview_cell_get_iter (cell, &iter);
 
 	if (gtk_source_buffer_iter_has_context_class (GTK_SOURCE_BUFFER(cell->priv->buffer), &iter, "string"))
 		attrib = IANJUTA_EDITOR_STRING;
@@ -167,15 +188,8 @@ static gboolean
 iiter_first(IAnjutaIterable* iiter, GError** e)
 {
 	SourceviewCell* cell = SOURCEVIEW_CELL(iiter);
-	GtkTextIter iter;
-	gtk_text_buffer_get_iter_at_mark (cell->priv->buffer, &iter, cell->priv->mark);
-	gboolean retval = gtk_text_iter_is_start (&iter);
-	if (!retval)
-	{
-		gtk_text_iter_set_offset (&iter, 0);
-		gtk_text_buffer_move_mark (cell->priv->buffer, cell->priv->mark, &iter);
-	}
-	return retval;
+	cell->priv->offset = 0;
+	return TRUE;
 }
 
 static gboolean
@@ -183,10 +197,11 @@ iiter_next(IAnjutaIterable* iiter, GError** e)
 {
 	SourceviewCell* cell = SOURCEVIEW_CELL(iiter);
 	GtkTextIter iter;
-	gtk_text_buffer_get_iter_at_mark (cell->priv->buffer, &iter, cell->priv->mark);
-	gboolean retval = gtk_text_iter_forward_char(&iter);
-	if (retval)
-			gtk_text_buffer_move_mark (cell->priv->buffer, cell->priv->mark, &iter);
+	gboolean retval;
+	sourceview_cell_get_iter (cell, &iter);
+	retval = gtk_text_iter_forward_char (&iter);
+	sourceview_cell_update_offset (cell, &iter);
+
 	return retval;
 }
 
@@ -194,36 +209,28 @@ static gboolean
 iiter_previous(IAnjutaIterable* iiter, GError** e)
 {
 	SourceviewCell* cell = SOURCEVIEW_CELL(iiter);
-	GtkTextIter iter;
-	gtk_text_buffer_get_iter_at_mark (cell->priv->buffer, &iter, cell->priv->mark);
-	gboolean retval = gtk_text_iter_backward_char(&iter);
-	if (retval)
-			gtk_text_buffer_move_mark (cell->priv->buffer, cell->priv->mark, &iter);
-	return retval;
+	if (cell->priv->offset > 0)
+	{
+		cell->priv->offset--;
+		return TRUE;
+	}
+
+	return FALSE;
 }
 
 static gboolean
 iiter_last(IAnjutaIterable* iiter, GError** e)
 {
 	SourceviewCell* cell = SOURCEVIEW_CELL(iiter);
-	GtkTextIter iter;
-	gtk_text_buffer_get_iter_at_mark (cell->priv->buffer, &iter, cell->priv->mark);
-	gboolean retval = gtk_text_iter_is_end (&iter);
-	if (!retval)
-	{
-		gtk_text_iter_forward_to_end(&iter);
-		gtk_text_buffer_move_mark (cell->priv->buffer, cell->priv->mark, &iter);
-	}
-	return retval;
+	cell->priv->offset = -1;
+	return TRUE;
 }
 
 static void
 iiter_foreach(IAnjutaIterable* iiter, GFunc callback, gpointer data, GError** e)
 {
 	SourceviewCell* cell = SOURCEVIEW_CELL(iiter);
-	GtkTextIter iter;
-	gtk_text_buffer_get_iter_at_mark (cell->priv->buffer, &iter, cell->priv->mark);
-	GtkTextMark* cached = gtk_text_buffer_create_mark (cell->priv->buffer, NULL, &iter, FALSE);
+	gint old_offset = cell->priv->offset;
 	
 	iiter_first (IANJUTA_ITERABLE(cell), NULL);
 	
@@ -231,18 +238,14 @@ iiter_foreach(IAnjutaIterable* iiter, GFunc callback, gpointer data, GError** e)
 	{
 		(*callback)(cell, data);
 	}
-	gtk_text_buffer_delete_mark (cell->priv->buffer, cell->priv->mark);
-	cell->priv->mark = cached;
+	cell->priv->offset = old_offset;
 }
 
 static gboolean
 iiter_set_position (IAnjutaIterable* iiter, gint position, GError** e)
 {
 	SourceviewCell* cell = SOURCEVIEW_CELL(iiter);
-	GtkTextIter iter;
-	gtk_text_buffer_get_iter_at_mark (cell->priv->buffer, &iter, cell->priv->mark);
-	gtk_text_iter_set_offset(&iter, position);
-	gtk_text_buffer_move_mark (cell->priv->buffer, cell->priv->mark, &iter);
+	cell->priv->offset = position;
 	return TRUE;
 }
 
@@ -250,9 +253,7 @@ static gint
 iiter_get_position(IAnjutaIterable* iiter, GError** e)
 {
 	SourceviewCell* cell = SOURCEVIEW_CELL(iiter);
-	GtkTextIter iter;
-	gtk_text_buffer_get_iter_at_mark (cell->priv->buffer, &iter, cell->priv->mark);
-	return gtk_text_iter_get_offset(&iter);
+	return cell->priv->offset;
 }
 
 static gint
@@ -268,7 +269,7 @@ iiter_clone (IAnjutaIterable *iiter, GError **e)
 {
 	SourceviewCell* cell = SOURCEVIEW_CELL(iiter);
 	GtkTextIter iter;
-	gtk_text_buffer_get_iter_at_mark (cell->priv->buffer, &iter, cell->priv->mark);
+	sourceview_cell_get_iter (cell, &iter);
 	return IANJUTA_ITERABLE (sourceview_cell_new (&iter, cell->priv->view));
 }
 
@@ -277,9 +278,7 @@ iiter_assign (IAnjutaIterable *iiter, IAnjutaIterable *src_iter, GError **e)
 {
 	SourceviewCell* cell = SOURCEVIEW_CELL(iiter);
 	SourceviewCell* src_cell = SOURCEVIEW_CELL(src_iter);
-	GtkTextIter iter;
-	gtk_text_buffer_get_iter_at_mark (src_cell->priv->buffer, &iter, src_cell->priv->mark);
-	gtk_text_buffer_move_mark (cell->priv->buffer, cell->priv->mark, &iter);
+	cell->priv->offset = src_cell->priv->offset;
 }
 
 static gint
@@ -287,11 +286,13 @@ iiter_compare (IAnjutaIterable *iiter, IAnjutaIterable *iother_iter, GError **e)
 {
 	SourceviewCell* cell = SOURCEVIEW_CELL(iiter);
 	SourceviewCell* other_cell = SOURCEVIEW_CELL(iother_iter);
+
 	GtkTextIter iter;
 	GtkTextIter other_iter;
-	gtk_text_buffer_get_iter_at_mark (cell->priv->buffer, &iter, cell->priv->mark);
-	gtk_text_buffer_get_iter_at_mark (other_cell->priv->buffer, &other_iter, other_cell->priv->mark);
 
+	sourceview_cell_get_iter (cell, &iter);
+	sourceview_cell_get_iter (other_cell, &other_iter);
+	
 	return gtk_text_iter_compare (&iter, &other_iter);
 }
 
@@ -300,14 +301,8 @@ iiter_diff (IAnjutaIterable *iiter, IAnjutaIterable *iother_iter, GError **e)
 {
 	SourceviewCell* cell = SOURCEVIEW_CELL(iiter);
 	SourceviewCell* other_cell = SOURCEVIEW_CELL(iother_iter);
-
-	GtkTextIter iter;
-	GtkTextIter other_iter;
-	gtk_text_buffer_get_iter_at_mark (cell->priv->buffer, &iter, cell->priv->mark);
-	gtk_text_buffer_get_iter_at_mark (other_cell->priv->buffer, &other_iter, other_cell->priv->mark);
 	
-	return (gtk_text_iter_get_offset (&other_iter) 
-					- gtk_text_iter_get_offset (&iter));
+	return (other_cell->priv->offset - cell->priv->offset);
 }
 
 static void



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]