xref: /openbmc/bmcweb/redfish-core/include/utils/stl_utils.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #pragma once
4 
5 #include <algorithm>
6 
7 namespace redfish
8 {
9 
10 namespace stl_utils
11 {
12 
13 template <typename ForwardIterator>
firstDuplicate(ForwardIterator first,ForwardIterator last)14 ForwardIterator firstDuplicate(ForwardIterator first, ForwardIterator last)
15 {
16     auto newLast = first;
17 
18     for (auto current = first; current != last; ++current)
19     {
20         if (std::find(first, newLast, *current) == newLast)
21         {
22             if (newLast != current)
23             {
24                 *newLast = *current;
25             }
26             ++newLast;
27         }
28     }
29 
30     return newLast;
31 }
32 
33 template <typename T>
removeDuplicate(T & t)34 void removeDuplicate(T& t)
35 {
36     t.erase(firstDuplicate(t.begin(), t.end()), t.end());
37 }
38 
39 } // namespace stl_utils
40 } // namespace redfish
41