~desrt/libvalmynd/master

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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
namespace Valmynd.Flatten {
	/* It is inconvenient to directly display a Valmynd.Model submenu that contains sections.  This is because
	 * the widget doing the display will have to monitor all of the sections for changes and merge their
	 * contents.  Since it is expected that a large number of consumers of the model will want to do this, we
	 * provide a utility class to accomplish it.
	 *
	 * To keep things simple, as a first attempt, we break the task up into three parts:
	 *
	 *   - adding separators before every non-empty section
	 *
	 *   - merging the sections, including their separators
	 *
	 *   - if the resulting first item is a separator without a label, drop it
	 *
	 * We have three classes to this end: PrefixSeparator, MergeSections, DropFirstSeparator.
	 *
	 * Eventually it should be possible to write one large class that is capable of keeping track of all of
	 * these things, but for the time being, the extra overhead buys us greatly reduced complexity.
	 */
	private class MergeSections : Model {
		bool separators;
		bool mutable;
		Model model;

		Model?[] sections;
		int[] totals;
		ulong[] ids;

		int before (int index) {
			return index > 0 ? totals[index - 1] : 0;
		}

		int n_items (int index) {
			if (sections[index] != null) {
				return sections[index].get_n_items ();
			} else {
				return 1;
			}
		}

		protected override bool is_mutable () {
			return mutable;
		}

		protected override int get_n_items () {
			return before (sections.length);
		}

		int section_number (ref int index) {
			for (var i = 0; i < sections.length; i++) {
				if (index < totals[i]) {
					index -= before (i);
					return i;
				}
			}

			assert_not_reached ();
		}

		ModelItem find_item (int index) {
			var sn = section_number (ref index);

			if (sections[sn] != null) {
				return sections[sn].get_item (index);
			} else {
				return model.get_item (sn);
			}
		}

		protected override Variant? virtual_get_item_attribute (int index, string name, VariantType? expected_type) {
			return find_item (index).get_attribute_value (name, expected_type);
		}

		protected override Model? get_item_link (int index, string name) {
			return find_item (index).get_link (name);
		}

		protected override AttributeIterator iterate_item_attributes (int index) {
			return find_item (index).iterate_attributes ();
		}

		void section_changed (Model section, int index, int removed, int added) {
			int i;

			for (i = 0; i < sections.length; i++) {
				if (section == sections[i]) {
					break;
				}
			}
			assert (i < sections.length);

			assert (index + removed <= totals[i]);

			for (var j = i; j < sections.length; j++) {
				totals[j] += added - removed;
			}

			var before = i > 0 ? totals[i - 1] : 0;

			items_changed (index + before, removed, added);
		}

		void model_changed (Model model, int index, int removed, int added) {
			assert (index + removed <= sections.length);

			var start = before (index);
			var end = before (index + removed);

			for (var i = index; i < index + removed; i++) {
				if (ids[i] != 0) {
					SignalHandler.disconnect (sections[i], ids[i]);
				}
				sections[i] = null;
				ids[i] = 0;
			}

			if (removed != added) {
				var n = sections.length + added - removed;
				var new_sections = new Model[n];
				var new_totals = new int[n];
				var new_ids = new ulong[n];

				for (var i = 0; i < index; i++) {
					new_sections[i] = sections[i];
					new_totals[i] = totals[i];
					new_ids[i] = ids[i];
				}

				for (var i = index + added; i < n; i++) {
					var prev = i - added + removed;

					new_sections[i] = sections[prev];
					new_ids[i] = ids[prev];
				}

				sections = (owned) new_sections;
				totals = (owned) new_totals;
				ids = (owned) new_ids;
			}

			for (var i = index; i < index + added; i++) {
				var item = model.get_item (i);

				if (item.get_type () == MenuItemType.SECTION) {
					assert (sections[i] == null);
					assert (ids[i] == 0);

					var section = flatten (item.get_link ("menu"), false);
					if (separators) {
						section = new PrefixSeparator (section);
					}

					sections[i] = section;

					if (section.is_mutable ()) {
						ids[i] = section.items_changed.connect (section_changed);
						mutable = true;
					}
				}
			}

			for (var i = index; i < sections.length; i++) {
				totals[i] = before (i) + n_items (i);
			}

			var new_end = before (index + added);

			items_changed (start, end - start, new_end - start);
		}

