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 
13 namespace 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
51  referentially_equal(const SliceItemPtr& other) const = 0;
52  };
53 
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 
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:
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 
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
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
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 
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
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 
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
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 
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 
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_
Represents a Python slice object (usual syntax: array[start:stop:step]).
Definition: Slice.h:93
#define LIBAWKWARD_EXPORT_SYMBOL
Definition: common.h:45
const std::string key(const RecordLookupPtr &recordlookup, int64_t fieldindex, int64_t numfields)
Returns the key associated with a field index, given a RecordLookup and a number of fields...
Definition: BitMaskedArray.h:15
A contiguous, one-dimensional array of integers used to represent data structures, rather than numerical data in the arrays themselves.
Definition: Index.h:16
const ContentPtr none
A constant value with type None.
Represents NumPy&#39;s newaxis marker (a.k.a. None), which prompts __getitem__ to insert a length-1 regul...
Definition: Slice.h:197
Definition: Slice.h:384
Represents a SliceArrayOf, SliceMissingOf, or SliceJaggedOf with missing values: None (no equivalent ...
Definition: Slice.h:431
Represents a single string in a slice tuple, indicating that a RecordArray should be replaced by one ...
Definition: Slice.h:340
const std::vector< std::string > keys(const RecordLookupPtr &recordlookup, int64_t numfields)
Returns a given RecordLookup as keys or generate anonymous ones form a number of fields.
Represents an array of integers in a slice (possibly converted from an array of booleans).
Definition: Slice.h:225
Represents an array of nested lists, where the content may be SliceArrayOf, SliceMissingOf, or SliceJaggedOf (no equivalent in NumPy).
Definition: Slice.h:511
A sequence of SliceItem objects representing a tuple passed to Python&#39;s __getitem__.
Definition: Slice.h:585
Represents a Python Ellipsis object (usual syntax: array[...]).
Definition: Slice.h:169
Abstract class for slice items, which are elements of a tuple passed to an array&#39;s __getitem__ in Pyt...
Definition: Slice.h:21
Represents an integer in a tuple of slices passed to __getitem__ in Python.
Definition: Slice.h:58
std::shared_ptr< SliceItem > SliceItemPtr
Definition: Slice.h:15