Awkward Array: include/awkward/Slice.h Source File

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
21 class LIBAWKWARD_EXPORT_SYMBOL SliceItem {
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
51 referentially_equal(const SliceItemPtr& other) const = 0;
52 };
53
58 class LIBAWKWARD_EXPORT_SYMBOL SliceAt: public SliceItem {
59 public:
63 SliceAt(int64_t at);
64
66 int64_t
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
93 class LIBAWKWARD_EXPORT_SYMBOL SliceRange: public SliceItem {
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
169 class LIBAWKWARD_EXPORT_SYMBOL SliceEllipsis: public SliceItem {
170 public:
172 SliceEllipsis();
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
197 class LIBAWKWARD_EXPORT_SYMBOL SliceNewAxis: public SliceItem {
198 public:
200 SliceNewAxis();
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
227 LIBAWKWARD_EXPORT_SYMBOL
228#endif
229 SliceArrayOf: public SliceItem {
230 public:
249 SliceArrayOf<T>(const IndexOf<T>& index,
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
302 tostring_part() const;
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
334 using SliceArray64 = SliceArrayOf<int64_t>;
335
340 class LIBAWKWARD_EXPORT_SYMBOL SliceField: public SliceItem {
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
384 class LIBAWKWARD_EXPORT_SYMBOL SliceFields: public SliceItem {
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
433 LIBAWKWARD_EXPORT_SYMBOL
434#endif
435 SliceMissingOf: public SliceItem {
436 public:
445 SliceMissingOf(const IndexOf<T>& index,
446 const Index8& originalmask,
447 const SliceItemPtr& content);
448
451 const IndexOf<T>
452 index() const;
453
456 const Index8
457 originalmask() const;
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
475 tostring_part() const;
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
501 using SliceMissing64 = SliceMissingOf<int64_t>;
502
510 template <typename T>
511 class
512#ifdef AWKWARD_SLICE_NO_EXTERN_TEMPLATE
513 LIBAWKWARD_EXPORT_SYMBOL
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
555 tostring_part() const;
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
579 using SliceJagged64 = SliceJaggedOf<int64_t>;
580
585 class LIBAWKWARD_EXPORT_SYMBOL Slice {
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
605 Slice();
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
648 prepended(const SliceItemPtr& item) const;
649
651 void
652 append(const SliceItemPtr& item);
653
655 void
656 append(const SliceAt& item);
657
659 void
660 append(const SliceRange& item);
661
663 void
664 append(const SliceEllipsis& item);
665
667 void
668 append(const SliceNewAxis& item);
669
672 void
673 append(const SliceArray64& item);
674
676 void
677 append(const SliceField& item);
678
680 void
681 append(const SliceFields& item);
682
685 void
686 append(const SliceMissing64& item);
687
690 void
691 append(const SliceJagged64& item);
692
694 void
695 become_sealed();
696
702 bool
703 isadvanced() const;
704
709 bool
710 referentially_equal(const Slice& other) const;
711
712 private:
714 std::vector<SliceItemPtr> items_;
716 bool sealed_;
717 };
718
719}
720
721#endif // AWKWARD_SLICE_H_
Index.h
awkward::IndexOf
A contiguous, one-dimensional array of integers used to represent data structures,...
Definition: Index.h:82
awkward::SliceArrayOf
Represents an array of integers in a slice (possibly converted from an array of booleans).
Definition: Slice.h:229
awkward::SliceArrayOf::tostring
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
awkward::SliceArrayOf::index
const IndexOf< T > index() const
A flattened version of the array used for slicing.
awkward::SliceArrayOf::length
const int64_t length() const
The length of the logical array: shape[0].
awkward::SliceArrayOf::frombool
bool frombool() const
If true, this integer array of positions was derived from a boolean array mask (from NumPy's nonzero ...
awkward::SliceArrayOf::strides
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.
awkward::SliceArrayOf::shallow_copy
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
awkward::SliceArrayOf::ndim
int64_t ndim() const
The number of dimensions: shape.size().
awkward::SliceArrayOf::referentially_equal
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.
awkward::SliceArrayOf::shape
const std::vector< int64_t > shape() const
Number of elements in each dimension, like NumPy's array shape.
awkward::SliceArrayOf::ravel
const IndexOf< T > ravel() const
Returns a one-dimensional contiguous version of the array, like NumPy's ravel.
awkward::SliceArrayOf::tostring_part
const std::string tostring_part() const
awkward::SliceArrayOf::preserves_type
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...
awkward::SliceAt
Represents an integer in a tuple of slices passed to __getitem__ in Python.
Definition: Slice.h:58
awkward::SliceAt::tostring
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
awkward::SliceAt::SliceAt
SliceAt(int64_t at)
Creates a SliceAt from a full set of parameters.
awkward::SliceAt::at
int64_t at() const
The integer that this slice item represents.
awkward::SliceAt::shallow_copy
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
awkward::SliceAt::referentially_equal
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.
awkward::SliceAt::preserves_type
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...
awkward::SliceEllipsis
Represents a Python Ellipsis object (usual syntax: array[...]).
Definition: Slice.h:169
awkward::SliceEllipsis::tostring
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
awkward::SliceEllipsis::SliceEllipsis
SliceEllipsis()
Creates a SliceEllipsis.
awkward::SliceEllipsis::shallow_copy
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
awkward::SliceEllipsis::referentially_equal
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.
awkward::SliceEllipsis::preserves_type
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...
awkward::SliceField
Represents a single string in a slice tuple, indicating that a RecordArray should be replaced by one ...
Definition: Slice.h:340
awkward::SliceField::tostring
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
awkward::SliceField::SliceField
SliceField(const std::string &key)
Creates a SliceField from a full set of parameters.
awkward::SliceField::shallow_copy
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
awkward::SliceField::key
const std::string key() const
The name of the field to select.
awkward::SliceField::referentially_equal
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.
awkward::SliceField::preserves_type
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...
awkward::SliceFields
Definition: Slice.h:384
awkward::SliceFields::tostring
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
awkward::SliceFields::SliceFields
SliceFields(const std::vector< std::string > &keys)
Creates a SliceFields from a full set of parameters.
awkward::SliceFields::keys
const std::vector< std::string > keys() const
The names of the fields to select.
awkward::SliceFields::shallow_copy
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
awkward::SliceFields::referentially_equal
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.
awkward::SliceFields::preserves_type
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...
awkward::SliceItem
Abstract class for slice items, which are elements of a tuple passed to an array's __getitem__ in Pyt...
Definition: Slice.h:21
awkward::SliceItem::shallow_copy
virtual const SliceItemPtr shallow_copy() const =0
Copies this node without copying any associated arrays.
awkward::SliceItem::tostring
virtual const std::string tostring() const =0
Returns a string representation of this slice item (single-line custom format).
awkward::SliceItem::referentially_equal
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.
awkward::SliceItem::preserves_type
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...
awkward::SliceItem::~SliceItem
virtual ~SliceItem()
Virtual destructor acts as a first non-inline virtual function that determines a specific translation...
awkward::SliceJaggedOf
Represents an array of nested lists, where the content may be SliceArrayOf, SliceMissingOf,...
Definition: Slice.h:515
awkward::SliceJaggedOf::tostring
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
awkward::SliceJaggedOf::SliceJaggedOf
SliceJaggedOf(const IndexOf< T > &offsets, const SliceItemPtr &content)
Creates a SliceJaggedOf with a full set of parameters.
awkward::SliceJaggedOf::shallow_copy
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
awkward::SliceJaggedOf::referentially_equal
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.
awkward::SliceJaggedOf::offsets
const IndexOf< T > offsets() const
Positions where one nested list stops and the next starts in the content in the same sense as ListOff...
awkward::SliceJaggedOf::length
int64_t length() const
The length of the array: len(offsets) - 1.
awkward::SliceJaggedOf::content
const SliceItemPtr content() const
The contiguous content of the nested lists, like ListOffsetArray's content.
awkward::SliceJaggedOf::tostring_part
const std::string tostring_part() const
awkward::SliceJaggedOf::preserves_type
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...
awkward::SliceMissingOf
Represents a SliceArrayOf, SliceMissingOf, or SliceJaggedOf with missing values: None (no equivalent ...
Definition: Slice.h:435
awkward::SliceMissingOf::tostring
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
awkward::SliceMissingOf::index
const IndexOf< T > index() const
Positions in the content or negative values representing None in the same sense as IndexedOptionArray...
awkward::SliceMissingOf::SliceMissingOf
SliceMissingOf(const IndexOf< T > &index, const Index8 &originalmask, const SliceItemPtr &content)
Creates a SliceMissingOf with a full set of parameters.
awkward::SliceMissingOf::shallow_copy
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
awkward::SliceMissingOf::referentially_equal
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.
awkward::SliceMissingOf::originalmask
const Index8 originalmask() const
The array of booleans from which the index was derived.
awkward::SliceMissingOf::length
int64_t length() const
The length of the array: len(index).
awkward::SliceMissingOf::content
const SliceItemPtr content() const
The non-None values of the array, much like an IndexedOptionArray's content.
awkward::SliceMissingOf::tostring_part
const std::string tostring_part() const
awkward::SliceMissingOf::preserves_type
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...
awkward::SliceNewAxis
Represents NumPy's newaxis marker (a.k.a. None), which prompts __getitem__ to insert a length-1 regul...
Definition: Slice.h:197
awkward::SliceNewAxis::tostring
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
awkward::SliceNewAxis::SliceNewAxis
SliceNewAxis()
Creates a SliceNewAxis.
awkward::SliceNewAxis::shallow_copy
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
awkward::SliceNewAxis::referentially_equal
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.
awkward::SliceNewAxis::preserves_type
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...
awkward::SliceRange
Represents a Python slice object (usual syntax: array[start:stop:step]).
Definition: Slice.h:93
awkward::SliceRange::tostring
const std::string tostring() const override
Returns a string representation of this slice item (single-line custom format).
awkward::SliceRange::SliceRange
SliceRange(int64_t start, int64_t stop, int64_t step)
Creates a SliceRange from a full set of parameters.
awkward::SliceRange::stop
int64_t stop() const
The exclusive stopping position.
awkward::SliceRange::start
int64_t start() const
The inclusive starting position.
awkward::SliceRange::shallow_copy
const SliceItemPtr shallow_copy() const override
Copies this node without copying any associated arrays.
awkward::SliceRange::hasstart
bool hasstart() const
Returns true if start is not Slice::none; false otherwise.
awkward::SliceRange::hasstop
bool hasstop() const
Returns true if stop is not Slice::none; false otherwise.
awkward::SliceRange::referentially_equal
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.
awkward::SliceRange::step
int64_t step() const
The step size, which may be negative but must not be zero.
awkward::SliceRange::preserves_type
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...
awkward::Slice
A sequence of SliceItem objects representing a tuple passed to Python's __getitem__.
Definition: Slice.h:585
awkward::Slice::append
void append(const SliceFields &item)
Inserts a SliceFields in-place at the end of the items.
awkward::Slice::append
void append(const SliceRange &item)
Inserts a SliceRange in-place at the end of the items.
awkward::Slice::append
void append(const SliceItemPtr &item)
Inserts a SliceItem in-place at the end of the items.
awkward::Slice::append
void append(const SliceMissing64 &item)
Inserts a SliceMissing64 in-place at the end of the items.
awkward::Slice::tostring
const std::string tostring() const
Returns a string representation of this slice item (single-line custom format).
awkward::Slice::tail
const Slice tail() const
Returns a Slice representing all but the first SliceItem.
awkward::Slice::Slice
Slice(const std::vector< SliceItemPtr > &items)
Creates an "unsealed" Slice, to which we can still add SliceItem objects (with append).
awkward::Slice::only_fields
const Slice only_fields() const
Returns a Slice containing only SliceField and SliceFields.
awkward::Slice::referentially_equal
bool referentially_equal(const Slice &other) const
Returns true if this slice has all the same buffers and parameters as other; false otherwise.
awkward::Slice::append
void append(const SliceField &item)
Inserts a SliceField in-place at the end of the items.
awkward::Slice::head
const SliceItemPtr head() const
Returns a pointer to the first SliceItem.
awkward::Slice::append
void append(const SliceAt &item)
Inserts a SliceAt in-place at the end of the items.
awkward::Slice::dimlength
int64_t dimlength() const
The number of SliceAt, SliceRange, and SliceArrayOf objects in the items.
awkward::Slice::append
void append(const SliceEllipsis &item)
Inserts a SliceEllipsis in-place at the end of the items.
awkward::Slice::Slice
Slice(const std::vector< SliceItemPtr > &items, bool sealed)
Creates a Slice with a full set of parameters.
awkward::Slice::append
void append(const SliceNewAxis &item)
Inserts a SliceNewAxis in-place at the end of the items.
awkward::Slice::append
void append(const SliceArray64 &item)
Inserts a SliceArray64 in-place at the end of the items.
awkward::Slice::none
static int64_t none()
Represents a missing start, stop, or step in a SliceRange.
awkward::Slice::sealed
bool sealed() const
If true, the Slice is immutable and append will fail. Otherwise, the items may be appended to.
awkward::Slice::append
void append(const SliceJagged64 &item)
Inserts a SliceJagged64 in-place at the end of the items.
awkward::Slice::Slice
Slice()
Creates an empty Slice.
awkward::Slice::become_sealed
void become_sealed()
Seal this Slice so that it is no longer open to append.
awkward::Slice::items
const std::vector< SliceItemPtr > items() const
The SliceItem objects in this Slice.
awkward::Slice::isadvanced
bool isadvanced() const
Returns true if the Slice contains SliceArrayOf; false otherwise.
awkward::Slice::prepended
const Slice prepended(const SliceItemPtr &item) const
Returns a new Slice with item prepended.
awkward::Slice::length
int64_t length() const
The number of SliceItem objects in items.
awkward::Slice::not_fields
const Slice not_fields() const
Returns a Slice without any SliceField or SliceFields.
common.h
LIBAWKWARD_EXPORT_SYMBOL
#define LIBAWKWARD_EXPORT_SYMBOL
Definition: common.h:45
awkward
Definition: BitMaskedArray.h:15
awkward::state::index
@ index
awkward::SliceItemPtr
std::shared_ptr< SliceItem > SliceItemPtr
Definition: Slice.h:15