Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:51:49

0001 #ifndef VECTOR_UTILS_H
0002 #define VECTOR_UTILS_H
0003 
0004 /**
0005  * @file VectorUtils.h
0006  * @author Bryan BERTHOU (SPhN / CEA Saclay)
0007  * @date 30 June 2015
0008  * @version 1.0
0009  */
0010 
0011 #include <algorithm>
0012 #include <vector>
0013 
0014 #include "../beans/List.h"
0015 
0016 namespace PARTONS {
0017 
0018 /**
0019  * @class VectorUtils
0020  */
0021 //TODO tester les fonctions de la classe. Je ne sais pas si elles sont correctes.
0022 class VectorUtils {
0023 public:
0024     //TODO attention ici on modifie les vecteurs car on les tri. Faut-il copier les vector avant de les trier ?
0025     // http://www.geeksforgeeks.org/find-union-and-intersection-of-two-unsorted-arrays/
0026     template<typename T>
0027     static std::vector<T> intersection(std::vector<T> & lhs,
0028             std::vector<T> & rhs) {
0029         std::vector<T> result;
0030 
0031         std::sort(lhs.begin(), lhs.end());
0032         std::sort(rhs.begin(), rhs.end());
0033 
0034         std::set_intersection(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(),
0035                 std::back_inserter(result));
0036 
0037         return result;
0038     }
0039 
0040     template<typename T>
0041     static List<T> intersection(const List<T> & lhs, const List<T> & rhs) {
0042         std::vector<T> result;
0043 
0044         std::vector<T> lhsData = lhs.getData();
0045         std::vector<T> rhsData = rhs.getData();
0046 
0047         std::sort(lhsData.begin(), lhsData.end());
0048         std::sort(rhsData.begin(), rhsData.end());
0049 
0050         std::set_intersection(lhsData.begin(), lhsData.end(), rhsData.begin(),
0051                 rhsData.end(), std::back_inserter(result));
0052 
0053         return List<T>(result);
0054     }
0055 };
0056 
0057 } /* namespace PARTONS */
0058 
0059 #endif /* VECTOR_UTILS_H */