Vuo 2.4.2
Loading...
Searching...
No Matches
VuoList_VuoLeapPointable.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 "VuoLeapPointable.h"
18}
19#include "type.h"
20#include <string>
21#include <sstream>
22#include <vector>
23#include <algorithm>
24
26
29#if 0 == 0
30#define RETAIN(element)
31#define RELEASE(element)
32#elif 0 == 1
33#define RETAIN(element) VuoRetain((void *)element)
34#define RELEASE(element) VuoRelease((void *)element)
35#elif 0 == 2
36#define RETAIN(element) VuoLeapPointable_retain(element)
37#define RELEASE(element) VuoLeapPointable_release(element)
38#endif
40
41extern "C" {
43#ifdef VUO_COMPILER
45 "title" : "List of VuoLeapPointable elements",
46 "keywords" : [ ],
47 "version" : "1.0.0",
48 "dependencies" : [
49 "VuoInteger",
50 "VuoLeapPointable"
51 ]
52 });
53#endif
55}
56
60void VuoListDestroy_VuoLeapPointable(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);
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_VuoLeapPointable(value);
92 for (unsigned long i = 1; i <= itemCount; ++i)
93 {
95 json_object *itemObject = VuoLeapPointable_getJson(item);
96 json_object_array_add(listObject, itemObject);
97 }
98
99 return listObject;
100}
101
102#ifdef VuoLeapPointable_REQUIRES_INTERPROCESS_JSON
103struct json_object * VuoList_VuoLeapPointable_getInterprocessJson(const VuoList_VuoLeapPointable value)
104{
105 json_object *listObject = json_object_new_array();
106
107 unsigned long itemCount = VuoListGetCount_VuoLeapPointable(value);
108 for (unsigned long i = 1; i <= itemCount; ++i)
109 {
111 json_object *itemObject = VuoLeapPointable_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_VuoLeapPointable(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 {
136 char *itemSummaryCstr = VuoLeapPointable_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<VuoLeapPointable> * l = new std::vector<VuoLeapPointable>;
158 return reinterpret_cast<VuoList_VuoLeapPointable>(l);
159}
160
162{
163 std::vector<VuoLeapPointable> * l = new std::vector<VuoLeapPointable>(count, value);
165
166#if 0 != 0
167 for (unsigned long i = 0; i < count; ++i)
168 RETAIN(value);
169#endif
170
171 return reinterpret_cast<VuoList_VuoLeapPointable>(l);
172}
173
175{
176 std::vector<VuoLeapPointable> *l = new std::vector<VuoLeapPointable>(values, values + valueCount);
178
179#if 0 != 0
180 for (unsigned long i = 0; i < valueCount; ++i)
181 RETAIN(values[i]);
182#endif
183
184 return reinterpret_cast<VuoList_VuoLeapPointable>(l);
185}
186
188{
189 if (!list)
190 return NULL;
191
192 std::vector<VuoLeapPointable> *oldList = (std::vector<VuoLeapPointable> *)list;
193
194 std::vector<VuoLeapPointable> *newList = new std::vector<VuoLeapPointable>(*oldList);
196
197 for (std::vector<VuoLeapPointable>::iterator i = newList->begin(); i != newList->end(); ++i)
198 RETAIN(*i);
199
200 return reinterpret_cast<VuoList_VuoLeapPointable>(newList);
201}
202
204{
205 if (!list)
206 return;
207
208#if 0 != 0
210#endif
211
212 std::vector<VuoLeapPointable> * l = (std::vector<VuoLeapPointable> *)list;
213 delete l;
214}
215
217{
218 std::vector<VuoLeapPointable> * l = (std::vector<VuoLeapPointable> *)list;
219
220 if (!l || l->size() == 0)
222
223 if (index == 0)
224 return (*l)[0];
225
226 if (index > l->size())
227 return (*l)[l->size()-1];
228
229 return (*l)[index-1];
230}
231
233{
234 std::vector<VuoLeapPointable> * l = (std::vector<VuoLeapPointable> *)list;
235
236 if (!l || l->size() == 0)
237 return NULL;
238
239 return &((*l)[0]);
240}
241
242void VuoListForeach_VuoLeapPointable(const VuoList_VuoLeapPointable list, bool (^function)(const VuoLeapPointable value))
243{
244 auto l = reinterpret_cast<const std::vector<VuoLeapPointable> *>(list);
245
246 if (!l || l->size() == 0)
247 return;
248
249 for (auto item : *l)
250 if (!function(item))
251 break;
252}
253
254void VuoListSetValue_VuoLeapPointable(const VuoList_VuoLeapPointable list, const VuoLeapPointable value, const unsigned long index, bool expandListIfNeeded)
255{
256 if (!list)
257 return;
258
259 std::vector<VuoLeapPointable> * l = (std::vector<VuoLeapPointable> *)list;
260
261 if (!expandListIfNeeded && l->size() == 0)
262 return;
263
264 unsigned long clampedIndex = index - 1;
265
266 if (index == 0)
267 clampedIndex = 0;
268
269 if (expandListIfNeeded && clampedIndex >= l->size())
270 {
271 l->resize(clampedIndex + 1);
272 (*l)[clampedIndex] = value;
273 RETAIN(value);
274 }
275 else
276 {
277 if (index > l->size())
278 clampedIndex = l->size() - 1;
279
280#if 0 != 0
281 VuoLeapPointable oldValue = (*l)[clampedIndex];
282#endif
283 (*l)[clampedIndex] = value;
284 RETAIN(value);
285 RELEASE(oldValue);
286 }
287}
288
289void VuoListInsertValue_VuoLeapPointable(const VuoList_VuoLeapPointable list, const VuoLeapPointable value, const unsigned long index)
290{
291 if (!list)
292 return;
293
294 std::vector<VuoLeapPointable> * l = (std::vector<VuoLeapPointable> *)list;
295
296 unsigned long clampedIndex = index - 1;
297
298 if (index == 0)
299 clampedIndex = 0;
300
301 if (index > l->size())
302 clampedIndex = l->size() - 1;
303
304 RETAIN(value);
305 if (index <= l->size())
306 l->insert(l->begin() + clampedIndex, value);
307 else
308 l->push_back(value);
309}
310
312{
313 if (!list)
314 return;
315
316 std::vector<VuoLeapPointable> * l = (std::vector<VuoLeapPointable> *)list;
317 RETAIN(value);
318 l->insert(l->begin(), value);
319}
320
322{
323 if (!list)
324 return;
325
326 std::vector<VuoLeapPointable> * l = (std::vector<VuoLeapPointable> *)list;
327 RETAIN(value);
328 l->push_back(value);
329}
330
331void VuoListExchangeValues_VuoLeapPointable(VuoList_VuoLeapPointable list, const unsigned long indexA, const unsigned long indexB)
332{
333 if (!list)
334 return;
335
336 std::vector<VuoLeapPointable> * l = (std::vector<VuoLeapPointable> *)list;
337
338 size_t size = l->size();
339 if (size == 0)
340 return;
341
342 unsigned long clampedIndexA = indexA - 1;
343 if (indexA == 0)
344 clampedIndexA = 0;
345 if (indexA > size)
346 clampedIndexA = size - 1;
347
348 unsigned long clampedIndexB = indexB - 1;
349 if (indexB == 0)
350 clampedIndexB = 0;
351 if (indexB > size)
352 clampedIndexB = size - 1;
353
354 VuoLeapPointable value = (*l)[clampedIndexA];
355 (*l)[clampedIndexA] = (*l)[clampedIndexB];
356 (*l)[clampedIndexB] = value;
357}
358
359#ifdef VuoLeapPointable_SUPPORTS_COMPARISON
360void VuoListSort_VuoLeapPointable(VuoList_VuoLeapPointable list)
361{
362 if (!list)
363 return;
364
365 std::vector<VuoLeapPointable> * l = (std::vector<VuoLeapPointable> *)list;
366
367 size_t size = l->size();
368 if (size < 2)
369 return;
370
371 std::sort(l->begin(), l->end(), VuoLeapPointable_isLessThan);
372}
373
374bool VuoList_VuoLeapPointable_areEqual(const VuoList_VuoLeapPointable _a, const VuoList_VuoLeapPointable _b)
375{
376 if (_a == _b)
377 return true;
378
379 if (!_a || !_b)
380 return false;
381
382 std::vector<VuoLeapPointable> *a = (std::vector<VuoLeapPointable> *)_a;
383 std::vector<VuoLeapPointable> *b = (std::vector<VuoLeapPointable> *)_b;
384 if (a->size() != b->size())
385 return false;
386
387 for (std::vector<VuoLeapPointable>::iterator ia = a->begin(), ib = b->begin(); ia != a->end(); ++ia, ++ib)
388 if (!VuoLeapPointable_areEqual(*ia, *ib))
389 return false;
390
391 return true;
392}
393
394bool VuoList_VuoLeapPointable_isLessThan(const VuoList_VuoLeapPointable _a, const VuoList_VuoLeapPointable _b)
395{
396 if (!_a || !_b)
397 return _a < _b;
398
399 std::vector<VuoLeapPointable> *a = (std::vector<VuoLeapPointable> *)_a;
400 std::vector<VuoLeapPointable> *b = (std::vector<VuoLeapPointable> *)_b;
401 if (a->size() < b->size()) return true;
402 if (a->size() > b->size()) return false;
403
404 for (std::vector<VuoLeapPointable>::iterator ia = a->begin(), ib = b->begin(); ia != a->end(); ++ia, ++ib)
405 {
406 if (VuoLeapPointable_isLessThan(*ia, *ib)) return true;
407 if (VuoLeapPointable_isLessThan(*ib, *ia)) return false;
408 }
409
410 return false;
411}
412#endif
413
415{
416 if (!list)
417 return;
418
419 std::vector<VuoLeapPointable> * l = (std::vector<VuoLeapPointable> *)list;
420
421 size_t size = l->size();
422 if (size == 0)
423 return;
424
425 double clampedChaos = MIN(MAX(chaos,0),1);
426 for (unsigned long i = 0; i < size * clampedChaos; ++i)
427 {
428 unsigned long j = VuoInteger_random(i, size-1);
429 VuoLeapPointable value = (*l)[i];
430 (*l)[i] = (*l)[j];
431 (*l)[j] = value;
432 }
433}
434
436{
437 if (!list)
438 return;
439
440 std::vector<VuoLeapPointable> * l = (std::vector<VuoLeapPointable> *)list;
441 std::reverse(l->begin(), l->end());
442}
443
444VuoList_VuoLeapPointable VuoListSubset_VuoLeapPointable(VuoList_VuoLeapPointable list, const signed long startIndex, const unsigned long itemCount)
445{
446 if (!list)
447 return NULL;
448
449 std::vector<VuoLeapPointable> * l = (std::vector<VuoLeapPointable> *)list;
450
451 size_t size = l->size();
452 if (size == 0)
453 return NULL;
454
455 signed long clampedStartIndex = startIndex - 1;
456 signed long clampedEndIndex = clampedStartIndex + itemCount - 1;
457
458 if (clampedStartIndex < 0)
459 clampedStartIndex = 0;
460 if (clampedEndIndex >= (signed long)size)
461 clampedEndIndex = size - 1;
462
463 if (clampedStartIndex > clampedEndIndex)
464 return NULL;
465
466 std::vector<VuoLeapPointable> *newList = new std::vector<VuoLeapPointable>(
467 l->begin() + clampedStartIndex,
468 l->begin() + clampedEndIndex + 1);
470
471 for (std::vector<VuoLeapPointable>::iterator i = newList->begin(); i != newList->end(); ++i)
472 RETAIN(*i);
473
474 return reinterpret_cast<VuoList_VuoLeapPointable>(newList);
475}
476
477#ifdef VuoLeapPointable_SUPPORTS_COMPARISON
478VuoList_VuoLeapPointable VuoListRemoveDuplicates_VuoLeapPointable(VuoList_VuoLeapPointable list)
479{
480 if (!list)
481 return NULL;
482
483 auto *l = (std::vector<VuoLeapPointable> *)list;
484
485 size_t size = l->size();
486 if (size == 0)
487 return NULL;
488
489 auto *newList = new std::vector<VuoLeapPointable>;
491
492 for (auto i = l->begin(); i != l->end(); ++i)
493 {
494 bool found = false;
495 for (auto j = newList->begin(); j != newList->end(); ++j)
496 if (VuoLeapPointable_areEqual(*i, *j))
497 {
498 found = true;
499 break;
500 }
501 if (!found)
502 newList->push_back(*i);
503 }
504
505 for (auto i = newList->begin(); i != newList->end(); ++i)
506 RETAIN(*i);
507
508 return reinterpret_cast<VuoList_VuoLeapPointable>(newList);
509}
510#endif
511
513{
514 if (!list)
515 return;
516
517 std::vector<VuoLeapPointable> * l = (std::vector<VuoLeapPointable> *)list;
518
519 if (!l->size())
520 return;
521
522 RELEASE(l->front());
523 l->erase(l->begin());
524}
525
527{
528 if (!list)
529 return;
530
531 std::vector<VuoLeapPointable> * l = (std::vector<VuoLeapPointable> *)list;
532
533 if (!l->size())
534 return;
535
536 RELEASE(l->back());
537 l->pop_back();
538}
539
541{
542 if (!list)
543 return;
544
545 while (VuoListGetCount_VuoLeapPointable(list) > 0)
547}
548
550{
551 if (!list)
552 return;
553
554 if (index == 0)
555 return;
556
557 std::vector<VuoLeapPointable> * l = (std::vector<VuoLeapPointable> *)list;
558
559 size_t size = l->size();
560 if (size == 0 || index > size)
561 return;
562
563 l->erase(l->begin() + index - 1);
564}
565
567{
568 if (!list)
569 return 0;
570
571 std::vector<VuoLeapPointable> * l = (std::vector<VuoLeapPointable> *)list;
572 return l->size();
573}
574
576{
577 VuoRetain(value);
578}
579
581{
582 VuoRelease(value);
583}