Vuo  2.4.0
VuoList_VuoTable.cc
Go to the documentation of this file.
1
10// This module is generated by vuo/type/list/generateVariants.sh.
11
12extern "C" {
13#include "VuoHeap.h"
14#include "VuoInteger.h"
15#include "VuoText.h"
16#include "VuoTable.h"
17#include "VuoList_VuoTable.h"
18}
19#include "type.h"
20#include <string>
21#include <sstream>
22#include <vector>
23#include <algorithm>
24
26
29#if 2 == 0
30#define RETAIN(element)
31#define RELEASE(element)
32#elif 2 == 1
33#define RETAIN(element) VuoRetain((void *)element)
34#define RELEASE(element) VuoRelease((void *)element)
35#elif 2 == 2
36#define RETAIN(element) VuoTable_retain(element)
37#define RELEASE(element) VuoTable_release(element)
38#endif
40
41extern "C" {
43#ifdef VUO_COMPILER
45 "title" : "List of VuoTable elements",
46 "keywords" : [ ],
47 "version" : "1.0.0",
48 "dependencies" : [
49 "VuoInteger",
50 "VuoTable"
51 ]
52 });
53#endif
55}
56
60void VuoListDestroy_VuoTable(void *list);
61
62
64{
65 if (!js)
66 return nullptr;
67
69
70 if (json_object_get_type(js) == json_type_array)
71 {
72 int itemCount = json_object_array_length(js);
73 for (int i = 0; i < itemCount; ++i)
74 {
75 json_object *itemObject = json_object_array_get_idx(js, i);
76 VuoTable item = VuoTable_makeFromJson(itemObject);
78 }
79 }
80
81 return list;
82}
83
85{
86 if (!value)
87 return nullptr;
88
89 json_object *listObject = json_object_new_array();
90
91 unsigned long itemCount = VuoListGetCount_VuoTable(value);
92 for (unsigned long i = 1; i <= itemCount; ++i)
93 {
94 VuoTable item = VuoListGetValue_VuoTable(value, i);
95 json_object *itemObject = VuoTable_getJson(item);
96 json_object_array_add(listObject, itemObject);
97 }
98
99 return listObject;
100}
101
102#ifdef VuoTable_REQUIRES_INTERPROCESS_JSON
103struct json_object * VuoList_VuoTable_getInterprocessJson(const VuoList_VuoTable value)
104{
105 json_object *listObject = json_object_new_array();
106
107 unsigned long itemCount = VuoListGetCount_VuoTable(value);
108 for (unsigned long i = 1; i <= itemCount; ++i)
109 {
110 VuoTable item = VuoListGetValue_VuoTable(value, i);
111 json_object *itemObject = VuoTable_getInterprocessJson(item);
112 json_object_array_add(listObject, itemObject);
113 }
114
115 return listObject;
116}
117#endif
118
120{
121 const int maxItems = 20;
122 const int maxCharacters = 400;
123
124 unsigned long itemCount = VuoListGetCount_VuoTable(value);
125 if (itemCount == 0)
126 return strdup("Empty list");
127
128 unsigned long characterCount = 0;
129
130 std::ostringstream summary;
131 summary << "List containing " << itemCount << " item" << (itemCount == 1 ? "" : "s") << ": <ul>";
132 unsigned long i;
133 for (i = 1; i <= itemCount && i <= maxItems && characterCount <= maxCharacters; ++i)
134 {
135 VuoTable item = VuoListGetValue_VuoTable(value, i);
136 char *itemSummaryCstr = VuoTable_getSummary(item);
137 std::string itemSummary = itemSummaryCstr;
138 free(itemSummaryCstr);
139 if (itemSummary.length() && itemSummary.find_first_not_of(' ') != std::string::npos)
140 summary << "\n<li>" << itemSummary << "</li>";
141 else
142 summary << "\n<li>&nbsp;</li>";
143 characterCount += itemSummary.length();
144 }
145
146 if (i <= itemCount)
147 summary << "\n<li>…</li>";
148
149 summary << "</ul>";
150
151 return strdup(summary.str().c_str());
152}
153
155{
156 std::vector<VuoTable> * l = new std::vector<VuoTable>;
158 return reinterpret_cast<VuoList_VuoTable>(l);
159}
160
161VuoList_VuoTable VuoListCreateWithCount_VuoTable(const unsigned long count, const VuoTable value)
162{
163 std::vector<VuoTable> * l = new std::vector<VuoTable>(count, value);
165
166#if 2 != 0
167 for (unsigned long i = 0; i < count; ++i)
168 RETAIN(value);
169#endif
170
171 return reinterpret_cast<VuoList_VuoTable>(l);
172}
173
175{
176 if (!list)
177 return NULL;
178
179 std::vector<VuoTable> *oldList = (std::vector<VuoTable> *)list;
180
181 std::vector<VuoTable> *newList = new std::vector<VuoTable>(*oldList);
183
184 for (std::vector<VuoTable>::iterator i = newList->begin(); i != newList->end(); ++i)
185 RETAIN(*i);
186
187 return reinterpret_cast<VuoList_VuoTable>(newList);
188}
189
191{
192 if (!list)
193 return;
194
195#if 2 != 0
196 VuoListRemoveAll_VuoTable(reinterpret_cast<VuoList_VuoTable>(list));
197#endif
198
199 std::vector<VuoTable> * l = (std::vector<VuoTable> *)list;
200 delete l;
201}
202
203VuoTable VuoListGetValue_VuoTable(const VuoList_VuoTable list, const unsigned long index)
204{
205 std::vector<VuoTable> * l = (std::vector<VuoTable> *)list;
206
207 if (!l || l->size() == 0)
208 return VuoTable_makeFromJson(NULL);
209
210 if (index == 0)
211 return (*l)[0];
212
213 if (index > l->size())
214 return (*l)[l->size()-1];
215
216 return (*l)[index-1];
217}
218
220{
221 std::vector<VuoTable> * l = (std::vector<VuoTable> *)list;
222
223 if (!l || l->size() == 0)
224 return NULL;
225
226 return &((*l)[0]);
227}
228
229void VuoListForeach_VuoTable(const VuoList_VuoTable list, bool (^function)(const VuoTable value))
230{
231 auto l = reinterpret_cast<const std::vector<VuoTable> *>(list);
232
233 if (!l || l->size() == 0)
234 return;
235
236 for (auto item : *l)
237 if (!function(item))
238 break;
239}
240
241void VuoListSetValue_VuoTable(const VuoList_VuoTable list, const VuoTable value, const unsigned long index, bool expandListIfNeeded)
242{
243 if (!list)
244 return;
245
246 std::vector<VuoTable> * l = (std::vector<VuoTable> *)list;
247
248 if (!expandListIfNeeded && l->size() == 0)
249 return;
250
251 unsigned long clampedIndex = index - 1;
252
253 if (index == 0)
254 clampedIndex = 0;
255
256 if (expandListIfNeeded && clampedIndex >= l->size())
257 {
258 l->resize(clampedIndex + 1);
259 (*l)[clampedIndex] = value;
260 RETAIN(value);
261 }
262 else
263 {
264 if (index > l->size())
265 clampedIndex = l->size() - 1;
266
267#if 2 != 0
268 VuoTable oldValue = (*l)[clampedIndex];
269#endif
270 (*l)[clampedIndex] = value;
271 RETAIN(value);
272 RELEASE(oldValue);
273 }
274}
275
276void VuoListInsertValue_VuoTable(const VuoList_VuoTable list, const VuoTable value, const unsigned long index)
277{
278 if (!list)
279 return;
280
281 std::vector<VuoTable> * l = (std::vector<VuoTable> *)list;
282
283 unsigned long clampedIndex = index - 1;
284
285 if (index == 0)
286 clampedIndex = 0;
287
288 if (index > l->size())
289 clampedIndex = l->size() - 1;
290
291 RETAIN(value);
292 if (index <= l->size())
293 l->insert(l->begin() + clampedIndex, value);
294 else
295 l->push_back(value);
296}
297
299{
300 if (!list)
301 return;
302
303 std::vector<VuoTable> * l = (std::vector<VuoTable> *)list;
304 RETAIN(value);
305 l->insert(l->begin(), value);
306}
307
309{
310 if (!list)
311 return;
312
313 std::vector<VuoTable> * l = (std::vector<VuoTable> *)list;
314 RETAIN(value);
315 l->push_back(value);
316}
317
318void VuoListExchangeValues_VuoTable(VuoList_VuoTable list, const unsigned long indexA, const unsigned long indexB)
319{
320 if (!list)
321 return;
322
323 std::vector<VuoTable> * l = (std::vector<VuoTable> *)list;
324
325 size_t size = l->size();
326 if (size == 0)
327 return;
328
329 unsigned long clampedIndexA = indexA - 1;
330 if (indexA == 0)
331 clampedIndexA = 0;
332 if (indexA > size)
333 clampedIndexA = size - 1;
334
335 unsigned long clampedIndexB = indexB - 1;
336 if (indexB == 0)
337 clampedIndexB = 0;
338 if (indexB > size)
339 clampedIndexB = size - 1;
340
341 VuoTable value = (*l)[clampedIndexA];
342 (*l)[clampedIndexA] = (*l)[clampedIndexB];
343 (*l)[clampedIndexB] = value;
344}
345
346#ifdef VuoTable_SUPPORTS_COMPARISON
347void VuoListSort_VuoTable(VuoList_VuoTable list)
348{
349 if (!list)
350 return;
351
352 std::vector<VuoTable> * l = (std::vector<VuoTable> *)list;
353
354 size_t size = l->size();
355 if (size < 2)
356 return;
357
358 std::sort(l->begin(), l->end(), VuoTable_isLessThan);
359}
360
361bool VuoList_VuoTable_areEqual(const VuoList_VuoTable _a, const VuoList_VuoTable _b)
362{
363 if (_a == _b)
364 return true;
365
366 if (!_a || !_b)
367 return false;
368
369 std::vector<VuoTable> *a = (std::vector<VuoTable> *)_a;
370 std::vector<VuoTable> *b = (std::vector<VuoTable> *)_b;
371 if (a->size() != b->size())
372 return false;
373
374 for (std::vector<VuoTable>::iterator ia = a->begin(), ib = b->begin(); ia != a->end(); ++ia, ++ib)
375 if (!VuoTable_areEqual(*ia, *ib))
376 return false;
377
378 return true;
379}
380
381bool VuoList_VuoTable_isLessThan(const VuoList_VuoTable _a, const VuoList_VuoTable _b)
382{
383 if (!_a || !_b)
384 return _a < _b;
385
386 std::vector<VuoTable> *a = (std::vector<VuoTable> *)_a;
387 std::vector<VuoTable> *b = (std::vector<VuoTable> *)_b;
388 if (a->size() < b->size()) return true;
389 if (a->size() > b->size()) return false;
390
391 for (std::vector<VuoTable>::iterator ia = a->begin(), ib = b->begin(); ia != a->end(); ++ia, ++ib)
392 {
393 if (VuoTable_isLessThan(*ia, *ib)) return true;
394 if (VuoTable_isLessThan(*ib, *ia)) return false;
395 }
396
397 return false;
398}
399#endif
400
401void VuoListShuffle_VuoTable(VuoList_VuoTable list, const double chaos)
402{
403 if (!list)
404 return;
405
406 std::vector<VuoTable> * l = (std::vector<VuoTable> *)list;
407
408 size_t size = l->size();
409 if (size == 0)
410 return;
411
412 double clampedChaos = MIN(MAX(chaos,0),1);
413 for (unsigned long i = 0; i < size * clampedChaos; ++i)
414 {
415 unsigned long j = VuoInteger_random(i, size-1);
416 VuoTable value = (*l)[i];
417 (*l)[i] = (*l)[j];
418 (*l)[j] = value;
419 }
420}
421
423{
424 if (!list)
425 return;
426
427 std::vector<VuoTable> * l = (std::vector<VuoTable> *)list;
428 std::reverse(l->begin(), l->end());
429}
430
431VuoList_VuoTable VuoListSubset_VuoTable(VuoList_VuoTable list, const signed long startIndex, const unsigned long itemCount)
432{
433 if (!list)
434 return NULL;
435
436 std::vector<VuoTable> * l = (std::vector<VuoTable> *)list;
437
438 size_t size = l->size();
439 if (size == 0)
440 return NULL;
441
442 signed long clampedStartIndex = startIndex - 1;
443 signed long clampedEndIndex = clampedStartIndex + itemCount - 1;
444
445 if (clampedStartIndex < 0)
446 clampedStartIndex = 0;
447 if (clampedEndIndex >= (signed long)size)
448 clampedEndIndex = size - 1;
449
450 if (clampedStartIndex > clampedEndIndex)
451 return NULL;
452
453 std::vector<VuoTable> *newList = new std::vector<VuoTable>(
454 l->begin() + clampedStartIndex,
455 l->begin() + clampedEndIndex + 1);
457
458 for (std::vector<VuoTable>::iterator i = newList->begin(); i != newList->end(); ++i)
459 RETAIN(*i);
460
461 return reinterpret_cast<VuoList_VuoTable>(newList);
462}
463
464#ifdef VuoTable_SUPPORTS_COMPARISON
465VuoList_VuoTable VuoListRemoveDuplicates_VuoTable(VuoList_VuoTable list)
466{
467 if (!list)
468 return NULL;
469
470 auto *l = (std::vector<VuoTable> *)list;
471
472 size_t size = l->size();
473 if (size == 0)
474 return NULL;
475
476 auto *newList = new std::vector<VuoTable>;
478
479 for (auto i = l->begin(); i != l->end(); ++i)
480 {
481 bool found = false;
482 for (auto j = newList->begin(); j != newList->end(); ++j)
483 if (VuoTable_areEqual(*i, *j))
484 {
485 found = true;
486 break;
487 }
488 if (!found)
489 newList->push_back(*i);
490 }
491
492 for (auto i = newList->begin(); i != newList->end(); ++i)
493 RETAIN(*i);
494
495 return reinterpret_cast<VuoList_VuoTable>(newList);
496}
497#endif
498
500{
501 if (!list)
502 return;
503
504 std::vector<VuoTable> * l = (std::vector<VuoTable> *)list;
505
506 if (!l->size())
507 return;
508
509 RELEASE(l->front());
510 l->erase(l->begin());
511}
512
514{
515 if (!list)
516 return;
517
518 std::vector<VuoTable> * l = (std::vector<VuoTable> *)list;
519
520 if (!l->size())
521 return;
522
523 RELEASE(l->back());
524 l->pop_back();
525}
526
528{
529 if (!list)
530 return;
531
532 while (VuoListGetCount_VuoTable(list) > 0)
534}
535
536void VuoListRemoveValue_VuoTable(VuoList_VuoTable list, const unsigned long index)
537{
538 if (!list)
539 return;
540
541 if (index == 0)
542 return;
543
544 std::vector<VuoTable> * l = (std::vector<VuoTable> *)list;
545
546 size_t size = l->size();
547 if (size == 0 || index > size)
548 return;
549
550 l->erase(l->begin() + index - 1);
551}
552
554{
555 if (!list)
556 return 0;
557
558 std::vector<VuoTable> * l = (std::vector<VuoTable> *)list;
559 return l->size();
560}