Ejemplo n.º 1
0
            /// <summary>
            /// Find the given string in the pool. If not found, returns null.
            /// </summary>
            public NormStr Get(string str, bool add = false)
            {
                AssertValid();

                if (str == null)
                {
                    str = "";
                }

                var  strSpan = str.AsSpan();
                uint hash    = Hashing.HashString(strSpan);
                int  ins     = GetIns(hash);

                while (ins >= 0)
                {
                    ulong meta = _rgmeta[ins];
                    if ((int)Utils.GetLo(meta) == str.Length)
                    {
                        var ns = GetNs(ins);
                        if (strSpan.SequenceEqual(ns.Value.Span))
                        {
                            return(ns);
                        }
                    }
                    ins = (int)Utils.GetHi(meta);
                }
                Contracts.Assert(ins == -1);

                return(add ? AddCore(str.AsMemory(), hash) : null);
            }
Ejemplo n.º 2
0
            public NormStr Get(ReadOnlyMemory <char> str, bool add = false, bool duplicateStr = true)
            {
                AssertValid();

                var  span = str.Span;
                uint hash = Hashing.HashString(span);
                int  ins  = GetIns(hash);

                while (ins >= 0)
                {
                    ulong meta = _rgmeta[ins];
                    if ((int)Utils.GetLo(meta) == str.Length)
                    {
                        var ns = GetNs(ins);
                        if (ns.Value.Span.SequenceEqual(span))
                        {
                            return(ns);
                        }
                    }
                    ins = (int)Utils.GetHi(meta);
                }
                Contracts.Assert(ins == -1);

                if (duplicateStr)
                {
                    // To avoid the case where 'str' actually stores a string with the
                    // content of a whole row in the dataset, a new 'str' is created
                    // See issue https://github.com/dotnet/machinelearning/issues/4571
                    // and PR https://github.com/dotnet/machinelearning/pull/4576
                    return(add ? AddCore(str.ToString().AsMemory(), hash) : null);
                }

                return(add ? AddCore(str, hash) : null);
            }
Ejemplo n.º 3
0
            /// <summary>
            /// Make sure the given string has an equivalent NormStr in the pool and return it.
            /// </summary>
            public NormStr Get(StringBuilder sb, bool add = false)
            {
                AssertValid();

                if (sb == null)
                {
                    return(Get("", add));
                }

                int cch = sb.Length;

                NormStr ns;
                uint    hash = Hashing.HashString(sb);
                int     ins  = GetIns(hash);

                while (ins >= 0)
                {
                    ulong meta = _rgmeta[ins];
                    if ((int)Utils.GetLo(meta) == cch)
                    {
                        ns = GetNs(ins);
                        var value = ns.Value;
                        for (int ich = 0; ; ich++)
                        {
                            if (ich == cch)
                            {
                                return(ns);
                            }
                            if (value.Span[ich] != sb[ich])
                            {
                                break;
                            }
                        }
                    }
                    ins = (int)Utils.GetHi(meta);
                }
                Contracts.Assert(ins == -1);

                return(add ? AddCore(sb.ToString().AsMemory(), hash) : null);
            }
Ejemplo n.º 4
0
        public static uint HashUint(uint u)
        {
            ulong uu = (ulong)u * 0x7ff19519UL; // this number is prime.

            return(Utils.GetLo(uu) + Utils.GetHi(uu));
        }