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