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