File: QualifiedId.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (272 lines) | stat: -rw-r--r-- 7,576 bytes parent folder | download | duplicates (7)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.Activities
{
    using System;
    using System.Collections.Generic;
    using System.Runtime;
    using System.Text;

    class QualifiedId : IEquatable<QualifiedId>
    {
        byte[] compressedId;

        public QualifiedId(Activity element)
        {
            int bufferSize = 0;

            Stack<int> ids = new Stack<int>();
            
            int id = element.InternalId;
            bufferSize += GetEncodedSize(id);
            ids.Push(id);

            IdSpace space = element.MemberOf;

            while (space != null && space.ParentId != 0)
            {
                bufferSize += GetEncodedSize(space.ParentId);
                ids.Push(space.ParentId);

                space = space.Parent;
            }

            this.compressedId = new byte[bufferSize];

            int offset = 0;
            while (ids.Count > 0)
            {
                offset += Encode(ids.Pop(), this.compressedId, offset);
            }
        }

        public QualifiedId(byte[] bytes)
        {
            this.compressedId = bytes;
        }

        public QualifiedId(int[] idArray)
        {
            int bufferSize = 0;

            for (int i = 0; i < idArray.Length; i++)
            {
                bufferSize += GetEncodedSize(idArray[i]);
            }

            this.compressedId = new byte[bufferSize];

            int offset = 0;
            for (int i = 0; i < idArray.Length; i++)
            {
                offset += Encode(idArray[i], this.compressedId, offset);
            }
        }

        public static bool TryGetElementFromRoot(Activity root, QualifiedId id, out Activity targetElement)
        {
            return TryGetElementFromRoot(root, id.compressedId, out targetElement);
        }

        public static bool TryGetElementFromRoot(Activity root, byte[] idBytes, out Activity targetElement)
        {
            Fx.Assert(root.MemberOf != null, "We need to have our IdSpaces set up for this to work.");

            Activity currentActivity = root;
            IdSpace currentIdSpace = root.MemberOf;

            int offset = 0;
            while (offset < idBytes.Length)
            {
                int value;
                offset += Decode(idBytes, offset, out value);

                if (currentIdSpace == null)
                {
                    targetElement = null;
                    return false;
                }

                currentActivity = currentIdSpace[value];

                if (currentActivity == null)
                {
                    targetElement = null;
                    return false;
                }

                currentIdSpace = currentActivity.ParentOf;
            }

            targetElement = currentActivity;
            return true;
        }

        public static QualifiedId Parse(string value)
        {
            QualifiedId result;
            if (!TryParse(value, out result))
            {
                throw FxTrace.Exception.AsError(new FormatException(SR.InvalidActivityIdFormat));
            }

            return result;
        }

        public static bool TryParse(string value, out QualifiedId result)
        {
            Fx.Assert(!string.IsNullOrEmpty(value), "We should have already made sure it isn't null or empty.");

            string[] idStrings = value.Split('.');
            int[] ids = new int[idStrings.Length];
            int bufferSize = 0;

            for (int i = 0; i < idStrings.Length; i++)
            {
                // only support non-negative integers as id segments
                int parsedInt;
                if (!int.TryParse(idStrings[i], out parsedInt) || parsedInt < 0)
                {
                    result = null;
                    return false;
                }

                ids[i] = parsedInt;
                bufferSize += GetEncodedSize(ids[i]);
            }

            byte[] bytes = new byte[bufferSize];
            int offset = 0;

            for (int i = 0; i < ids.Length; i++)
            {
                offset += Encode(ids[i], bytes, offset);
            }

            result = new QualifiedId(bytes);
            return true;
        }

        public static bool Equals(byte[] lhs, byte[] rhs)
        {
            if (lhs.Length == rhs.Length)
            {
                for (int i = 0; i < lhs.Length; i++)
                {
                    if (lhs[i] != rhs[i])
                    {
                        return false;
                    }
                }

                return true;
            }

            return false;
        }

        public byte[] AsByteArray()
        {
            // Note that we don't do a copy because we assume all users will
            // treat it as immutable.
            return this.compressedId;
        }

        public int[] AsIDArray()
        {
            List<int> tmpList = new List<int>();
            int offset = 0;
            while (offset < this.compressedId.Length)
            {
                int value;
                offset += Decode(this.compressedId, offset, out value);

                tmpList.Add(value);
            }
            return tmpList.ToArray();
        }

        public bool Equals(QualifiedId rhs)
        {
            return Equals(this.compressedId, rhs.compressedId);
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();

            bool needDot = false;
            int offset = 0;
            while (offset < this.compressedId.Length)
            {
                if (needDot)
                {
                    builder.Append('.');   
                }

                int value;
                offset += Decode(this.compressedId, offset, out value);

                builder.Append(value);

                needDot = true;
            }

            return builder.ToString();
        }

        // This is the same Encode/Decode logic as the WCF FramingEncoder
        static int Encode(int value, byte[] bytes, int offset)
        {
            Fx.Assert(value >= 0, "Must be non-negative");

            int count = 1;
            while ((value & 0xFFFFFF80) != 0)
            {
                bytes[offset++] = (byte)((value & 0x7F) | 0x80);
                count++;
                value >>= 7;
            }
            bytes[offset] = (byte)value;
            return count;
        }

        // This is the same Encode/Decode logic as the WCF FramingEncoder
        static int Decode(byte[] buffer, int offset, out int value)
        {
            int bytesConsumed = 0;
            value = 0;
            
            while (offset < buffer.Length)
            {
                int next = buffer[offset];
                value |= (next & 0x7F) << (bytesConsumed * 7);
                bytesConsumed++;
                if ((next & 0x80) == 0)
                {
                    break;
                }
                offset++;
            }

            return bytesConsumed;
        }

        static int GetEncodedSize(int value)
        {
            Fx.Assert(value >= 0, "Must be non-negative");

            int count = 1;
            while ((value & 0xFFFFFF80) != 0)
            {
                count++;
                value >>= 7;
            }
            return count;
        }
    }
}