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