P4C
The P4 Compiler
Loading...
Searching...
No Matches
P4::ClosedRange< Unit, Order > Struct Template Reference

#include <bitrange.h>

Public Types

using FromTo = BitRange::FromTo
 
using MinToMax = BitRange::MinToMax
 
using StartLen = BitRange::StartLen
 
using ZeroToMax = BitRange::ZeroToMax
 

Public Member Functions

 ClosedRange (const HalfOpenRange< Unit, Order > &r)
 
 ClosedRange (FromTo &&fromTo)
 
constexpr ClosedRange (int lo, int hi)
 
 ClosedRange (MinToMax &&)
 
 ClosedRange (StartLen &&startLen)
 
 ClosedRange (std::pair< int, int > range)
 
 ClosedRange (ZeroToMax &&)
 
bool contains (ClosedRange other) const
 
bool contains (int index) const
 
cstring formatAsSlice (int spaceSize) const
 
int hiByte () const
 
HalfOpenRange< Unit, Order > intersectWith (ClosedRange a) const
 
HalfOpenRange< Unit, Order > intersectWith (int l, int h) const
 
bool isHiAligned () const
 
bool isLoAligned () const
 
int loByte () const
 
int nextByte () const
 
bool operator!= (ClosedRange other) const
 
HalfOpenRange< Unit, Order > operator& (ClosedRange a) const
 
HalfOpenRange< Unit, Order > operator&= (ClosedRange a)
 
bool operator< (const ClosedRange &other) const
 
bool operator== (ClosedRange other) const
 
ClosedRange operator| (ClosedRange a) const
 
ClosedRange operator|= (ClosedRange a)
 
bool overlaps (ClosedRange a) const
 
bool overlaps (int l, int h) const
 
ClosedRange< RangeUnit::Bit, Order > resizedToBits (int size) const
 
ClosedRange resizedToBytes (int size) const
 
ClosedRange< RangeUnit::Bit, Order > shiftedByBits (int offset) const
 
ClosedRange shiftedByBytes (int offset) const
 
ssize_t size () const
 
void toJSON (JSONGenerator &json) const
 JSON serialization/deserialization.
 
template<Endian DestOrder>
ClosedRange< Unit, DestOrder > toOrder (int spaceSize) const
 
template<RangeUnit DestUnit>
ClosedRange< DestUnit, Order > toUnit () const
 
ClosedRange unionWith (ClosedRange a) const
 
ClosedRange unionWith (int l, int h) const
 

Static Public Member Functions

static ClosedRange fromJSON (JSONLoader &json)
 

Public Attributes

int hi
 
int lo
 

Static Public Attributes

static constexpr Endian order = Order
 
static constexpr RangeUnit unit = Unit
 

Friends

size_t hash_value (const ClosedRange &r)
 

Detailed Description

template<RangeUnit Unit, Endian Order>
struct P4::ClosedRange< Unit, Order >

A closed range of bits or bytes - [lo, hi] specified in terms of a specific endian order. Closed ranges include both lo and hi, so ClosedRange(3, 5) contains 3, 4, and 5.

Use a closed range when you want to forbid the possibility of an empty range. Using a closed range may also make certain algorithms easier to express.

XXX: ClosedRange's interface is very similar to HalfOpenRange, but they have almost totally different implementations, and the limitations of C++ templates make sharing the things they do have in common challenging. Most of the benefit would have come from sharing documentation; as a compromise, most of the methods in ClosedRange just reference HalfOpenRange's documentation.

XXX: Currently, for backwards compatibility, it's possible to construct ranges where lo is greater than or equal to hi. We should enforce that ranges are consistent; we'll add the necessary checks after the existing code has been audited.

XXX: We should also add checks to avoid integer overflow.

Member Function Documentation

◆ contains() [1/2]

template<RangeUnit Unit, Endian Order>
bool P4::ClosedRange< Unit, Order >::contains ( ClosedRange< Unit, Order > other) const
inline
See also
HalfOpenRange::contains().

◆ contains() [2/2]

template<RangeUnit Unit, Endian Order>
bool P4::ClosedRange< Unit, Order >::contains ( int index) const
inline
See also
HalfOpenRange::contains().

◆ formatAsSlice()

template<RangeUnit Unit, Endian Order>
cstring P4::ClosedRange< Unit, Order >::formatAsSlice ( int spaceSize) const
inline

Formats this range as P4 syntax by converting to a little-endian range of bits, and formatting the result as "[hi:lo]".

