Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #ifndef LIST_H
0002 #define LIST_H
0003 
0004 /**
0005  * @file List.h
0006  * @author: Bryan BERTHOU (SPhN / CEA Saclay)
0007  * @date November 26, 2015
0008  * @version 1.0
0009  */
0010 
0011 #include <ElementaryUtils/logger/CustomException.h>
0012 #include <ElementaryUtils/string_utils/Formatter.h>
0013 #include <ElementaryUtils/string_utils/StringUtils.h>
0014 #include <ElementaryUtils/thread/Packet.h>
0015 #include <stddef.h>
0016 #include <algorithm>    // std::sort
0017 #include <string>
0018 #include <vector>
0019 
0020 #include "../BaseObject.h"
0021 
0022 namespace PARTONS {
0023 
0024 /**
0025  * @class List
0026  *
0027  * @brief STL-like container to store basic PARTONS objects.
0028  *
0029  * This class is a STL-like container created in order to store PARTONS objects that derivative from BaseObject class.
0030  * More precisely, it is a wrap of std::vector that provides additional functionalities, like sorting and serialization, all based on functions predefined in BaseObject class.
0031  * This example illustrates how to use this container:
0032  \code{.cpp}
0033  //define two lists of GPDResult objects
0034  List<GPDResult> list1;
0035  List<GPDResult> list2;
0036 
0037  //fill these lists with few objects
0038 
0039  //retrieve GPD service
0040  GPDService* pGPDService =
0041  Partons::getInstance()->getServiceObjectRegistry()->getGPDService();
0042 
0043  //load GPD module with the BaseModuleFactory
0044  GPDModule* pGPDModel =
0045  Partons::getInstance()->getModuleObjectFactory()->newGPDModule(
0046  MMS13Model::classId);
0047 
0048  //define list of GPD types to be computed
0049  List<GPDType> gpdTypeList;
0050  gpdTypeList.add(GPDType::H);
0051 
0052  //get four examples
0053  for (size_t i = 0; i < 4; i++) {
0054 
0055  //evaluate
0056  GPDResult gpdResult = pGPDService->computeGPDModel(
0057  GPDKinematic(-0.1 + 0.05 * i, 0.05, 0., 2., 2.), pGPDModel,
0058  gpdTypeList);
0059 
0060  //fill lists
0061  list1.add(gpdResult);
0062  list2.add(gpdResult);
0063  }
0064 
0065  \endcode
0066  */
0067 template<class T> class List: public BaseObject {
0068 public:
0069 
0070     /**
0071      * Constructor.
0072      */
0073     List() :
0074             BaseObject("List") {
0075     }
0076 
0077     /**
0078      * Assignment constructor.
0079      * @param stdVector Vector storing values to be assigned.
0080      */
0081     List(std::vector<T> &stdVector) :
0082             BaseObject("List") {
0083         for (unsigned int i = 0; i != stdVector.size(); i++) {
0084             this->add(stdVector[i]);
0085         }
0086     }
0087 
0088     /**
0089      * Destructor.
0090      */
0091     virtual ~List() {
0092     }
0093 
0094     /**
0095      * Add new element.
0096      */
0097     virtual void add(const T &data) {
0098         m_data.push_back(data);
0099     }
0100 
0101     /**
0102      * Add list of new elements.
0103      * @param list List of elements to be added.
0104      */
0105     void add(const List<T> &list) {
0106         for (size_t i = 0; i != list.size(); i++) {
0107             add(list[i]);
0108         }
0109     }
0110 
0111     /**
0112      * Add vector of new elements.
0113      * @param list Vector of elements to be added.
0114      */
0115     void add(const std::vector<T> &vector) {
0116         for (size_t i = 0; i != vector.size(); i++) {
0117             add(vector[i]);
0118         }
0119     }
0120 
0121     /**
0122      * Get reference to element of given index.
0123      * @param n Index of requested element.
0124      * @return Reference to requested element.
0125      */
0126     const T& get(size_t n) const {
0127         return m_data.at(n);
0128     }
0129 
0130     /**
0131      * Get reference to element of given index.
0132      * @param n Index of requested element.
0133      * @return Reference to requested element.
0134      */
0135     T& operator[](size_t n) {
0136         return m_data[n];
0137     }
0138 
0139     /**
0140      * Get reference to element of given index.
0141      * @param n Index of requested element.
0142      * @return Reference to requested element.
0143      */
0144     const T& operator[](size_t n) const {
0145         return m_data[n];
0146     }
0147 
0148     /**
0149      * Get size of this list.
0150      * @return Size of this list.
0151      */
0152     size_t size() const {
0153         return m_data.size();
0154     }
0155 
0156     /**
0157      * Check if list is empty.
0158      * @return True if list is empty, otherwise false.
0159      */
0160     bool isEmpty() const {
0161         return (size() == 0) ? true : false;
0162     }
0163 
0164     /**
0165      * Return a pre-formatted characters string for output visualization of class member's values.
0166      * @return A pre-formatted characters string.
0167      */
0168     std::string toString() {
0169 
0170         ElemUtils::Formatter formatter;
0171 
0172         for (size_t i = 0; i != m_data.size(); i++) {
0173 
0174             formatter << '\n';
0175             formatter << "List[index] = " << i;
0176             formatter << '\n';
0177             formatter << m_data[i].toString();
0178         }
0179 
0180         return formatter.str();
0181     }
0182 
0183     //TODO implement
0184 //    void sort(const SortingMode &sortingMode) {
0185 //        switch (sortingMode.getType()) {
0186 //        case SortingMode::ASCENDING: {
0187 //
0188 //            break;
0189 //        }
0190 //        case SortingMode::DESCENDING: {
0191 //            break;
0192 //        }
0193 //        }
0194 //    }
0195 
0196     /**
0197      * Sort elements of this list.
0198      */
0199     void sort() {
0200         std::sort(m_data.begin(), m_data.end());
0201     }
0202 
0203     /**
0204      * Get vector containing stored elements.
0205      * @return Vector containing stored elements
0206      */
0207     std::vector<T> getData() const {
0208         return m_data;
0209     }
0210 
0211     /**
0212      * Clear list.
0213      */
0214     void clear() {
0215         m_data.clear();
0216     }
0217 
0218     /**
0219      * Resize list to given size.
0220      * @param n Size to be set.
0221      */
0222     void resize(size_t n) {
0223         m_data.resize(n);
0224     }
0225 
0226     /**
0227      * Remove the last element from this list.
0228      */
0229     void removeLast() {
0230         m_data.pop_back();
0231     }
0232 
0233     /**
0234      * Get the last element of this list.
0235      * @return Requested element.
0236      */
0237     const T& getLast() const {
0238 
0239         if (size() > 0)
0240             return m_data[size() - 1];
0241         else
0242             throw ElemUtils::CustomException(getClassName(), __func__,
0243                     "Cannot get the last object because the list is empty");
0244     }
0245 
0246     /**
0247      * Used to split a complex C++ object into a concatenation of simple types.
0248      * @param packet Target Packet.
0249      */
0250     void serialize(ElemUtils::Packet &packet) const {
0251         packet << static_cast<unsigned int>(size());
0252 
0253         for (size_t i = 0; i != size(); i++) {
0254             packet << m_data[i];
0255         }
0256     }
0257 
0258     /**
0259      * Used to rebuild a complex C++ object from a concatenation of simple type.
0260      * @param packet Input Packet.
0261      */
0262     void unserialize(ElemUtils::Packet &packet) {
0263         unsigned int packetSize;
0264         packet >> packetSize;
0265 
0266         for (size_t i = 0; i != packetSize; i++) {
0267             T data;
0268             packet >> data;
0269             add(data);
0270         }
0271     }
0272 
0273 protected:
0274 
0275     /**
0276      * Elements of this list.
0277      */
0278     std::vector<T> m_data;
0279 
0280 };
0281 
0282 /**
0283  * Stream operator to serialize class into Packet. See also GPDType::serialize().
0284  */
0285 template<class T>
0286 ElemUtils::Packet& operator <<(ElemUtils::Packet& packet, List<T>& list) {
0287     list.serialize(packet);
0288     return packet;
0289 }
0290 
0291 /**
0292  * Stream operator to retrieve class from Packet. See also GPDType::unserialize().
0293  */
0294 template<class T>
0295 ElemUtils::Packet& operator >>(ElemUtils::Packet& packet, List<T>& list) {
0296     list.unserialize(packet);
0297     return packet;
0298 }
0299 
0300 } /* namespace PARTONS */
0301 
0302 #endif /* LIST_H */