예제 #1
0
        public void Dictionary()
        {
            ReusableDictionary <int, float> intFloats  = new ReusableDictionary <int, float>();
            Dictionary <int, float>         dictionary = intFloats.CleanDictionary;

            Assert.AreEqual(0, dictionary.Count);
        }
예제 #2
0
파일: Result.cs 프로젝트: AutoCSer/AutoCSer
            /// <summary>
            /// 添加新的数据
            /// </summary>
            /// <param name="key">数据标识</param>
            /// <param name="values">分词结果</param>
            public void Add(ref keyType key, ReusableDictionary <HashString, ResultIndexLeftArray> values)
            {
                WordCounterIndex counterIndex;
                ResultIndexArray resultIndex = default(ResultIndexArray);

                foreach (KeyValue <HashString, ResultIndexLeftArray> result in values.KeyValues)
                {
                    if (!results.TryGetValue(result.Key, out counterIndex))
                    {
                        counterIndex.Index = counterPool.Get(out counterIndex.Array);
                        results.Set(result.Key, counterIndex);
                        counterIndex.CreateResult();
                    }
                    if ((flags & SearchFlags.ResultIndexs) == 0)
                    {
                        resultIndex.Set(EmptyArray <int> .Array, result.Value.TextLength);
                        counterIndex.Add(key, result.Value.WordType, ref resultIndex);
                        ++WordCount;
                    }
                    else
                    {
                        resultIndex.Set(result.Value.Indexs.GetArray(), result.Value.TextLength);
                        counterIndex.Add(key, result.Value.WordType, ref resultIndex);
                        WordCount += result.Value.Indexs.Length;
                    }
                }
                ++Version;
            }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="valueType">数据类型</typeparam>
        /// <param name="data">数据</param>
        /// <param name="value">目标对象</param>
        /// <returns>是否成功</returns>
        internal bool DeSerializeTcpServer <valueType>(ref SubArray <byte> data, ref valueType value)
        {
            if (data.Length >= sizeof(int) * 3 && (data.Length & 3) == 0)
            {
                fixed(byte *dataFixed = (Buffer = data.Array))
                {
                    start = dataFixed + (bufferIndex = data.Start);
                    int length = data.Length - sizeof(int);

                    End = start + length;
                    if (((uint)(*(int *)End ^ length) | ((*(uint *)start & SerializeConfig.HeaderMapAndValue) ^ SerializeConfig.HeaderMapValue)) == 0)
                    {
                        //MemberMap = null;
                        getGlobalVersion();
                        if (isReferenceMember == TypeDeSerializer <valueType> .IsReferenceMember)
                        {
                            if (points != null)
                            {
                                points.Clear();
                            }
                        }
                        else
                        {
                            if (isReferenceMember)
                            {
                                isReferenceMember = false;
                            }
                            else
                            {
                                isReferenceMember = true;
                                if (points == null)
                                {
                                    points = ReusableDictionary.CreateInt <object>();
                                }
                                else
                                {
                                    points.Clear();
                                }
                            }
                        }
                        isReferenceArray = true;
                        State            = DeSerializeState.Success;
                        TypeDeSerializer <valueType> .DeSerializeTcpServer(this, ref value);

                        return(State == DeSerializeState.Success && Read == End);
                    }
                }
            }
            return(false);
        }
예제 #4
0
 /// <summary>
 /// 对象转换JSON字符串
 /// </summary>
 /// <typeparam name="valueType">目标数据类型</typeparam>
 /// <param name="value">数据对象</param>
 private void serialize <valueType>(ref valueType value)
 {
     if (Config.GetLoopObject == null || Config.SetLoopObject == null)
     {
         if (Config.GetLoopObject != null)
         {
             Warning = SerializeWarning.LessSetLoop;
         }
         else if (Config.SetLoopObject != null)
         {
             Warning = SerializeWarning.LessGetLoop;
         }
         else
         {
             Warning = SerializeWarning.None;
         }
         if (checkLoopDepth != Config.CheckLoopDepth || isLoopObject)
         {
             if (Config.CheckLoopDepth <= 0)
             {
                 checkLoopDepth = 0;
                 if (forefather == null)
                 {
                     forefather = new object[sizeof(int)];
                 }
             }
             else
             {
                 checkLoopDepth = Config.CheckLoopDepth;
             }
         }
         isLoopObject = false;
     }
     else
     {
         if (!isLoopObject)
         {
             isLoopObject = true;
             if (objectIndexs == null)
             {
                 objectIndexs = ReusableDictionary <ObjectReference> .Create <string>();
             }
         }
         Warning        = SerializeWarning.None;
         checkLoopDepth = Config.CheckLoopDepth <= 0 ? SerializeConfig.DefaultCheckLoopDepth : Config.CheckLoopDepth;
     }
     TypeSerializer <valueType> .Serialize(this, value);
 }
