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 // DOCSTAG: [pw_allocator-examples-pmr]
16*61c4878aSAndroid Build Coastguard Worker #include <map>
17*61c4878aSAndroid Build Coastguard Worker #include <string_view>
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard Worker #include "pw_allocator/allocator.h"
20*61c4878aSAndroid Build Coastguard Worker #include "pw_allocator/pmr_allocator.h"
21*61c4878aSAndroid Build Coastguard Worker
22*61c4878aSAndroid Build Coastguard Worker namespace examples {
23*61c4878aSAndroid Build Coastguard Worker
24*61c4878aSAndroid Build Coastguard Worker class LibraryIndex {
25*61c4878aSAndroid Build Coastguard Worker public:
26*61c4878aSAndroid Build Coastguard Worker using StringType = ::std::pmr::string;
27*61c4878aSAndroid Build Coastguard Worker using MapType = ::std::pmr::multimap<StringType, StringType>;
28*61c4878aSAndroid Build Coastguard Worker using Iterator = typename MapType::iterator;
29*61c4878aSAndroid Build Coastguard Worker
LibraryIndex(pw::allocator::Allocator & allocator)30*61c4878aSAndroid Build Coastguard Worker LibraryIndex(pw::allocator::Allocator& allocator)
31*61c4878aSAndroid Build Coastguard Worker : allocator_(allocator), by_author_(allocator_) {}
32*61c4878aSAndroid Build Coastguard Worker
AddBook(std::string_view title,std::string_view author)33*61c4878aSAndroid Build Coastguard Worker void AddBook(std::string_view title, std::string_view author) {
34*61c4878aSAndroid Build Coastguard Worker by_author_.emplace(author, title);
35*61c4878aSAndroid Build Coastguard Worker }
36*61c4878aSAndroid Build Coastguard Worker
FindByAuthor(std::string_view author)37*61c4878aSAndroid Build Coastguard Worker std::pair<Iterator, Iterator> FindByAuthor(std::string_view author) {
38*61c4878aSAndroid Build Coastguard Worker return by_author_.equal_range(StringType(author, allocator_));
39*61c4878aSAndroid Build Coastguard Worker }
40*61c4878aSAndroid Build Coastguard Worker
41*61c4878aSAndroid Build Coastguard Worker private:
42*61c4878aSAndroid Build Coastguard Worker pw::allocator::PmrAllocator allocator_;
43*61c4878aSAndroid Build Coastguard Worker MapType by_author_;
44*61c4878aSAndroid Build Coastguard Worker };
45*61c4878aSAndroid Build Coastguard Worker
46*61c4878aSAndroid Build Coastguard Worker } // namespace examples
47*61c4878aSAndroid Build Coastguard Worker // DOCSTAG: [pw_allocator-examples-pmr]
48*61c4878aSAndroid Build Coastguard Worker
49*61c4878aSAndroid Build Coastguard Worker // Unit tests.
50*61c4878aSAndroid Build Coastguard Worker #include "pw_allocator/testing.h"
51*61c4878aSAndroid Build Coastguard Worker #include "pw_unit_test/framework.h"
52*61c4878aSAndroid Build Coastguard Worker
53*61c4878aSAndroid Build Coastguard Worker namespace {
54*61c4878aSAndroid Build Coastguard Worker
55*61c4878aSAndroid Build Coastguard Worker using AllocatorForTest = ::pw::allocator::test::AllocatorForTest<4096>;
56*61c4878aSAndroid Build Coastguard Worker
TEST(PmrExample,FindBookByAuthor)57*61c4878aSAndroid Build Coastguard Worker TEST(PmrExample, FindBookByAuthor) {
58*61c4878aSAndroid Build Coastguard Worker AllocatorForTest allocator;
59*61c4878aSAndroid Build Coastguard Worker examples::LibraryIndex books(allocator);
60*61c4878aSAndroid Build Coastguard Worker
61*61c4878aSAndroid Build Coastguard Worker // Books which sold over 60M copies.
62*61c4878aSAndroid Build Coastguard Worker // inclusive-language: disable
63*61c4878aSAndroid Build Coastguard Worker books.AddBook("A Tale of Two Cities", "Charles Dickens");
64*61c4878aSAndroid Build Coastguard Worker books.AddBook("Le Petit Prince", "Antoine de Saint-Exupery");
65*61c4878aSAndroid Build Coastguard Worker books.AddBook("O Alquimista", "Paulo Coelho");
66*61c4878aSAndroid Build Coastguard Worker books.AddBook("Harry Potter and the Philosopher's Stone", "J. K. Rowling");
67*61c4878aSAndroid Build Coastguard Worker books.AddBook("And Then There Were None", "Agatha Christie");
68*61c4878aSAndroid Build Coastguard Worker books.AddBook("Dream of the Red Chamber", "Cao Xueqin");
69*61c4878aSAndroid Build Coastguard Worker books.AddBook("The Hobbit", "J. R. R. Tolkien");
70*61c4878aSAndroid Build Coastguard Worker books.AddBook("She: A History of Adventure", "H. Rider Haggard");
71*61c4878aSAndroid Build Coastguard Worker books.AddBook("The Da Vinci Code", "Dan Brown");
72*61c4878aSAndroid Build Coastguard Worker books.AddBook("Harry Potter and the Chamber of Secrets", "J. K. Rowling");
73*61c4878aSAndroid Build Coastguard Worker books.AddBook("Harry Potter and the Prisoner of Azkaban", "J. K. Rowling");
74*61c4878aSAndroid Build Coastguard Worker books.AddBook("Harry Potter and the Goblet of Fire", "J. K. Rowling");
75*61c4878aSAndroid Build Coastguard Worker books.AddBook("Harry Potter and the Order of the Phoenix", "J. K. Rowling");
76*61c4878aSAndroid Build Coastguard Worker books.AddBook("Harry Potter and the Half-Blood Prince", "J. K. Rowling");
77*61c4878aSAndroid Build Coastguard Worker books.AddBook("Harry Potter and the Deathly Hallows", "J. K. Rowling");
78*61c4878aSAndroid Build Coastguard Worker books.AddBook("The Catcher in the Rye", "J. D. Salinger");
79*61c4878aSAndroid Build Coastguard Worker // inclusive-language: enable
80*61c4878aSAndroid Build Coastguard Worker
81*61c4878aSAndroid Build Coastguard Worker examples::LibraryIndex::Iterator begin, end;
82*61c4878aSAndroid Build Coastguard Worker std::tie(begin, end) = books.FindByAuthor("J. K. Rowling");
83*61c4878aSAndroid Build Coastguard Worker EXPECT_EQ(std::distance(begin, end), 7);
84*61c4878aSAndroid Build Coastguard Worker }
85*61c4878aSAndroid Build Coastguard Worker
86*61c4878aSAndroid Build Coastguard Worker } // namespace
87