<commit_before>using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

using FSpot.Core;

namespace FSpot.Widgets
{
    public class SelectionCollection : IBrowsableCollection
    {
        IBrowsableCollection parent;
        Dictionary<IPhoto, int> selected_cells;
        BitArray bit_array;
        IPhoto [] items;
        IPhoto [] old;

        public SelectionCollection (IBrowsableCollection collection)
        {
            this.selected_cells = new Dictionary<IPhoto, int> ();
            this.parent = collection;
            this.bit_array = new BitArray (this.parent.Count);
            this.parent.Changed += HandleParentChanged;
            this.parent.ItemsChanged += HandleParentItemsChanged;
        }

        private void HandleParentChanged (IBrowsableCollection collection)
        {
            IPhoto [] local = old;
            selected_cells.Clear ();
            bit_array = new BitArray (parent.Count);
            ClearCached ();

            if (old != null) {
                int i = 0;

                for (i = 0; i < local.Length; i++) {
                    int parent_index = parent.IndexOf (local [i]);
                    if (parent_index >= 0)
                        this.Add (parent_index, false);
                }
            }

            // Call the directly so that we don't reset old immediately this way the old selection
            // set isn't actually lost until we change it.
            if (this.Changed != null)
                Changed (this);

            if (this.DetailedChanged != null)
                DetailedChanged (this, null);

        }

        public void MarkChanged (int item, IBrowsableItemChanges changes)
        {
            throw new NotImplementedException ();
        }

        private void HandleParentItemsChanged (IBrowsableCollection collection, BrowsableEventArgs args)
        {
            if (this.ItemsChanged == null)
                return;

            ArrayList local_ids = new ArrayList ();
            foreach (int parent_index in args.Items) {
                // If the item isn't part of the selection ignore it
                if (!this.Contains (collection [parent_index]))
                    return;

                int local_index = this.IndexOf (parent_index);
                if (local_index >= 0)
                    local_ids.Add (local_index);
            }

            if (local_ids.Count == 0)
                return;

            int [] items = (int [])local_ids.ToArray (typeof (int));
            ItemsChanged (this, new BrowsableEventArgs (items, args.Changes));
        }

        public BitArray ToBitArray () {
            return new BitArray (bit_array);
        }

        public int [] Ids {
            get {
                // TODO: use IEnumerable<>
                return (from i in selected_cells.Values orderby i select i).ToArray ();
            }
        }

        public IPhoto this [int index] {
            get {
                int [] ids = this.Ids;
                return parent [ids[index]];
            }
        }

        public IPhoto [] Items {
            get {
                if (items != null)
                    return items;

                int [] ids = this.Ids;
                items = new IPhoto [ids.Length];
                for (int i = 0; i < items.Length; i++) {
                    items [i] = parent [ids[i]];
                }
                return items;
            }
        }

        public void Clear ()
        {
            Clear (true);
        }

        public void Clear (bool update)
        {
            int [] ids = Ids;
            selected_cells.Clear ();
            bit_array.SetAll (false);
            SignalChange (ids);
        }

        public void Add (IPhoto item)
        {
            if (this.Contains (item))
                return;

            int index = parent.IndexOf (item);
            this.Add (index);
        }

        public int Count {
            get {
                return selected_cells.Count;
            }
        }

        public bool Contains (IPhoto item)
        {<commit_msg>Do not emit Change signal in SelectionCollection when it is not requested<commit_after>using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

using FSpot.Core;

namespace FSpot.Widgets
{
    public class SelectionCollection : IBrowsableCollection
    {
        IBrowsableCollection parent;
        Dictionary<IPhoto, int> selected_cells;
        BitArray bit_array;
        IPhoto [] items;
        IPhoto [] old;

        public SelectionCollection (IBrowsableCollection collection)
        {
            this.selected_cells = new Dictionary<IPhoto, int> ();
            this.parent = collection;
            this.bit_array = new BitArray (this.parent.Count);
            this.parent.Changed += HandleParentChanged;
            this.parent.ItemsChanged += HandleParentItemsChanged;
        }

        private void HandleParentChanged (IBrowsableCollection collection)
        {
            IPhoto [] local = old;
            selected_cells.Clear ();
            bit_array = new BitArray (parent.Count);
            ClearCached ();

            if (old != null) {
                int i = 0;

                for (i = 0; i < local.Length; i++) {
                    int parent_index = parent.IndexOf (local [i]);
                    if (parent_index >= 0)
                        this.Add (parent_index, false);
                }
            }

            // Call the directly so that we don't reset old immediately this way the old selection
            // set isn't actually lost until we change it.
            if (this.Changed != null)
                Changed (this);

            if (this.DetailedChanged != null)
                DetailedChanged (this, null);

        }

        public void MarkChanged (int item, IBrowsableItemChanges changes)
        {
            throw new NotImplementedException ();
        }

        private void HandleParentItemsChanged (IBrowsableCollection collection, BrowsableEventArgs args)
        {
            if (this.ItemsChanged == null)
                return;

            ArrayList local_ids = new ArrayList ();
            foreach (int parent_index in args.Items) {
                // If the item isn't part of the selection ignore it
                if (!this.Contains (collection [parent_index]))
                    return;

                int local_index = this.IndexOf (parent_index);
                if (local_index >= 0)
                    local_ids.Add (local_index);
            }

            if (local_ids.Count == 0)
                return;

            int [] items = (int [])local_ids.ToArray (typeof (int));
            ItemsChanged (this, new BrowsableEventArgs (items, args.Changes));
        }

        public BitArray ToBitArray () {
            return new BitArray (bit_array);
        }

        public int [] Ids {
            get {
                // TODO: use IEnumerable<>
                return (from i in selected_cells.Values orderby i select i).ToArray ();
            }
        }

        public IPhoto this [int index] {
            get {
                int [] ids = this.Ids;
                return parent [ids[index]];
            }
        }

        public IPhoto [] Items {
            get {
                if (items != null)
                    return items;

                int [] ids = this.Ids;
                items = new IPhoto [ids.Length];
                for (int i = 0; i < items.Length; i++) {
                    items [i] = parent [ids[i]];
                }
                return items;
            }
        }

        public void Clear ()
        {
            Clear (true);
        }

        public void Clear (bool update)
        {
            int [] ids = Ids;
            selected_cells.Clear ();
            bit_array.SetAll (false);
            if (update)
                SignalChange (ids);
        }

        public void Add (IPhoto item)
        {
            if (this.Contains (item))
                return;

            int index = parent.IndexOf (item);
            this.Add (index);
        }

        public int Count {
            get {
                return selected_cells.Count;
            }
        }

        public bool Contains (IPhoto item)
        {
{'fraction_non_alphanumeric': 0.08546803926262868, 'fraction_numerical': 0.0014364376346660283, 'mean_word_length': 1.3403361344537814, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '4736607', 'n_tokens_mistral': 2158, 'n_tokens_neox': 1990, 'n_words': 601}