<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eto.Forms;
using System.Runtime.InteropServices;
using System.Collections;

namespace Eto.Platform.GtkSharp
{
	public class GtkTreeModel<T, S> : GLib.Object, Gtk.TreeModelImplementor
		where S: class, IDataStore<T>
		where T: class, ITreeItem<T>
	{

		public IGtkListModelHandler<T, S> Handler { get; set; }

		public GtkTreeModel ()
		{
		}
		
		class Node
		{
			public T Item { get; set; }

			public int[] Indices { get; set; }

		}
		
		Node GetNodeAtPath (Gtk.TreePath path)
		{
			if (path.Indices.Length > 0) {
				S item = (S)Handler.DataStore;
				for (int i = 0; i < path.Indices.Length; i++) {
					var idx = path.Indices [i];
					item = (S)(object)item [idx];
				}
				var node = new Node ();
				node.Item = (T)(object)item;
				node.Indices = path.Indices;
				return node;
			}
			return null;
		}
		
		public T GetItemAtPath (Gtk.TreePath path)
		{
			if (path.Indices.Length > 0) {
				var store = (S)Handler.DataStore;
				if (store != null) {
					for (int i = 0; i < path.Indices.Length; i++) {
						var idx = path.Indices [i];
						store = (S)(object)store [idx];
					}
					return (T)(object)store;
				}
			}
			return default(T);
		}

		public T GetItemAtPath (string path)
		{
			return GetItemAtPath (new Gtk.TreePath (path));
		}
		
		public Gtk.TreeIter GetIterFromItem (T item, Gtk.TreePath path)
		{
			GCHandle gch;
			var node = new Node{ Item = item, Indices = path.Indices };
			gch = GCHandle.Alloc (node);
			Gtk.TreeIter result = Gtk.TreeIter.Zero;
			result.UserData = (IntPtr)gch;
			return result;<commit_msg>Gtk: Make GtkTreeModel more resilient to errors<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eto.Forms;
using System.Runtime.InteropServices;
using System.Collections;

namespace Eto.Platform.GtkSharp
{
	public class GtkTreeModel<T, S> : GLib.Object, Gtk.TreeModelImplementor
		where S: class, IDataStore<T>
		where T: class, ITreeItem<T>
	{

		public IGtkListModelHandler<T, S> Handler { get; set; }

		public GtkTreeModel ()
		{
		}
		
		class Node
		{
			public T Item { get; set; }

			public int[] Indices { get; set; }

		}
		
		Node GetNodeAtPath (Gtk.TreePath path)
		{
			if (path.Indices.Length > 0) {
				S item = (S)Handler.DataStore;
				for (int i = 0; i < path.Indices.Length; i++) {
					var idx = path.Indices [i];
					item = (S)(object)item [idx];
				}
				var node = new Node ();
				node.Item = (T)(object)item;
				node.Indices = path.Indices;
				return node;
			}
			return null;
		}
		
		public T GetItemAtPath (Gtk.TreePath path)
		{
			if (path.Indices.Length > 0) {
				var store = (S)Handler.DataStore;
				if (store != null) {
					for (int i = 0; i < path.Indices.Length; i++) {
						var idx = path.Indices [i];
						if (idx < store.Count)
							store = (S)(object)store [idx];
						else
							return null;
					}
					return (T)(object)store;
				}
			}
			return default(T);
		}

		public T GetItemAtPath (string path)
		{
			return GetItemAtPath (new Gtk.TreePath (path));
		}
		
		public Gtk.TreeIter GetIterFromItem (T item, Gtk.TreePath path)
		{
			GCHandle gch;
			var node = new Node{ Item = item, Indices = path.Indices };
			gch = GCHandle.Alloc (node);
			Gtk.TreeIter result = Gtk.TreeIter.Zero;
			result.UserData = (IntPtr)gch;
			return result;
{'fraction_non_alphanumeric': 0.12861736334405144, 'fraction_numerical': 0.00233849751534639, 'mean_word_length': 2.832026875699888, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5896312', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1240, 'n_words': 321}