|
19 | 19 | #include <optional>
|
20 | 20 | #include <queue>
|
21 | 21 | #include <string>
|
| 22 | +#include <tuple> |
22 | 23 | #include <utility>
|
23 | 24 | #include <vector>
|
24 | 25 |
|
|
30 | 31 | #include "absl/status/status.h"
|
31 | 32 | #include "absl/status/statusor.h"
|
32 | 33 | #include "absl/strings/str_cat.h"
|
| 34 | +#include "absl/types/optional.h" |
33 | 35 | #include "absl/types/span.h"
|
34 | 36 | #include "boost/graph/adjacency_list.hpp"
|
35 | 37 | #include "google/protobuf/repeated_ptr_field.h"
|
36 | 38 | #include "gutil/collections.h"
|
37 | 39 | #include "gutil/status.h"
|
38 | 40 | #include "p4/config/v1/p4info.pb.h"
|
39 | 41 | #include "p4/v1/p4runtime.pb.h"
|
| 42 | +#include "p4_pdpi/entity_keys.h" |
40 | 43 | #include "p4_pdpi/ir.pb.h"
|
41 | 44 | #include "p4_pdpi/names.h"
|
42 | 45 | #include "p4_pdpi/references.h"
|
@@ -376,6 +379,44 @@ absl::Status StableSortEntities(const IrP4Info& info,
|
376 | 379 | return absl::OkStatus();
|
377 | 380 | }
|
378 | 381 |
|
| 382 | +absl::Status SortEntities(const IrP4Info& info, |
| 383 | + std::vector<p4::v1::Entity>& entities) { |
| 384 | + absl::c_sort(entities, [&](const p4::v1::Entity& a, const p4::v1::Entity& b) { |
| 385 | + return a.DebugString() < b.DebugString(); |
| 386 | + }); |
| 387 | + |
| 388 | + struct EntityTuple { |
| 389 | + p4::v1::Entity entity; |
| 390 | + EntityKey key; |
| 391 | + int dependency_rank; |
| 392 | + }; |
| 393 | + |
| 394 | + std::vector<EntityTuple> augmented_entities; |
| 395 | + for (p4::v1::Entity& entity : entities) { |
| 396 | + ASSIGN_OR_RETURN(EntityKey key, EntityKey::MakeEntityKey(entity)); |
| 397 | + ASSIGN_OR_RETURN(std::string table_name, EntityToTableName(info, entity)); |
| 398 | + ASSIGN_OR_RETURN( |
| 399 | + int dependency_rank, |
| 400 | + gutil::FindOrStatus(info.dependency_rank_by_table_name(), table_name)); |
| 401 | + |
| 402 | + augmented_entities.push_back({std::move(entity), key, dependency_rank}); |
| 403 | + } |
| 404 | + absl::c_stable_sort(augmented_entities, |
| 405 | + [&](const EntityTuple& a, const EntityTuple& b) { |
| 406 | + return a.key < b.key; |
| 407 | + }); |
| 408 | + |
| 409 | + absl::c_stable_sort(augmented_entities, |
| 410 | + [&](const EntityTuple& a, const EntityTuple& b) { |
| 411 | + return a.dependency_rank > b.dependency_rank; |
| 412 | + }); |
| 413 | + |
| 414 | + for (int i = 0; i < entities.size(); ++i) { |
| 415 | + entities[i] = std::move(augmented_entities[i].entity); |
| 416 | + } |
| 417 | + return absl::OkStatus(); |
| 418 | +} |
| 419 | + |
379 | 420 | absl::Status StableSortUpdates(
|
380 | 421 | const IrP4Info& info,
|
381 | 422 | google::protobuf::RepeatedPtrField<p4::v1::Update>& updates,
|
|
0 commit comments