xref: /aosp_15_r20/external/pigweed/pw_containers/examples/intrusive_map.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_containers/intrusive_map.h"
16 
17 #include "pw_unit_test/framework.h"
18 
19 namespace examples {
20 
21 // DOCSTAG: [pw_containers-intrusive_map]
22 
23 struct Book : public pw::IntrusiveMap<uint32_t, Book>::Pair {
24  private:
25   using Pair = pw::IntrusiveMap<uint32_t, Book>::Pair;
26 
27  public:
Bookexamples::Book28   Book(const char* name, uint32_t oclc) : Pair(oclc), name_(name) {}
nameexamples::Book29   const char* name() const { return name_; }
30 
31  private:
32   const char* name_;
33 };
34 
35 std::array<Book, 8> books = {{
36     {"A Tale of Two Cities", 20848014u},
37     {"The Little Prince", 182537909u},
38     {"The Alchemist", 26857452u},
39     {"Harry Potter and the Philosopher's Stone", 44795766u},
40     {"And Then There Were None", 47032439u},
41     {"Dream of the Red Chamber", 20692970u},
42     {"The Hobbit", 1827184u},
43     {"Alice's Adventures in Wonderland", 5635965u},
44 }};
45 
46 pw::IntrusiveMap<uint32_t, Book> library(books.begin(), books.end());
47 
VisitLibrary(pw::IntrusiveMap<uint32_t,Book> & book_bag)48 void VisitLibrary(pw::IntrusiveMap<uint32_t, Book>& book_bag) {
49   // Return any books we previously checked out.
50   library.merge(book_bag);
51 
52   // Pick out some new books to read to the kids, but only if they're available.
53   std::array<uint32_t, 3> oclcs = {
54       1827184u,   // The Hobbit
55       11914189u,  // Curious George
56       44795766u,  // Harry Potter
57   };
58   for (uint32_t oclc : oclcs) {
59     auto iter = library.find(oclc);
60     if (iter != library.end()) {
61       Book& book = *iter;
62       library.erase(iter);
63       book_bag.insert(book);
64     }
65   }
66 }
67 
68 // DOCSTAG: [pw_containers-intrusive_map]
69 
70 }  // namespace examples
71 
72 namespace {
73 
TEST(IntrusiveMapExampleTest,VisitLibrary)74 TEST(IntrusiveMapExampleTest, VisitLibrary) {
75   examples::Book book = {"One Hundred Years of Solitude", 17522865u};
76   pw::IntrusiveMap<uint32_t, examples::Book> book_bag;
77   book_bag.insert(book);
78 
79   examples::VisitLibrary(book_bag);
80   auto iter = book_bag.begin();
81   EXPECT_STREQ((iter++)->name(), "The Hobbit");
82   EXPECT_STREQ((iter++)->name(), "Harry Potter and the Philosopher's Stone");
83   EXPECT_EQ(iter, book_bag.end());
84 
85   // Remove books before items go out scope.
86   book_bag.clear();
87   examples::library.clear();
88 }
89 
90 }  // namespace
91