P4C
The P4 Compiler
Loading...
Searching...
No Matches
assoc Namespace Reference

Namespaces

namespace  detail
 Implementation details of for the namespace assoc.
 

Classes

class  hash_map
 
class  hash_set
 
class  map
 
class  set
 

Typedefs

template<typename Key , typename T , typename Compare = std::less<Key>, typename Allocator = std::allocator<std::pair<const Key, T>>>
using iterable_map = map<Key, T, Compare, Allocator, Iterable::Yes>
 
template<typename Key , typename Compare = std::less<Key>, typename Allocator = std::allocator<Key>>
using iterable_set = set<Key, Compare, Allocator, Iterable::Yes>
 
template<typename Key , typename T , typename Compare = std::less<Key>, typename Allocator = std::allocator<std::pair<const Key, T>>>
using non_iterable_map = map<Key, T, Compare, Allocator, Iterable::No>
 
template<typename Key , typename Compare = std::less<Key>, typename Allocator = std::allocator<Key>>
using non_iterable_set = set<Key, Compare, Allocator, Iterable::No>
 
template<typename Key , typename T , typename Compare = std::less<Key>, typename Allocator = std::allocator<std::pair<const Key, T>>>
using ordered_map = ::P4::ordered_map<Key, T, Compare, Allocator>
 
template<typename Key , typename Compare = std::less<Key>, typename Allocator = std::allocator<Key>>
using ordered_set = ::P4::ordered_set<Key, Compare, Allocator>
 

Enumerations

enum class  Iterable { Auto , No , Yes }
 

Functions

template<class K , class T , class V , class Comp , class Alloc , Iterable It>
get (const map< K, V, Comp, Alloc, It > &m, T key, V def=V())
 
template<class K , class T , class V , class Comp , class Alloc , Iterable It>
get (const map< K, V, Comp, Alloc, It > *m, T key, V def=V())
 
template<class K , class T , class V , class Comp , class Alloc , Iterable It>
const V * getref (const map< K, V, Comp, Alloc, It > &m, T key)
 
template<class K , class T , class V , class Comp , class Alloc , Iterable It>
const V * getref (const map< K, V, Comp, Alloc, It > *m, T key)
 
template<class K , class T , class V , class Comp , class Alloc , Iterable It>
V * getref (map< K, V, Comp, Alloc, It > &m, T key)
 
template<class K , class T , class V , class Comp , class Alloc , Iterable It>
V * getref (map< K, V, Comp, Alloc, It > *m, T key)
 
template<typename T >
std::ostream & operator<< (std::ostream &out, const assoc::hash_set< T > &set)
 
template<typename T >
std::ostream & operator<< (std::ostream &out, const assoc::set< T > &set)
 

Detailed Description

Copyright (C) 2024 Intel Corporation

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

SPDX-License-Identifier: Apache-2.0 The assoc namespace contains wrappers over various associative containers that can be used more safely in the compiler. In particular, it tries to keep track of possible sources of nondeterminism – cases when repeated compilation of the same P4 code produces different results. If using the containers from assoc namespace, the code does not compile if it iterates over sorted containers that use pointers as keys or if it iterates over unordered containers. The reason is that such iteration often introduces dependency on particular ordering of pointers that can differ between runs of the compiler.

The downside of this approach is that it is an approximation – there can be cases when this is perfectly correct and does not introduce nondeterminism: e.g. if the code looks for if some value in a map matches some predicate (but does not return the actual value).

In general, the iteration (using container.begin()) is forbidden for:

  • all unsorted (hash-based) containers;
  • containers that have pointers or tuples/pairs containing pointers as keys, if the containers do not use explicit non-standard comparator.

This behaviour can be overridden by:

  • using .unstable_iterable() on the container at the place where it is actually safe to iterate as the result does not really depend on the iteration order.
  • using .unstable_begin() (together with .end()) in similar cases e.g. in standard algorithms such as std::any_of/stdall_of/stdnone_of.
  • declaring the data structure as assoc::iterable_* (e.g. assoc::iterable_map) to allow the iteration always (not recommended).

Enumeration Type Documentation

◆ Iterable

enum class assoc::Iterable
strong

Used to specify if the associative container should be iterable

  • Yes: always iterable
  • No: always not iterable
  • Auto: iterable for sorted containers over keys that are not pointers or tuples/pairs containing pointers.