xref: /openbmc/bmcweb/features/redfish/include/utils/stl_utils.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
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>
6287ece64SGeorge Liu 
7287ece64SGeorge Liu namespace redfish
8287ece64SGeorge Liu {
9287ece64SGeorge Liu 
10287ece64SGeorge Liu namespace stl_utils
11287ece64SGeorge Liu {
12287ece64SGeorge Liu 
13287ece64SGeorge Liu template <typename ForwardIterator>
14287ece64SGeorge Liu ForwardIterator firstDuplicate(ForwardIterator first, ForwardIterator last)
15287ece64SGeorge Liu {
16287ece64SGeorge Liu     auto newLast = first;
17287ece64SGeorge Liu 
18287ece64SGeorge Liu     for (auto current = first; current != last; ++current)
19287ece64SGeorge Liu     {
20287ece64SGeorge Liu         if (std::find(first, newLast, *current) == newLast)
21287ece64SGeorge Liu         {
22287ece64SGeorge Liu             if (newLast != current)
23287ece64SGeorge Liu             {
24287ece64SGeorge Liu                 *newLast = *current;
25287ece64SGeorge Liu             }
26287ece64SGeorge Liu             ++newLast;
27287ece64SGeorge Liu         }
28287ece64SGeorge Liu     }
29287ece64SGeorge Liu 
30287ece64SGeorge Liu     return newLast;
31287ece64SGeorge Liu }
32287ece64SGeorge Liu 
33287ece64SGeorge Liu template <typename T>
34287ece64SGeorge Liu void removeDuplicate(T& t)
35287ece64SGeorge Liu {
36287ece64SGeorge Liu     t.erase(firstDuplicate(t.begin(), t.end()), t.end());
37287ece64SGeorge Liu }
38287ece64SGeorge Liu 
39287ece64SGeorge Liu } // namespace stl_utils
40287ece64SGeorge Liu } // namespace redfish
41