示例#1
0
        public static bool AddOrdered(this List <CollectionHoplon> list, CollectionHoplon value)
        {
            List <CollectionHoplon> aux = new List <CollectionHoplon>();

            list.Add(value);
            aux.AddRange(list);
            var ordenado = aux.OrderBy(s => s.Key);

            list.Clear();
            list.AddRange(ordenado);
            return(true);
        }
示例#2
0
        public bool Add(string key, int subIndex, string value)
        {
            var collection = new CollectionHoplon(); // o(n)

            // Ja existe a chave?
            var chave = _collectionHoplon.FirstOrDefault(s => s.Key == key); // o(n)

            if (chave != null)                                               // o(n)
            {
                // Já existe o valor em algum subIndex?
                var anyValue = chave.Value.Any(s => s.Value.Any(x => x == value)); // o(n)

                if (anyValue)
                {
                    // Se já existe qual é o subIndex?
                    var subIndexFind = Utils.GetIndexValue(chave.Value, value); // O(n²)

                    if (subIndexFind != subIndex)                               // O(n)
                    {
                        // Remover do subIndex encontrado
                        var dictIndex = chave.Value.Where(s => s.Key == subIndexFind).FirstOrDefault(); // O(n) na verdade nao sei o que faz por trás...
                        dictIndex.Value.RemoveOrdered(value);                                           // O(n)

                        // Adiciona no subIndex atual, ele já existe?
                        var existSubIndex = chave.Value.Any(s => s.Key == subIndex); // O(n)

                        if (!existSubIndex)
                        {
                            var valor = new KeyValuePair <int, List <string> >(subIndex, new List <string> {
                                value
                            });                            // O(n)
                            chave.Value.AddOrdered(valor); // O(n)
                            return(false);                 // O(n)
                        }
                        else
                        {
                            chave.Value.First(s => s.Key == subIndex).Value.AddOrdered(value); // O(n²)
                            return(false);
                        }
                    }
                }
                // O valor não existe em nenhum subIndex.
                else
                {
                    // Já existe o subIndex?
                    var anySubIndex = chave.Value.Any(s => s.Key == subIndex);

                    if (anySubIndex)
                    {
                        chave.Value.First(s => s.Key == subIndex).Value.AddOrdered(value); // O(n²)
                        return(true);
                    }
                    else
                    {
                        var valor = new KeyValuePair <int, List <string> >(subIndex, new List <string> {
                            value
                        });
                        chave.Value.AddOrdered(valor); // O(n²)
                        return(true);
                    }
                }
            }
            else
            {
                var valor = new KeyValuePair <int, List <string> >(subIndex, new List <string> {
                    value
                });
                collection.Key = key;
                collection.Value.AddOrdered(valor);       // O(n²)
                _collectionHoplon.AddOrdered(collection); // O(n²)

                return(true);
            }
            return(false);
        }