Loading...
Searching...
No Matches
Slice.h
Go to the documentation of this file.
1// BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE
2
3#ifndef AWKWARD_SLICE_H_
4#define AWKWARD_SLICE_H_
5
6#include <string>
7#include <vector>
8#include <memory>
9
10#include "awkward/common.h"
11#include "awkward/Index.h"
12
13namespace awkward {
14 class SliceItem;
15 using SliceItemPtr = std::shared_ptr<SliceItem>;
16
22 public:
26 virtual ~SliceItem();
27
29 virtual const SliceItemPtr
30 shallow_copy() const = 0;
31
34 virtual const std::string
35 tostring() const = 0;
36
43 virtual bool
44 preserves_type(const Index64& advanced) const = 0;
45
50 virtual bool
52 };
53
59 public:
64
67 at() const;
68
69 const SliceItemPtr
70 shallow_copy() const override;
71
72 const std::string
73 tostring() const override;
74
78 bool
79 preserves_type(const Index64& advanced) const override;
80
81 virtual bool
82 referentially_equal(const SliceItemPtr& other) const override;
83
84 private:
86 const int64_t at_;
87 };
88
94 public:
105 SliceRange(int64_t start, int64_t stop, int64_t step);
106
112 int64_t
113 start() const;
114
120 int64_t
121 stop() const;
122
128 int64_t
129 step() const;
130
133 bool
134 hasstart() const;
135
138 bool
139 hasstop() const;
140
141 const SliceItemPtr
142 shallow_copy() const override;
143
144 const std::string
145 tostring() const override;
146
150 bool
151 preserves_type(const Index64& advanced) const override;
152
153 virtual bool
154 referentially_equal(const SliceItemPtr& other) const override;
155
156 private:
158 const int64_t start_;
160 const int64_t stop_;
162 const int64_t step_;
163 };
164
170 public:
173
174 const SliceItemPtr
175 shallow_copy() const override;
176
177 const std::string
178 tostring() const override;
179
183 bool
184 preserves_type(const Index64& advanced) const override;
185
186 virtual bool
187 referentially_equal(const SliceItemPtr& other) const override;
188 };
189
198 public:
201
202 const SliceItemPtr
203 shallow_copy() const override;
204
205 const std::string
206 tostring() const override;
207
211 bool
212 preserves_type(const Index64& advanced) const override;
213
214 virtual bool
215 referentially_equal(const SliceItemPtr& other) const override;
216 };
217
224 template <typename T>
225 class
226#ifdef AWKWARD_SLICE_NO_EXTERN_TEMPLATE
228#endif
229 SliceArrayOf: public SliceItem {
230 public:
250 const std::vector<int64_t>& shape,
251 const std::vector<int64_t>& strides, bool frombool);
252
257 const IndexOf<T>
258 index() const;
259
266 const std::vector<int64_t>
267 shape() const;
268
275 const std::vector<int64_t>
276 strides() const;
277
282 bool
283 frombool() const;
284
288 const int64_t
289 length() const;
290
292 int64_t
293 ndim() const;
294
295 const SliceItemPtr
296 shallow_copy() const override;
297
298 const std::string
299 tostring() const override;
300
301 const std::string
303
308 bool
309 preserves_type(const Index64& advanced) const override;
310
311 virtual bool
312 referentially_equal(const SliceItemPtr& other) const override;
313
316 const IndexOf<T>
317 ravel() const;
318
319 private:
321 const IndexOf<T> index_;
323 const std::vector<int64_t> shape_;
325 const std::vector<int64_t> strides_;
327 bool frombool_;
328 };
329
330#ifndef AWKWARD_SLICE_NO_EXTERN_TEMPLATE
331 extern template class SliceArrayOf<int64_t>;
332#endif
333
335
341 public:
349 SliceField(const std::string& key);
350
357 const std::string
358 key() const;
359
360 const SliceItemPtr
361 shallow_copy() const override;
362
363 const std::string
364 tostring() const override;
365
369 bool
370 preserves_type(const Index64& advanced) const override;
371
372 virtual bool
373 referentially_equal(const SliceItemPtr& other) const override;
374
375 private:
377 const std::string key_;
378 };
379
385 public:
393 SliceFields(const std::vector<std::string>& keys);
394
401 const std::vector<std::string>
402 keys() const;
403
404 const SliceItemPtr
405 shallow_copy() const override;
406
407 const std::string
408 tostring() const override;
409
413 bool
414 preserves_type(const Index64& advanced) const override;
415
416 virtual bool
417 referentially_equal(const SliceItemPtr& other) const override;
418
419 private:
421 const std::vector<std::string> keys_;
422 };
423
430 template <typename T>
431 class
432#ifdef AWKWARD_SLICE_NO_EXTERN_TEMPLATE
434#endif
435 SliceMissingOf: public SliceItem {
436 public:
446 const Index8& originalmask,
447 const SliceItemPtr& content);
448
451 const IndexOf<T>
452 index() const;
453
456 const Index8
458
461 const SliceItemPtr
462 content() const;
463
465 int64_t
466 length() const;
467
468 const SliceItemPtr
469 shallow_copy() const override;
470
471 const std::string
472 tostring() const override;
473
474 const std::string
476
482 bool
483 preserves_type(const Index64& advanced) const override;
484
485 virtual bool
486 referentially_equal(const SliceItemPtr& other) const override;
487
488 private:
490 const IndexOf<T> index_;
492 const Index8 originalmask_;
494 const SliceItemPtr content_;
495 };
496
497#ifndef AWKWARD_SLICE_NO_EXTERN_TEMPLATE
498 extern template class SliceMissingOf<int64_t>;
499#endif
500
502
510 template <typename T>
511 class
512#ifdef AWKWARD_SLICE_NO_EXTERN_TEMPLATE
514#endif
515 SliceJaggedOf: public SliceItem {
516 public:
527 SliceJaggedOf(const IndexOf<T>& offsets, const SliceItemPtr& content);
528
536 const IndexOf<T>
537 offsets() const;
538
541 const SliceItemPtr
542 content() const;
543
545 int64_t
546 length() const;
547
548 const SliceItemPtr
549 shallow_copy() const override;
550
551 const std::string
552 tostring() const override;
553
554 const std::string
556
562 bool
563 preserves_type(const Index64& advanced) const override;
564
565 virtual bool
566 referentially_equal(const SliceItemPtr& other) const override;
567
568 private:
570 const IndexOf<T> offsets_;
572 const SliceItemPtr content_;
573 };
574
575#ifndef AWKWARD_SLICE_NO_EXTERN_TEMPLATE
576 extern template class SliceJaggedOf<int64_t>;
577#endif
578
580
586 public:
590 static int64_t none();
591
597 Slice(const std::vector<SliceItemPtr>& items, bool sealed);
598
602 Slice(const std::vector<SliceItemPtr>& items);
603
606
608 const std::vector<SliceItemPtr>
609 items() const;
610
613 bool
614 sealed() const;
615
617 int64_t
618 length() const;
619
622 int64_t
623 dimlength() const;
624
626 const SliceItemPtr
627 head() const;
628
630 const Slice
631 tail() const;
632
634 const Slice
635 only_fields() const;
636
638 const Slice
639 not_fields() const;
640
643 const std::string
644 tostring() const;
645
647 const Slice
649
651 void
653
655 void
657
659 void
661
663 void
665
667 void
669
672 void
674
676 void
678
680 void
682
685 void
687
690 void
692
694 void
696
702 bool
703 isadvanced() const;
704
709 bool
711
712 private:
714 std::vector<SliceItemPtr> items_;
716 bool sealed_;
717 };
718
719}
720
721#endif // AWKWARD_SLICE_H_
A contiguous, one-dimensional array of integers used to represent data structures,...
Definition Index.h:82
Filters, rearranges, and/or duplicates items in its content through an index, which has the same effe...
Definition IndexedArray.h:204
Represents an array of integers in a slice (possibly converted from an array of booleans).
Definition Slice.h:229
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
const IndexOf< T > index() const
A flattened version of the array used for slicing.
const int64_t length() const
The length of the logical array: shape[0].
bool frombool() const
If true, this integer array of positions was derived from a boolean array mask (from NumPy's nonzero ...
const std::vector< int64_t > strides() const
Length of each dimension in number of items. The length of strides must match the length of shape.
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
int64_t ndim() const
The number of dimensions: shape.size().
virtual bool referentially_equal(const SliceItemPtr &other) const override
Returns true if this slice item has all the same buffers and parameters as other; false otherwise.
const std::vector< int64_t > shape() const
Number of elements in each dimension, like NumPy's array shape.
const IndexOf< T > ravel() const
Returns a one-dimensional contiguous version of the array, like NumPy's ravel.
const std::string tostring_part() const
bool preserves_type(const Index64 &advanced) const override
Returns true if this slice would preserve an array's slice and therefore should pass on Content::para...
Represents an integer in a tuple of slices passed to __getitem__ in Python.
Definition Slice.h:58
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
SliceAt(int64_t at)
Creates a SliceAt from a full set of parameters.
int64_t at() const
The integer that this slice item represents.
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
virtual bool referentially_equal(const SliceItemPtr &other) const override
Returns true if this slice item has all the same buffers and parameters as other; false otherwise.
bool preserves_type(const Index64 &advanced) const override
Returns true if this slice would preserve an array's slice and therefore should pass on Content::para...
Represents a Python Ellipsis object (usual syntax: array[...]).
Definition Slice.h:169
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
SliceEllipsis()
Creates a SliceEllipsis.
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
virtual bool referentially_equal(const SliceItemPtr &other) const override
Returns true if this slice item has all the same buffers and parameters as other; false otherwise.
bool preserves_type(const Index64 &advanced) const override
Returns true if this slice would preserve an array's slice and therefore should pass on Content::para...
Represents a single string in a slice tuple, indicating that a RecordArray should be replaced by one ...
Definition Slice.h:340
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
SliceField(const std::string &key)
Creates a SliceField from a full set of parameters.
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
const std::string key() const
The name of the field to select.
virtual bool referentially_equal(const SliceItemPtr &other) const override
Returns true if this slice item has all the same buffers and parameters as other; false otherwise.
bool preserves_type(const Index64 &advanced) const override
Returns true if this slice would preserve an array's slice and therefore should pass on Content::para...
Definition Slice.h:384
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
SliceFields(const std::vector< std::string > &keys)
Creates a SliceFields from a full set of parameters.
const std::vector< std::string > keys() const
The names of the fields to select.
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
virtual bool referentially_equal(const SliceItemPtr &other) const override
Returns true if this slice item has all the same buffers and parameters as other; false otherwise.
bool preserves_type(const Index64 &advanced) const override
Returns true if this slice would preserve an array's slice and therefore should pass on Content::para...
Abstract class for slice items, which are elements of a tuple passed to an array's __getitem__ in Pyt...
Definition Slice.h:21
virtual const SliceItemPtr shallow_copy() const =0
Copies this node without copying any associated arrays.
virtual const std::string tostring() const =0
Returns a string representation of this slice item (single-line custom format).
virtual bool referentially_equal(const SliceItemPtr &other) const =0
Returns true if this slice item has all the same buffers and parameters as other; false otherwise.
virtual bool preserves_type(const Index64 &advanced) const =0
Returns true if this slice would preserve an array's slice and therefore should pass on Content::para...
virtual ~SliceItem()
Virtual destructor acts as a first non-inline virtual function that determines a specific translation...
Represents an array of nested lists, where the content may be SliceArrayOf, SliceMissingOf,...
Definition Slice.h:515
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
SliceJaggedOf(const IndexOf< T > &offsets, const SliceItemPtr &content)
Creates a SliceJaggedOf with a full set of parameters.
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
virtual bool referentially_equal(const SliceItemPtr &other) const override
Returns true if this slice item has all the same buffers and parameters as other; false otherwise.
const IndexOf< T > offsets() const
Positions where one nested list stops and the next starts in the content in the same sense as ListOff...
int64_t length() const
The length of the array: len(offsets) - 1.
const SliceItemPtr content() const
The contiguous content of the nested lists, like ListOffsetArray's content.
const std::string tostring_part() const
bool preserves_type(const Index64 &advanced) const override
Returns true if this slice would preserve an array's slice and therefore should pass on Content::para...
Represents a SliceArrayOf, SliceMissingOf, or SliceJaggedOf with missing values: None (no equivalent ...
Definition Slice.h:435
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
const IndexOf< T > index() const
Positions in the content or negative values representing None in the same sense as IndexedOptionArray...
SliceMissingOf(const IndexOf< T > &index, const Index8 &originalmask, const SliceItemPtr &content)
Creates a SliceMissingOf with a full set of parameters.
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
virtual bool referentially_equal(const SliceItemPtr &other) const override
Returns true if this slice item has all the same buffers and parameters as other; false otherwise.
const Index8 originalmask() const
The array of booleans from which the index was derived.
int64_t length() const
The length of the array: len(index).
const SliceItemPtr content() const
The non-None values of the array, much like an IndexedOptionArray's content.
const std::string tostring_part() const
bool preserves_type(const Index64 &advanced) const override
Returns true if this slice would preserve an array's slice and therefore should pass on Content::para...
Represents NumPy's newaxis marker (a.k.a. None), which prompts __getitem__ to insert a length-1 regul...
Definition Slice.h:197
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
SliceNewAxis()
Creates a SliceNewAxis.
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
virtual bool referentially_equal(const SliceItemPtr &other) const override
Returns true if this slice item has all the same buffers and parameters as other; false otherwise.
bool preserves_type(const Index64 &advanced) const override
Returns true if this slice would preserve an array's slice and therefore should pass on Content::para...
Represents a Python slice object (usual syntax: array[start:stop:step]).
Definition Slice.h:93
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
SliceRange(int64_t start, int64_t stop, int64_t step)
Creates a SliceRange from a full set of parameters.
int64_t stop() const
The exclusive stopping position.
int64_t start() const
The inclusive starting position.
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
bool hasstart() const
Returns true if start is not Slice::none; false otherwise.
bool hasstop() const
Returns true if stop is not Slice::none; false otherwise.
virtual bool referentially_equal(const SliceItemPtr &other) const override
Returns true if this slice item has all the same buffers and parameters as other; false otherwise.
int64_t step() const
The step size, which may be negative but must not be zero.
bool preserves_type(const Index64 &advanced) const override
Returns true if this slice would preserve an array's slice and therefore should pass on Content::para...
A sequence of SliceItem objects representing a tuple passed to Python's __getitem__.
Definition Slice.h:585
void append(const SliceFields &item)
Inserts a SliceFields in-place at the end of the items.
void append(const SliceRange &item)
Inserts a SliceRange in-place at the end of the items.
void append(const SliceItemPtr &item)
Inserts a SliceItem in-place at the end of the items.
void append(const SliceMissing64 &item)
Inserts a SliceMissing64 in-place at the end of the items.
const std::string tostring() const
Returns a string representation of this slice item (single-line custom format).
const Slice tail() const
Returns a Slice representing all but the first SliceItem.
Slice(const std::vector< SliceItemPtr > &items)
Creates an "unsealed" Slice, to which we can still add SliceItem objects (with append).
const Slice only_fields() const
Returns a Slice containing only SliceField and SliceFields.
bool referentially_equal(const Slice &other) const
Returns true if this slice has all the same buffers and parameters as other; false otherwise.
void append(const SliceField &item)
Inserts a SliceField in-place at the end of the items.
const SliceItemPtr head() const
Returns a pointer to the first SliceItem.
void append(const SliceAt &item)
Inserts a SliceAt in-place at the end of the items.
int64_t dimlength() const
The number of SliceAt, SliceRange, and SliceArrayOf objects in the items.
void append(const SliceEllipsis &item)
Inserts a SliceEllipsis in-place at the end of the items.
Slice(const std::vector< SliceItemPtr > &items, bool sealed)
Creates a Slice with a full set of parameters.
void append(const SliceNewAxis &item)
Inserts a SliceNewAxis in-place at the end of the items.
void append(const SliceArray64 &item)
Inserts a SliceArray64 in-place at the end of the items.
static int64_t none()
Represents a missing start, stop, or step in a SliceRange.
bool sealed() const
If true, the Slice is immutable and append will fail. Otherwise, the items may be appended to.
void append(const SliceJagged64 &item)
Inserts a SliceJagged64 in-place at the end of the items.
Slice()
Creates an empty Slice.
void become_sealed()
Seal this Slice so that it is no longer open to append.
const std::vector< SliceItemPtr > items() const
The SliceItem objects in this Slice.
bool isadvanced() const
Returns true if the Slice contains SliceArrayOf; false otherwise.
const Slice prepended(const SliceItemPtr &item) const
Returns a new Slice with item prepended.
int64_t length() const
The number of SliceItem objects in items.
const Slice not_fields() const
Returns a Slice without any SliceField or SliceFields.
#define LIBAWKWARD_EXPORT_SYMBOL
Definition common.h:45
Definition BitMaskedArray.h:15
std::shared_ptr< SliceItem > SliceItemPtr
Definition Slice.h:15