Exemplo n.º 1
0
 /// <summary>
 /// 中文分词词语集合写入文本文件
 /// </summary>
 private unsafe static void writeTxtFile()
 {
     string[] words = Words;
     using (unmanagedStream wordStream = new unmanagedStream())
     {
         *(int *)wordStream.Data = words.Length;
         wordStream.UnsafeAddLength(sizeof(int));
         foreach (string word in words)
         {
             wordStream.Write(word);
             wordStream.Write((char)0);
         }
         subArray <byte> data = io.compression.stream.Deflate.GetCompress(wordStream.GetArray(), 0);
         using (FileStream fileStream = new FileStream(fastCSharp.config.search.Default.WordTxtFileName, FileMode.Create, FileAccess.Write, FileShare.None))
         {
             fileStream.Write(data.UnsafeArray, data.StartIndex, data.Count);
         }
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// 序列化
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="map"></param>
 /// <param name="fieldCount"></param>
 internal static void FieldSerialize(unmanagedStream stream, ulong map, int fieldCount)
 {
     if (map == 0)
     {
         stream.Write(0);
     }
     else
     {
         stream.PrepLength(sizeof(int) + sizeof(ulong));
         byte *data = stream.CurrentData;
         *(int *)data = fieldCount;
         *(ulong *)(data + sizeof(int)) = map;
         stream.UnsafeAddLength(sizeof(int) + sizeof(ulong));
         stream.PrepLength();
     }
 }
Exemplo n.º 3
0
            /// <summary>
            /// 获取解压数据
            /// </summary>
            private unsafe void get()
            {
                byte[] buffer = fastCSharp.memoryPool.StreamBuffers.Get();
                try
                {
                    int bufferLength = buffer.Length;
                    fixed(byte *bufferFixed = buffer)
                    {
                        int length = CompressStream.Read(buffer, 0, bufferLength);

                        while (length != 0)
                        {
                            dataStream.Write(bufferFixed, length);
                            length = CompressStream.Read(buffer, 0, bufferLength);
                        }
                    }
                }
                finally { fastCSharp.memoryPool.StreamBuffers.PushNotNull(buffer); }
            }
Exemplo n.º 4
0
 /// <summary>
 /// 序列化
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="map"></param>
 /// <param name="type"></param>
 internal static void FieldSerialize(unmanagedStream stream, byte *map, type type)
 {
     if (map == null)
     {
         stream.Write(0);
     }
     else
     {
         stream.PrepLength(type.FieldSerializeSize + sizeof(int));
         byte *data = stream.CurrentData, read = map;
         *(int *)data = type.FieldCount;
         data        += sizeof(int);
         for (byte *end = map + (type.FieldSerializeSize & (int.MaxValue - sizeof(ulong) + 1)); read != end; read += sizeof(ulong), data += sizeof(ulong))
         {
             *(ulong *)data = *(ulong *)read;
         }
         if ((type.FieldSerializeSize & sizeof(int)) != 0)
         {
             *(uint *)data = *(uint *)read;
         }
         stream.UnsafeAddLength(type.FieldSerializeSize + sizeof(int));
         stream.PrepLength();
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// 获取C#计算哈希代码
        /// </summary>
        /// <param name="keyName">关键字变量名称</param>
        /// <returns>C#计算哈希代码</returns>
        public unsafe string Code(string keyName = "key")
        {
            if (indexs == null)
            {
                return(string.Empty);
            }
            using (unmanagedStream code = new unmanagedStream())
            {
                if (indexs.Length == 0)
                {
                    code.Write(@"fixed (char* keyFixed = ");
                    code.Write(keyName);
                    code.Write(@")
{
");
                }
                code.Write("    uint code = ");
                bool isFirst = true;
                if (indexs.Length == 0)
                {
                    code.Write("(uint)fastCSharp.algorithm.hashCode.GetHashCode(keyFixed, ");
                    code.Write(keyName);
                    code.Write(@" << 1)");
                }
                else
                {
                    int shiftBit = bit * (indexs.Length - 1);
                    foreach (index index in indexs)
                    {
                        if (isFirst)
                        {
                            isFirst = false;
                        }
                        else
                        {
                            code.Write(" + ");
                        }
                        code.Write("(uint)");
                        if (shiftBit != 0)
                        {
                            code.Write('(');
                        }
                        if (sub != 0)
                        {
                            code.Write('(');
                        }
                        code.Write(keyName);
                        code.Write('[');
                        switch (index.Type)
                        {
                        case index.type.Left:
                            code.Write(index.Index.toString());
                            break;

                        case index.type.Right:
                            code.Write(keyName);
                            code.Write(".Length - ");
                            code.Write((index.Index + 1).toString());
                            break;

                        default:
                            code.Write(keyName);
                            code.Write(".Length >> ");
                            code.Write(index.Index.toString());
                            break;
                        }
                        code.Write(']');
                        if (sub != 0)
                        {
                            code.Write(" - ");
                            code.Write(sub.toString());
                            code.Write(')');
                        }
                        if (shiftBit != 0)
                        {
                            code.Write(" << ");
                            code.Write(shiftBit.toString());
                            code.Write(')');
                            shiftBit -= bit;
                        }
                    }
                }
                code.Write(@";
    return (int)((");
                isFirst = true;
                foreach (int shift in shifts)
                {
                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        code.Write(" ^ ");
                    }
                    if (shift != 0)
                    {
                        code.Write('(');
                    }
                    code.Write("code");
                    if (shift != 0)
                    {
                        code.Write(" >> ");
                        code.Write(shift.toString());
                        code.Write(')');
                    }
                }
                code.Write(") & ((1U << ");
                code.Write(minSize.toString());
                code.Write(") - 1))");
                if (!isMaxCode && minCode != 0)
                {
                    code.Write(" - ");
                    code.Write(minCode.toString());
                }
                code.Write(';');
                if (indexs.Length == 0)
                {
                    code.Write(@"
}");
                }
                return(code.ToString());
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="value">数据对象</param>
        /// <param name="typeIndex">数据类型</param>
        public void Append(object value, byte typeIndex)
        {
            switch (typeIndex)
            {
            case 0:
                stream.Write((int)value);
                break;

            case 1:
                stream.Write((int)(int?)value);
                break;

            case 2:
                strings.Add((string)value);
                break;

            case 3:
                stream.Write(((DateTime)value).toKindTicks());
                break;

            case 4:
                stream.Write(((DateTime)(DateTime?)value).toKindTicks());
                break;

            case 5:
                stream.Write((double)value);
                break;

            case 6:
                stream.Write((double)(double?)value);
                break;

            case 7:
                stream.Write((float)value);
                break;

            case 8:
                stream.Write((float)(float?)value);
                break;

            case 9:
                stream.Write((decimal)value);
                break;

            case 10:
                stream.Write((decimal)(decimal?)value);
                break;

            case 11:
                stream.Write((Guid)value);
                break;

            case 12:
                stream.Write((Guid)(Guid?)value);
                break;

            case 13:
                stream.Write((bool)value ? (byte)1 : (byte)0);
                break;

            case 14:
                stream.Write((bool)(bool?)value ? (byte)1 : (byte)0);
                break;

            case 15:
                stream.Write((byte)value);
                break;

            case 16:
                stream.Write((byte)(byte?)value);
                break;

            case 17:
                bytes.Add((byte[])value);
                break;

            case 18:
                stream.Write((sbyte)value);
                break;

            case 19:
                stream.Write((sbyte)(sbyte?)value);
                break;

            case 20:
                stream.Write((short)value);
                break;

            case 21:
                stream.Write((short)(short?)value);
                break;

            case 22:
                stream.Write((ushort)value);
                break;

            case 23:
                stream.Write((ushort)(ushort?)value);
                break;

            case 24:
                stream.Write((uint)value);
                break;

            case 25:
                stream.Write((uint)(uint?)value);
                break;

            case 26:
                stream.Write((long)value);
                break;

            case 27:
                stream.Write((long)(long?)value);
                break;

            case 28:
                stream.Write((ulong)value);
                break;

            case 29:
                stream.Write((ulong)(ulong?)value);
                break;
            }
        }