◆ hiByte()

template<RangeUnit Unit, Endian Order>
int P4::ClosedRange< Unit, Order >::hiByte ( ) const
inline
See also
HalfOpenRange::hiByte().

◆ intersectWith()

template<RangeUnit Unit, Endian Order>
HalfOpenRange< Unit, Order > P4::ClosedRange< Unit, Order >::intersectWith ( ClosedRange< Unit, Order > a) const
inline
Returns
a range which contains all the bits which are included in both this range and the provided range, or an empty range if there are no bits in common. Because the resulting range may be empty, this method returns a HalfOpenRange.

◆ isHiAligned()

template<RangeUnit Unit, Endian Order>
bool P4::ClosedRange< Unit, Order >::isHiAligned ( ) const
inline
See also
HalfOpenRange::isHiAligned().

◆ isLoAligned()

template<RangeUnit Unit, Endian Order>
bool P4::ClosedRange< Unit, Order >::isLoAligned ( ) const
inline
See also
HalfOpenRange::isLoAligned().

◆ loByte()

template<RangeUnit Unit, Endian Order>
int P4::ClosedRange< Unit, Order >::loByte ( ) const
inline
See also
HalfOpenRange::loByte().

◆ nextByte()

template<RangeUnit Unit, Endian Order>
int P4::ClosedRange< Unit, Order >::nextByte ( ) const
inline
See also
HalfOpenRange::nextByte().

◆ operator<()

template<RangeUnit Unit, Endian Order>
bool P4::ClosedRange< Unit, Order >::operator< ( const ClosedRange< Unit, Order > & other) const
inline
See also
HalfOpenRange::operator<.

◆ overlaps()

template<RangeUnit Unit, Endian Order>
bool P4::ClosedRange< Unit, Order >::overlaps ( ClosedRange< Unit, Order > a) const
inline
See also
HalfOpenRange::overlaps().

◆ resizedToBits()

template<RangeUnit Unit, Endian Order>
ClosedRange< RangeUnit::Bit, Order > P4::ClosedRange< Unit, Order >::resizedToBits ( int size) const
inline
See also
HalfOpenRange::resizedToBits().

◆ resizedToBytes()

template<RangeUnit Unit, Endian Order>
ClosedRange P4::ClosedRange< Unit, Order >::resizedToBytes ( int size) const
inline
See also
HalfOpenRange::resizedToBytes().

◆ shiftedByBits()

template<RangeUnit Unit, Endian Order>
ClosedRange< RangeUnit::Bit, Order > P4::ClosedRange< Unit, Order >::shiftedByBits ( int offset) const
inline
See also
HalfOpenRange::shiftedByBits().

◆ shiftedByBytes()

template<RangeUnit Unit, Endian Order>
ClosedRange P4::ClosedRange< Unit, Order >::shiftedByBytes ( int offset) const
inline
See also
HalfOpenRange::shiftedByBytes().

◆ size()

template<RangeUnit Unit, Endian Order>
ssize_t P4::ClosedRange< Unit, Order >::size ( ) const
inline
See also
HalfOpenRange::size().

◆ toOrder()

template<RangeUnit Unit, Endian Order>
template<Endian DestOrder>
ClosedRange< Unit, DestOrder > P4::ClosedRange< Unit, Order >::toOrder ( int spaceSize) const
inline
See also
HalfOpenRange::toOrder().

◆ toUnit()

template<RangeUnit Unit, Endian Order>
template<RangeUnit DestUnit>
ClosedRange< DestUnit, Order > P4::ClosedRange< Unit, Order >::toUnit ( ) const
inline
See also
HalfOpenRange::toUnit().

◆ unionWith()

template<RangeUnit Unit, Endian Order>
ClosedRange P4::ClosedRange< Unit, Order >::unionWith ( ClosedRange< Unit, Order > a) const
inline
See also
HalfOpenRange::unionWith().

Member Data Documentation

◆ hi

template<RangeUnit Unit, Endian Order>
int P4::ClosedRange< Unit, Order >::hi

The highest numbered index in the range. For Endian::Network, this is the least significant bit or byte; for Endian::Little, it's the most significant. Because this is a closed range, the range element this index identifies is included in the range.

◆ lo

template<RangeUnit Unit, Endian Order>
int P4::ClosedRange< Unit, Order >::lo

The lowest numbered index in the range. For Endian::Network, this is the most significant bit or byte; for Endian::Little, it's the least significant.