File: container_handling.h

package info (click to toggle)
renderdoc 1.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 79,584 kB
  • sloc: cpp: 491,671; ansic: 285,823; python: 12,617; java: 11,345; cs: 7,181; makefile: 6,703; yacc: 5,682; ruby: 4,648; perl: 3,461; php: 2,119; sh: 2,068; lisp: 1,835; tcl: 1,068; ml: 747; xml: 137
file content (691 lines) | stat: -rw-r--r-- 17,047 bytes parent folder | download
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/******************************************************************************
 * The MIT License (MIT)
 *
 * Copyright (c) 2017-2018 Baldur Karlsson
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/

#pragma once

#include <algorithm>

///////////////////////////////////////////////////////////////////////////
// utility function to get the real C++ this pointer out of PyObject* given
// a type, so we can modify it by reference if we want
template <typename arrayType>
arrayType *array_thisptr(PyObject *self)
{
  void *ptr = NULL;
  int res = 0;
  swig_type_info *typeInfo = TypeConversion<arrayType>::GetTypeInfo();

  if(!typeInfo)
    SWIG_exception_fail(SWIG_RuntimeError, "Internal error fetching type info");

  res = SWIG_ConvertPtr(self, &ptr, typeInfo, 0);
  if(!SWIG_IsOK(res))
    SWIG_exception_fail(SWIG_ArgError(res), "Couldn't convert array type");

  return (arrayType *)ptr;

fail:
  return NULL;
}

///////////////////////////////////////////////////////////////////////////
// Bit of a hack - use a dispatch struct templated on a constant bool, so
// we can do a compile-time check if we're converting 'self' and only invoke
// array_thisptr for arrays that we want to be modifying by reference.
template <bool isSelf>
struct self_dispatch
{
  template <typename arrayType>
  static arrayType *getthis(PyObject *self)
  {
    return NULL;
  }
};

template <>
struct self_dispatch<true>
{
  template <typename arrayType>
  static arrayType *getthis(PyObject *self)
  {
    return ::array_thisptr<arrayType>(self);
  }
};
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
// templated implementations of array functions for both slots and named
// functions in python sequences

// returns the index into an array, including reverseing negative indices to refer to elements from
// the end of the array (-1 = last, -2 = second last, etc).
template <typename arrayType>
Py_ssize_t array_revindex(arrayType *thisptr, PyObject *index)
{
  Py_ssize_t idx = 0;

  if(!PyIndex_Check(index))
    SWIG_exception_fail(SWIG_TypeError, "invalid index type");

  idx = PyNumber_AsSsize_t(index, PyExc_IndexError);

  // don't need to fail, it's already been thrown
  if(idx == -1 && PyErr_Occurred())
    return PY_SSIZE_T_MIN;

  // if the index is negative, treat it as an offset from the end, with -1 being not last but
  // next-to-last
  if(idx < 0)
    idx = (Py_ssize_t)thisptr->size() + idx;

  return idx;
fail:
  return PY_SSIZE_T_MIN;
}

template <typename arrayType>
PyObject *array_repr(arrayType *thisptr)
{
  PyObject *result = NULL;
  PyObject *list = NULL;

  // make a copy as a python list
  list = ConvertToPy(*thisptr);

  if(list == NULL)
    SWIG_exception_fail(SWIG_ValueError, "invalid array");

  // use the python list repr function
  result = PyObject_Repr(list);
  Py_DECREF(list);
  return result;
fail:
  return NULL;
}

template <typename arrayType>
PyObject *array_getitem(arrayType *thisptr, Py_ssize_t idx)
{
  if(idx < 0 || (size_t)idx >= thisptr->size())
    SWIG_exception_fail(SWIG_IndexError, "list index out of range");

  return ConvertToPy(thisptr->at(idx));
fail:
  return NULL;
}

template <typename arrayType>
int array_setitem(arrayType *thisptr, Py_ssize_t idx, PyObject *val)
{
  int res = SWIG_OK;

  if(idx < 0 || (size_t)idx >= thisptr->size())
    SWIG_exception_fail(SWIG_IndexError, "list assignment index out of range");

  ExtRefcount<typename arrayType::value_type>::Dec(thisptr->at(idx));

  if(val == NULL)
  {
    thisptr->erase(idx);
    res = SWIG_OK;
  }
  else
  {
    res = ConvertFromPy(val, thisptr->at(idx));
  }

  if(SWIG_IsOK(res))
    return 0;
fail:
  return -1;
}

template <typename arrayType>
Py_ssize_t array_len(arrayType *thisptr)
{
  return thisptr->size();
}

template <typename arrayType>
PyObject *array_clear(arrayType *thisptr)
{
  for(size_t i = 0; i < thisptr->size(); i++)
    ExtRefcount<typename arrayType::value_type>::Dec(thisptr->at(i));

  thisptr->clear();
  return SWIG_Py_Void();
}

template <typename arrayType>
PyObject *array_reverse(arrayType *thisptr)
{
  std::reverse(thisptr->begin(), thisptr->end());
  return SWIG_Py_Void();
}

template <typename arrayType>
PyObject *array_copy(arrayType *thisptr)
{
  PyObject *list = PyList_New(0);
  if(!list)
    return NULL;

  PyObject *ret = NULL;

  for(size_t i = 0; i < thisptr->size(); i++)
  {
    ret = ConvertToPy(thisptr->at(i));

    PyList_Append(list, ret);

    if(!ret)
      SWIG_exception_fail(SWIG_TypeError, "failed to convert element while copying");
  }

  return list;
fail:
  if(list)
    Py_XDECREF(list);

  return NULL;
}

template <typename arrayType>
PyObject *array_sort(arrayType *thisptr, PyObject *key, bool reverse)
{
  typedef typename arrayType::value_type val;
  if(key)
  {
    SWIG_exception_fail(SWIG_RuntimeError, "key sort is not supported on rdcarray");
  }
  else
  {
    std::sort(thisptr->begin(), thisptr->end(), [](const val &a, const val &b) { return a < b; });
  }

  if(reverse)
    array_reverse(thisptr);

  return SWIG_Py_Void();

fail:
  return NULL;
}

template <typename arrayType>
PyObject *array_append(arrayType *thisptr, PyObject *value)
{
  typename arrayType::value_type converted;
  int res = ConvertFromPy(value, converted);

  if(SWIG_IsOK(res))
  {
    thisptr->push_back(converted);
    return SWIG_Py_Void();
  }

  SWIG_exception_fail(SWIG_ArgError(res), "failed to convert element while appending");
fail:
  return NULL;
}

template <typename arrayType>
PyObject *array_insert(arrayType *thisptr, PyObject *index, PyObject *value)
{
  typename arrayType::value_type converted;
  int res = 0;
  Py_ssize_t idx = array_revindex(thisptr, index);

  // if an error occurred an exception has been thrown, just return NULL
  if(idx == PY_SSIZE_T_MIN)
    return NULL;

  // insert clamps the index
  if(idx < 0)
    idx = 0;
  if(idx > thisptr->count())
    idx = thisptr->count();

  res = ConvertFromPy(value, converted);

  if(SWIG_IsOK(res))
  {
    thisptr->insert(idx, converted);
    return SWIG_Py_Void();
  }

  SWIG_exception_fail(SWIG_ArgError(res), "failed to convert element while inserting");
fail:
  return NULL;
}

template <typename arrayType>
PyObject *array_pop(arrayType *thisptr, PyObject *index)
{
  PyObject *ret = NULL;
  Py_ssize_t idx = index ? array_revindex(thisptr, index) : thisptr->size() - 1;

  // if an error occurred an exception has been thrown, just return NULL
  if(idx == PY_SSIZE_T_MIN)
    return NULL;

  if(idx < 0 || idx > thisptr->count())
    SWIG_exception_fail(SWIG_IndexError, "pop index out of range");

  if(thisptr->empty())
    SWIG_exception_fail(SWIG_IndexError, "pop from empty list");

  ret = ConvertToPy(thisptr->at(idx));

  if(!ret)
    SWIG_exception_fail(SWIG_TypeError, "failed to convert element while popping");

  ExtRefcount<typename arrayType::value_type>::Dec(thisptr->at(idx));
  thisptr->erase(idx);
  return ret;
fail:
  return NULL;
}

template <typename arrayType>
PyObject *array_indexOf(arrayType *thisptr, PyObject *item, PyObject *start, PyObject *end)
{
  typename arrayType::value_type converted;
  int res = 0;

  size_t startIdx = 0;
  size_t endIdx = ~0U;

  if(start)
  {
    if(!PyLong_Check(start))
    {
      SWIG_exception_fail(SWIG_TypeError, "start index is not an integer");
    }

    startIdx = (size_t)PyLong_AsLong(start);
  }

  if(end)
  {
    if(!PyLong_Check(end))
    {
      SWIG_exception_fail(SWIG_TypeError, "end index is not an integer");
    }
    endIdx = (size_t)PyLong_AsLong(end);
  }

  res = ConvertFromPy(item, converted);

  if(SWIG_IsOK(res))
  {
    int idx = thisptr->indexOf(converted, startIdx, endIdx);

    if(idx < 0)
    {
      SWIG_exception_fail(SWIG_ValueError, "item is not in list");
    }

    return PyLong_FromLong(idx);
  }

  SWIG_exception_fail(SWIG_ArgError(res), "failed to convert element in index");
fail:
  return NULL;
}

template <typename arrayType>
PyObject *array_countOf(arrayType *thisptr, PyObject *item)
{
  typename arrayType::value_type converted;
  int res = ConvertFromPy(item, converted);

  if(SWIG_IsOK(res))
  {
    int count = 0;

    for(size_t i = 0; i < thisptr->size(); i++)
      if(thisptr->at(i) == converted)
        count++;

    return PyLong_FromLong(count);
  }

  SWIG_exception_fail(SWIG_ArgError(res), "failed to convert element in count");
fail:
  return NULL;
}

template <typename arrayType>
PyObject *array_removeOne(arrayType *thisptr, PyObject *item)
{
  typename arrayType::value_type converted;
  int res = ConvertFromPy(item, converted);

  if(SWIG_IsOK(res))
  {
    int idx = thisptr->indexOf(converted);

    if(idx < 0)
    {
      SWIG_exception_fail(SWIG_ValueError, "item is not in list");
    }

    thisptr->erase(idx);

    return SWIG_Py_Void();
  }

  SWIG_exception_fail(SWIG_ArgError(res), "failed to convert element in remove");
fail:
  return NULL;
}

template <typename arrayType>
PyObject *array_concat(arrayType *thisptr, PyObject *items)
{
  Py_ssize_t count = 0;
  PyObject *list = NULL;
  PyObject *ret = NULL;

  if(!PySequence_Check(items))
    SWIG_exception_fail(SWIG_TypeError, "can't concatenate non-sequence");

  list = PyList_New(0);
  if(!list)
    return NULL;

  for(size_t i = 0; i < thisptr->size(); i++)
  {
    ret = ConvertToPy(thisptr->at(i));

    PyList_Append(list, ret);

    if(!ret)
      SWIG_exception_fail(SWIG_TypeError, "failed to convert element while copying");
  }

  count = PySequence_Size(items);

  for(Py_ssize_t i = 0; i < count; i++)
  {
    PyObject *item = PySequence_GetItem(items, i);
    PyList_Append(list, item);
    Py_DECREF(item);
  }

  return list;
fail:
  if(list)
    Py_XDECREF(list);

  return NULL;
}

template <typename arrayType>
PyObject *array_selfconcat(arrayType *thisptr, PyObject *items)
{
  typename arrayType::value_type converted;
  int res = 0;
  Py_ssize_t count = 0;

  if(!PySequence_Check(items))
    SWIG_exception_fail(SWIG_TypeError, "can't concatenate non-sequence");

  count = PySequence_Size(items);

  for(Py_ssize_t i = 0; i < count; i++)
  {
    PyObject *item = PySequence_GetItem(items, i);
    res = ConvertFromPy(item, converted);

    if(SWIG_IsOK(res))
      thisptr->push_back(converted);

    Py_DECREF(item);

    if(!SWIG_IsOK(res))
      SWIG_exception_fail(SWIG_ArgError(res), "failed to convert element in extend");
  }

  return SWIG_Py_Void();

fail:
  return NULL;
}

template <typename arrayType>
PyObject *array_repeat(arrayType *thisptr, Py_ssize_t count)
{
  PyObject *list = PyList_New(0);
  if(!list)
    return NULL;

  if(count <= 0 || thisptr->empty())
  {
    return list;
  }

  PyObject *ret = NULL;
  rdcarray<PyObject *> converted;

  for(size_t i = 0; i < thisptr->size(); i++)
  {
    ret = ConvertToPy(thisptr->at(i));

    converted.push_back(ret);

    if(!ret)
      SWIG_exception_fail(SWIG_TypeError, "failed to convert element while copying");
  }

  for(Py_ssize_t c = 0; c < count; c++)
    for(size_t i = 0; i < converted.size(); i++)
      PyList_Append(list, converted[i]);

  return list;
fail:
  if(list)
    Py_XDECREF(list);

  return NULL;
}

template <typename arrayType>
PyObject *array_selfrepeat(arrayType *thisptr, Py_ssize_t count)
{
  if(count <= 0 || thisptr->empty())
  {
    thisptr->clear();
    return SWIG_Py_Void();
  }

  size_t origCount = thisptr->size();

  thisptr->reserve(origCount * count);
  for(Py_ssize_t i = 0; i < count - 1; i++)
  {
    thisptr->append(thisptr->data(), origCount);
  }

  return SWIG_Py_Void();
}

template <typename arrayType>
PyObject *array_getsubscript(arrayType *thisptr, PyObject *idxobj)
{
  if(PyIndex_Check(idxobj))
  {
    Py_ssize_t idx = array_revindex(thisptr, idxobj);

    // if an error occurred an exception has been thrown, just return NULL
    if(idx == PY_SSIZE_T_MIN)
      return NULL;

    return array_getitem(thisptr, idx);
  }

  if(PySlice_Check(idxobj))
  {
    int len = thisptr->count();
    Py_ssize_t start, stop, step, slicelength;

    if(PySlice_GetIndicesEx(idxobj, len, &start, &stop, &step, &slicelength) < 0)
      return NULL;

    PyObject *list = PyList_New(0);
    if(!list)
      return NULL;

    PyObject *ret = NULL;

    for(Py_ssize_t i = start, count = 0; count < slicelength; i += step, count++)
    {
      ret = ConvertToPy(thisptr->at(i));

      PyList_Append(list, ret);

      if(!ret)
      {
        Py_DECREF(list);
        SWIG_exception_fail(SWIG_TypeError, "failed to convert element while getting slice");
      }
    }

    return list;
  }

  SWIG_exception_fail(SWIG_TypeError, "list index not index or slice");

fail:
  return NULL;
}

template <typename arrayType>
int array_setsubscript(arrayType *thisptr, PyObject *idxobj, PyObject *val)
{
  typename arrayType::value_type converted;
  int res = 0;

  if(PyIndex_Check(idxobj))
  {
    Py_ssize_t idx = array_revindex(thisptr, idxobj);

    // if an error occurred an exception has been thrown, just return NULL
    if(idx == PY_SSIZE_T_MIN)
      return -1;

    return array_setitem(thisptr, idx, val);
  }

  if(PySlice_Check(idxobj))
  {
    int len = thisptr->count();
    Py_ssize_t start, stop, step, slicelength;

    if(PySlice_GetIndicesEx(idxobj, len, &start, &stop, &step, &slicelength) < 0)
    {
      return -1;
    }

    if(val == NULL)
    {
      // we're deleting this slice. Erase all the indices

      for(Py_ssize_t i = start, count = 0; count < slicelength; i += step, count++)
      {
        Py_ssize_t idx = i;

        // if we're stepping forwards, erasing the earlier indices will have moved the ones to
        // delete, so adjust the index based on how many we've deleted.
        // if we're stepping backwards then there's no need to change anything
        if(step >= 1)
          idx -= count;

        ExtRefcount<typename arrayType::value_type>::Dec(thisptr->at((size_t)idx));
        thisptr->erase((size_t)idx);
      }
    }
    else
    {
      // we must be assigning an iterable object
      if(!PySequence_Check(val))
      {
        SWIG_exception_fail(SWIG_TypeError, "can only assign an iterable");
      }

      Py_ssize_t vallen = PySequence_Size(val);

      // if the range isn't contiguous or reversed or something, the input size must match
      if(step != 1)
      {
        if(slicelength != vallen)
        {
          SWIG_exception_fail(SWIG_ValueError,
                              "can't assign sequence of different size to extended slice");
        }

        for(Py_ssize_t i = start, count = 0; count < slicelength; i += step, count++)
        {
          // dec refcount on previous item in this index
          ExtRefcount<typename arrayType::value_type>::Dec(thisptr->at(i));

          // convert the input item
          PyObject *item = PySequence_GetItem(val, count);
          res = ConvertFromPy(item, thisptr->at(i));
          Py_DECREF(item);

          if(!SWIG_IsOK(res))
            SWIG_exception_fail(SWIG_ArgError(res), "failed to convert element in slice set");
        }
      }
      else
      {
        // the range is contiguous. First erase it, dec refcount if needed
        for(Py_ssize_t i = start; i < start + slicelength; i++)
          ExtRefcount<typename arrayType::value_type>::Dec(thisptr->at((size_t)i));
        thisptr->erase(start, slicelength);

        // then insert the new items
        for(Py_ssize_t count = 0; count < vallen; count++)
        {
          PyObject *item = PySequence_GetItem(val, count);
          res = ConvertFromPy(item, converted);
          Py_DECREF(item);

          if(!SWIG_IsOK(res))
            SWIG_exception_fail(SWIG_ArgError(res), "failed to convert element in slice set");

          thisptr->insert(count + start, converted);
        }
      }
    }

    return 0;
  }

  SWIG_exception_fail(SWIG_TypeError, "list index not index or slice");
fail:
  return -1;
}