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