xref: /openbmc/bmcweb/features/redfish/include/utils/stl_utils.hpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
1*40e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
2*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3287ece64SGeorge Liu #pragma once
4287ece64SGeorge Liu 
5287ece64SGeorge Liu #include <algorithm>
69ea15c35SEd Tanous #include <vector>
7287ece64SGeorge Liu 
8287ece64SGeorge Liu namespace redfish
9287ece64SGeorge Liu {
10287ece64SGeorge Liu 
11287ece64SGeorge Liu namespace stl_utils
12287ece64SGeorge Liu {
13287ece64SGeorge Liu 
14287ece64SGeorge Liu template <typename ForwardIterator>
15287ece64SGeorge Liu ForwardIterator firstDuplicate(ForwardIterator first, ForwardIterator last)
16287ece64SGeorge Liu {
17287ece64SGeorge Liu     auto newLast = first;
18287ece64SGeorge Liu 
19287ece64SGeorge Liu     for (auto current = first; current != last; ++current)
20287ece64SGeorge Liu     {
21287ece64SGeorge Liu         if (std::find(first, newLast, *current) == newLast)
22287ece64SGeorge Liu         {
23287ece64SGeorge Liu             if (newLast != current)
24287ece64SGeorge Liu             {
25287ece64SGeorge Liu                 *newLast = *current;
26287ece64SGeorge Liu             }
27287ece64SGeorge Liu             ++newLast;
28287ece64SGeorge Liu         }
29287ece64SGeorge Liu     }
30287ece64SGeorge Liu 
31287ece64SGeorge Liu     return newLast;
32287ece64SGeorge Liu }
33287ece64SGeorge Liu 
34287ece64SGeorge Liu template <typename T>
35287ece64SGeorge Liu void removeDuplicate(T& t)
36287ece64SGeorge Liu {
37287ece64SGeorge Liu     t.erase(firstDuplicate(t.begin(), t.end()), t.end());
38287ece64SGeorge Liu }
39287ece64SGeorge Liu 
40287ece64SGeorge Liu } // namespace stl_utils
41287ece64SGeorge Liu } // namespace redfish
42