<commit_before>﻿// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Caching;
using osu.Framework.Extensions.IEnumerableExtensions;

namespace osu.Framework.Graphics.Containers
{
    public class SearchContainer : SearchContainer<Drawable>
    {
    }

    public class SearchContainer<T> : FillFlowContainer<T> where T : Drawable
    {
        private string searchTerm;

        /// <summary>
        /// A string that should match the <see cref="IFilterable"/> children
        /// </summary>
        public string SearchTerm
        {
            get => searchTerm;
            set
            {
                searchTerm = value;
                filterValid.Invalidate();
            }
        }

        protected internal override void AddInternal(Drawable drawable)
        {
            base.AddInternal(drawable);
            filterValid.Invalidate();
        }

        private readonly Cached filterValid = new Cached();

        protected override void Update()
        {
            base.Update();

            if (!filterValid.IsValid)
            {
                performFilter();
                filterValid.Validate();
            }
        }

        private void performFilter()
        {
            var terms = (searchTerm ?? string.Empty).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            Children.OfType<IFilterable>().ForEach(child => match(child, terms, terms.Length > 0));
        }

        private static bool match(IFilterable filterable, IEnumerable<string> terms, bool searchActive)
        {
            //Words matched by parent is not needed to match children
            var childTerms = terms.Where(term =>
                !filterable.FilterTerms.Any(filterTerm =>
                    filterTerm.IndexOf(term, StringComparison.InvariantCultureIgnoreCase) >= 0)).ToArray();

            var hasFilterableChildren = filterable as IHasFilterableChildren;

            bool matching = childTerms.Length == 0;

            //We need to check the children and should any child match this matches as well
            if (hasFilterableChildren != null)
                foreach (IFilterable child in hasFilterableChildren.FilterableChildren)
                    matching |= match(child, childTerms, searchActive);

            filterable.FilteringActive = searchActive;
            return filterable.MatchingFilter = matching;
        }
    }
}
<commit_msg>Fix SearchContainer not correctly using Cached<commit_after>﻿// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Caching;
using osu.Framework.Extensions.IEnumerableExtensions;

namespace osu.Framework.Graphics.Containers
{
    public class SearchContainer : SearchContainer<Drawable>
    {
    }

    public class SearchContainer<T> : FillFlowContainer<T> where T : Drawable
    {
        private string searchTerm;

        /// <summary>
        /// A string that should match the <see cref="IFilterable"/> children
        /// </summary>
        public string SearchTerm
        {
            get => searchTerm;
            set
            {
                if (value == searchTerm)
                    return;

                searchTerm = value;
                filterValid.Invalidate();
            }
        }

        protected internal override void AddInternal(Drawable drawable)
        {
            base.AddInternal(drawable);
            filterValid.Invalidate();
        }

        private Cached filterValid = new Cached();

        protected override void Update()
        {
            base.Update();

            if (!filterValid.IsValid)
            {
                performFilter();
                filterValid.Validate();
            }
        }

        private void performFilter()
        {
            var terms = (searchTerm ?? string.Empty).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            Children.OfType<IFilterable>().ForEach(child => match(child, terms, terms.Length > 0));
        }

        private static bool match(IFilterable filterable, IEnumerable<string> terms, bool searchActive)
        {
            //Words matched by parent is not needed to match children
            var childTerms = terms.Where(term =>
                !filterable.FilterTerms.Any(filterTerm =>
                    filterTerm.IndexOf(term, StringComparison.InvariantCultureIgnoreCase) >= 0)).ToArray();

            var hasFilterableChildren = filterable as IHasFilterableChildren;

            bool matching = childTerms.Length == 0;

            //We need to check the children and should any child match this matches as well
            if (hasFilterableChildren != null)
                foreach (IFilterable child in hasFilterableChildren.FilterableChildren)
                    matching |= match(child, childTerms, searchActive);

            filterable.FilteringActive = searchActive;
            return filterable.MatchingFilter = matching;
        }
    }
}

{'fraction_non_alphanumeric': 0.08485193621867881, 'fraction_numerical': 0.0011389521640091116, 'mean_word_length': 2.0125786163522013, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 35, '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': '2397951', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1257, 'n_words': 354}