schangxiang@126.com
2024-12-03 a5acf83d77d56723125d8bdbe06e03db16a7c38d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System.Collections.Generic;
 
namespace iWareModel
{
    public class LZW
    {
        private Dictionary<string, int> myBaseDir;
 
        private Dictionary<byte, int> myBaseByteDir;
 
        public LZW(){}
 
        public LZW(Dictionary<string, int> myBaseDir) { this.myBaseDir = myBaseDir;}
 
        public LZW(Dictionary<byte, int> myBaseByteDir) { this.myBaseByteDir = myBaseByteDir; }
 
        /// <summary>
        /// LZW压缩算法
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public List<int> Compress(string source) 
        {
            var nextValue = 0;
            var res = new List<int>();
            var dict = new Dictionary<string, int>();
            if (myBaseDir != null) 
            { 
                foreach (var item in myBaseDir) 
                { 
                    dict.Add(item.Key, item.Value);
                    if(item.Value > nextValue)
                    {
                        nextValue = item.Value;
                    }
                } 
            }
            nextValue += 1;
            var prefix = "";
            for (var i = 0; i < source.Length; i++ )
            {
                if (dict.ContainsKey(prefix + source[i])) { prefix += source[i]; }
                else 
                { 
                    res.Add(dict[prefix]);
                    dict.Add(prefix + source[i], nextValue++);
                    prefix = source[i].ToString();
                }
            }
 
            if (dict.ContainsKey(prefix)) { res.Add(dict[prefix]); }
       
            dict = null;
            return res;
        }
 
        /// <summary>
        /// LZW解压算法
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public string Decompress(List<int> destination)
        {
            var nextValue = 0;
            var res = string.Empty;
            var dict = new Dictionary<int, string>();
            if (myBaseDir != null)
            {
                foreach (var item in myBaseDir)
                {
                    dict.Add(item.Value, item.Key);
                    if (item.Value > nextValue)
                    {
                        nextValue = item.Value;
                    }
                }
            }
            nextValue += 1;
            var prefix = string.Empty;
            var current = string.Empty;
            var oCode = -1;
            var nCode = -1;
            for (var i = 0; i < destination.Count; i++)
            {
                nCode = destination[i];
                if(dict.ContainsKey(nCode))
                {
                    prefix = dict[nCode];
                }
                else
                {
                    prefix = dict[oCode] + current;
                }
                res += prefix;
                current = prefix[0].ToString();
                if(dict.ContainsKey(oCode))
                {
                    dict.Add(nextValue++, dict[oCode] + current);
                }
                
                oCode = nCode;               
            }   
            dict = null;
            return res;
        }
 
 
       
 
 
 
 
 
 
 
 
 
 
 
    }
}