예제 #1
0
        private static Context_Compress produceW(Context_Compress context)
        {
            if (context.dictionaryToCreate.ContainsKey(context.w))
            {
                if (context.w[0] < 256)
                {
                    context.data = writeBits(context.numBits, 0, context.data);
                    context.data = writeBits(8, context.w[0], context.data);
                }
                else
                {
                    context.data = writeBits(context.numBits, 1, context.data);
                    context.data = writeBits(16, context.w[0], context.data);
                }

                context = decrementEnlargeIn(context);
                context.dictionaryToCreate.Remove(context.w);
            }
            else
            {
                context.data = writeBits(context.numBits, context.dictionary[context.w], context.data);
            }

            return(context);
        }
예제 #2
0
        private static Context_Compress produceW(Context_Compress context)
        {

            if (context.dictionaryToCreate.ContainsKey(context.w))
            {
                if (context.w[0] < 256)
                {
                    context.data = writeBits(context.numBits, 0, context.data);
                    context.data = writeBits(8, context.w[0], context.data);
                }
                else
                {
                    context.data = writeBits(context.numBits, 1, context.data);
                    context.data = writeBits(16, context.w[0], context.data);
                }

                context = decrementEnlargeIn(context);
                context.dictionaryToCreate.Remove(context.w);
            }
            else
            {
                context.data = writeBits(context.numBits, context.dictionary[context.w], context.data);
            }

            return context;
        }
예제 #3
0
 private static Context_Compress decrementEnlargeIn(Context_Compress context)
 {
     context.enlargeIn--;
     if (context.enlargeIn == 0)
     {
         context.enlargeIn = (int)Math.Pow(2, context.numBits);
         context.numBits++;
     }
     return(context);
 }
예제 #4
0
        public static string compress(string uncompressed)
        {
            Context_Compress      context = new Context_Compress();
            Context_Compress_Data data    = new Context_Compress_Data();

            context.dictionary         = new Dictionary <string, int>();
            context.dictionaryToCreate = new Dictionary <string, bool>();
            context.c         = "";
            context.wc        = "";
            context.w         = "";
            context.enlargeIn = 2;
            context.dictSize  = 3;
            context.numBits   = 2;

            data.str      = "";
            data.val      = 0;
            data.position = 0;

            context.data = data;

            try
            {
                for (int i = 0; i < uncompressed.Length; i++)
                {
                    context.c = uncompressed[i].ToString();

                    if (!context.dictionary.ContainsKey(context.c))
                    {
                        context.dictionary[context.c]         = context.dictSize++;
                        context.dictionaryToCreate[context.c] = true;
                    }
                    ;

                    context.wc = context.w + context.c;

                    if (context.dictionary.ContainsKey(context.wc))
                    {
                        context.w = context.wc;
                    }
                    else
                    {
                        context = produceW(context);
                        context = decrementEnlargeIn(context);
                        context.dictionary[context.wc] = context.dictSize++;
                        context.w = context.c;
                    }
                }

                if (context.w != "")
                {
                    context = produceW(context);
                }

                // Mark the end of the stream
                context.data = writeBits(context.numBits, 2, context.data);

                // Flush the last char
                while (true)
                {
                    context.data.val = (context.data.val << 1);
                    if (context.data.position == 15)
                    {
                        context.data.str += (char)context.data.val;
                        break;
                    }
                    else
                    {
                        context.data.position++;
                    }
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }

            return(context.data.str);
        }
예제 #5
0
        private static Context_Compress decrementEnlargeIn(Context_Compress context)
        {

            context.enlargeIn--;
            if (context.enlargeIn == 0)
            {
                context.enlargeIn = (int)Math.Pow(2, context.numBits);
                context.numBits++;
            }
            return context;
        }
예제 #6
0
        public static string compress(string uncompressed)
        {

            Context_Compress context = new Context_Compress();
            Context_Compress_Data data = new Context_Compress_Data();

            context.dictionary = new Dictionary<string, int>();
            context.dictionaryToCreate = new Dictionary<string, bool>();
            context.c = "";
            context.wc = "";
            context.w = "";
            context.enlargeIn = 2;
            context.dictSize = 3;
            context.numBits = 2;

            data.str = "";
            data.val = 0;
            data.position = 0;

            context.data = data;

            try
            {
                for (int i = 0; i < uncompressed.Length; i++)
                {
                    context.c = uncompressed[i].ToString();

                    if (!context.dictionary.ContainsKey(context.c))
                    {
                        context.dictionary[context.c] = context.dictSize++;
                        context.dictionaryToCreate[context.c] = true;
                    };

                    context.wc = context.w + context.c;

                    if (context.dictionary.ContainsKey(context.wc))
                    {
                        context.w = context.wc;
                    }
                    else
                    {
                        context = produceW(context);
                        context = decrementEnlargeIn(context);
                        context.dictionary[context.wc] = context.dictSize++;
                        context.w = context.c;
                    }
                }

                if (context.w != "")
                {
                    context = produceW(context);
                }

                // Mark the end of the stream
                context.data = writeBits(context.numBits, 2, context.data);

                // Flush the last char
                while (true)
                {
                    context.data.val = (context.data.val << 1);
                    if (context.data.position == 15)
                    {
                        context.data.str += (char)context.data.val;
                        break;
                    }
                    else
                        context.data.position++;
                }

            }
            catch (Exception ex)
            {
                return ex.Message;
            }

            return context.data.str;
        }