예제 #5
0
 /// <summary>
 /// 序列化
 /// </summary>
 /// <typeparam name="valueType">目标数据类型</typeparam>
 /// <param name="value">数据对象</param>
 /// <param name="stream"></param>
 internal void SerializeTcpServer <valueType>(valueType value, UnmanagedStream stream)
 {
     if (value == null)
     {
         stream.Write(NullValue);
     }
     else
     {
         //CurrentMemberMap = MemberMap = null;
         //Warning = SerializeWarning.None;
         if (isReferenceMember == TypeSerializer <valueType> .IsReferenceMember)
         {
             if (points != null)
             {
                 points.Clear();
             }
         }
         else if (isReferenceMember)
         {
             isReferenceMember = false;
         }
         else
         {
             isReferenceMember = true;
             if (points == null)
             {
                 points = ReusableDictionary <ObjectReference> .Create <int>();
             }
             else
             {
                 points.Clear();
             }
         }
         //streamStartIndex = Stream.OffsetLength;
         streamStartIndex = stream.ByteSize;
         isReferenceArray = true;
         Config.UnsafeWriteHeader(stream);
         Stream.From(stream);
         try
         {
             //Warning = SerializeWarning.None;
             TypeSerializer <valueType> .SerializeTcpServer(this, ref value);
         }
         finally { stream.From(Stream); }
         //Stream.Write(Stream.OffsetLength - streamStartIndex);
         stream.Write(stream.ByteSize - streamStartIndex);
     }
 }
예제 #6
0
        /// <summary>
        /// GIF文件写入器
        /// </summary>
        /// <param name="filename">文件名称</param>
        /// <param name="width">素数宽度</param>
        /// <param name="height">素数高度</param>
        /// <param name="globalColors">全局颜色列表</param>
        /// <param name="backgroundColorIndex">背景颜色在全局颜色列表中的索引,如果没有全局颜色列表,该值没有意义</param>
        /// <param name="log">日志处理</param>
        public unsafe FileWriter(string filename, short width, short height, Color[] globalColors = null, byte backgroundColorIndex = 0, ILog log = null)
        {
            this.log = log ?? AutoCSer.Log.Pub.Log;
            if (width <= 0)
            {
                throw new IndexOutOfRangeException("width[" + width.toString() + "] <= 0");
            }
            if (height <= 0)
            {
                throw new IndexOutOfRangeException("height[" + height.toString() + "] <= 0");
            }
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException("filename is null or empty");
            }
            fileStream       = new FileStream(filename, FileMode.CreateNew, FileAccess.Write, FileShare.None, 1, FileOptions.WriteThrough);
            Width            = width;
            Height           = height;
            globalColorCount = globalColors.length();
            int pixel = 0;

            if (globalColorCount != 0)
            {
                if (globalColorCount < 256)
                {
                    pixel = ((uint)globalColorCount).bits() - 1;
                    if (globalColorCount != (1 << pixel))
                    {
                        ++pixel;
                    }
                }
                else
                {
                    globalColorCount = 256;
                    pixel            = 7;
                }
                pixel |= 0x80;
            }

            fixed(byte *bufferFixed = fileBuffer)
            {
                *(ulong *)bufferFixed      = fileVersion | ((ulong)width << 48);
                *(uint *)(bufferFixed + 8) = (uint)(int)height | (globalColorCount == 0 ? 0 : ((uint)pixel << 16)) | (7 << (16 + 4))
                                             | (backgroundColorIndex >= globalColorCount ? 0 : ((uint)backgroundColorIndex << 24));
                bufferIndex = 13;
                if (globalColorCount != 0)
                {
                    byte *currentBuffer = bufferFixed + 13;
                    fixed(Color *colorFixed = globalColors)
                    {
                        for (Color *currentColor = colorFixed, colorEnd = colorFixed + globalColorCount; currentColor != colorEnd; ++currentColor)
                        {
                            Color color           = *currentColor;
                            *     currentBuffer++ = color.Red;
                            *     currentBuffer++ = color.Green;
                            *     currentBuffer++ = color.Blue;
                        }
                    }

                    bufferIndex += 3 << (pixel ^ 0x80);
                }
            }

            colors      = new Color[(int)Width * Height];
            colorCounts = new int[colors.Length];
            colorIndexs = ReusableDictionary <Color> .Create <int>();
        }