		internal MergeSections (Model model, bool separators) {
			this.separators = separators;
			this.model = model;

			if (model.is_mutable ()) {
				model.items_changed.connect (model_changed);
				mutable = true;
			}

			model_changed (model, 0, 0, model.get_n_items ());
		}
	}

	private class DropFirstSeparator : Model {
		Model model;
		bool mutable;
		int del;

		protected override bool is_mutable () {
			return mutable;
		}

		protected override int get_n_items () {
			return model.get_n_items () - del;
		}

		protected override Variant? virtual_get_item_attribute (int index, string name, VariantType? expected_type) {
			return model.virtual_get_item_attribute (index + del, name, expected_type);
		}

		protected override Model? get_item_link (int index, string name) {
			return model.get_item_link (index + del, name);
		}

		protected override AttributeIterator iterate_item_attributes (int index) {
			return model.iterate_item_attributes (index + del);
		}

		void model_changed (Model model, int index, int removed, int added) {
			var was_del = del;
			del = (model.get_n_items () > 0 && model.get_item_label (0) == null) ? 1 : 0;

			if (was_del < del) {
				if (added > 0) {
					added--;
				} else {
					removed++;
				}
			} else if (del < was_del) {
				if (added > 0) {
					added++;
				} else {
					removed--;
				}
			} else {
				index -= del;
			}

			items_changed (index, removed, added);
		}

		internal DropFirstSeparator (Model model) {
			this.model = model;

			if (model.is_mutable ()) {
				model.items_changed.connect (model_changed);
				mutable = true;
			}

			var is_del = model.get_n_items () > 0 && model.get_item_label (0) == null;
			del = is_del ? 1 : 0;
		}
	}

	private class SeparatorAttributeIterator : AttributeIterator {
		bool done;

		protected override bool next () {
			if (!done) {
				done = true;
				return true;
			} else {
				return false;
			}
		}

		protected override Attribute get () {
			var attr = Attribute ();
			attr.name = "type";
			attr.value = new Variant.string ("separator");
			return attr;
		}
	}

	private class PrefixSeparator : Model {
		Model model;
		int n_items;
		bool mutable;

		protected override bool is_mutable () {
			return mutable;
		}

		protected override int get_n_items () {
			var n = n_items;

			if (n > 0) {
				n++;
			}

			return n;
		}

		protected override Variant? virtual_get_item_attribute (int index, string name, VariantType? expected_type) {
			if (index == 0) {
				if (name == "type") {
					return new Variant.string ("separator");
				}

				return null;
			}

			return model.virtual_get_item_attribute (index - 1, name, expected_type);
		}

		protected override Model? get_item_link (int index, string name) {
			if (index == 0) {
				return null;
			}

			return model.get_item_link (index - 1, name);
		}

		protected override AttributeIterator iterate_item_attributes (int index) {
			if (index == 0) {
				return new SeparatorAttributeIterator ();
			}

			return model.iterate_item_attributes (index - 1);
		}

		void model_changed (Model model, int index, int removed, int added) {
			var was_empty = n_items == 0;
			n_items += added - removed;
			var is_empty = n_items == 0;
			assert (n_items == model.get_n_items ());

			if (was_empty && !is_empty) {
				assert (index == 0 && removed == 0);
				added++;
			} else if (is_empty && !was_empty) {
				assert (index == 0 && added == 0);
				removed++;
			} else {
				index++;
			}

			items_changed (index, removed, added);
		}

		public PrefixSeparator (Model model) {
			this.model = model;

			if (model.is_mutable ()) {
				model.items_changed.connect (model_changed);
				mutable = true;
			}

			n_items = model.get_n_items ();
		}

		~PrefixSeparator () {
		}
	}
}

namespace Valmynd {
	public Model flatten (Model model, bool separators = true) {
		if (separators) {
			return new Flatten.DropFirstSeparator (new Flatten.MergeSections (model, true));
		} else {
			return new Flatten.MergeSections (model, false);
		}
	}
}