// EventIdList.h #ifndef EventIdList_H #define EventIdList_H // Ordered container of EventId's. // // Includes an iterator over EventId's (not just EventIdRange). // // I was lazy and used the same class for iterator and // const_iterator. This is the reason for const_cast's and // compiler warnings. #include #include #include #include #if ( __GNUC__ == 2 ) namespace std { template struct iterator { typedef _Category iterator_category; typedef _Tp value_type; typedef _Distance difference_type; typedef _Pointer pointer; typedef _Reference reference; }; } #else #include #endif #include "dataset_util/XmlElement.h" #include "dataset_id/EventId.h" #include "dataset_id/EventIdRange.h" class EventIdList { public: // typedefs typedef EventIdRange Range; typedef std::vector RangeList; private: // Embedded classes. class ConstIterator : public std::iterator { friend class EventIdList; private: const EventIdList* _plist; RangeList::const_iterator _irng; EventId::EventNumber _evt; public: // Default constructor. ConstIterator(); // Full constructor. ConstIterator(const EventIdList* plist, RangeList::const_iterator& irng, EventId::EventNumber evt); // Constructor for the end of a list. explicit ConstIterator(const EventIdList* plist); // Equality. bool operator==(const ConstIterator& rhs) const; // Inequality. bool operator!=(const ConstIterator& rhs) const; // Dereference. EventId operator*() const; //operator->() const; ConstIterator& operator++(); ConstIterator operator++(int); ConstIterator& operator--(); ConstIterator operator--(int); }; public: // typedefs // C++ std requirements. typedef EventId key_type; typedef key_type value_type; typedef std::less key_compare; typedef key_compare value_compare; //typedef std::allocator allocator_type; typedef value_type& reference; typedef const value_type& const_reference; typedef ConstIterator iterator; typedef ConstIterator const_iterator; typedef RangeList::size_type size_type; typedef RangeList::difference_type difference_type; typedef value_type* pointer; typedef const value_type* const_pointer; //typedef void reverse_iterator; //typedef void const_reverse_iterator; // Return from insert functions. typedef std::pair InsertStatus; private: // data // Size. size_type m_size; // Ordered list of ranges. RangeList m_ranges; // Cached XML representation. bool m_update_from_xml; XmlElement m_ele; public: // static functions // XML name. static const char* xml_name() { return "EventIdList"; } // XML DTD. static const Text& dtd(); public: // Constructors and destructor // Default constructor. // Creates an emty list. EventIdList(); // Constructor from a range. explicit EventIdList(const EventIdRange& rng); // XML constructor. // If try_children is true, then the children of the XML element will // be searched for a list. The corresponding child will be used if there // is exactly one match. explicit EventIdList (const XmlElement& ele, bool try_children =false); public: // const functions // Return the list of ranges. const RangeList& ranges() const; // Iterators. const_iterator begin() const; const_iterator end() const; // Capacity. //bool empty() const; size_type size() const; size_type max_size() const; // First entry. // Returns invalid for empty list. value_type front() const; // Last entry. // Returns invalid for empty list. value_type back() const; // Find. const_iterator find(const EventId& eid) const; // Is an event ID contained in this list? bool contains(const EventId& eid) const; // Is an event ID range contained in this list? bool contains(const EventIdRange& rng) const; // Is every element in the argument contained in this? bool contains(const EventIdList& eids) const; // Do any events appear in both the this and the argument? bool overlaps(const EventIdList& eids) const; // Memory managed by this object. size_t memsize() const; // Write to XML. // Caller is expected to delete the returned object. const XmlElement* xml() const; // Return the cached xml_representation. // Copied during construction from XML or when XML is // generated. const XmlElement& cached_xml() const { return m_ele; } // Return if the list is only in an XML representation. // It may take time to build list from XML if any methods are invoked. bool xml_only() const { return m_update_from_xml; } public: // non-const functions // Insert an Event ID. InsertStatus insert(const EventId& rhs); const_iterator insert(const const_iterator& ipos, const EventId& rhs); // Insert a range of event ID's. InsertStatus insert(const Range& rhs); // Remove the ID referenced by an iterator. void erase(const const_iterator& ieid); // Remove an ID with the specified value. // The number of ID's removed is returned. size_type erase(const EventId& eid); // Select the ID's that are also contained in the input list. // The new size of the container is returned. size_type select(const EventIdList& eids); // Add the ID's from the input list. // The new size of the container is returned. // If noshare is true, then the merge is not done if lists // share any entries. Zero is returned if this condition is violated. size_type merge(const EventIdList& eids, bool noshare =false); // Update the class using the XML representation // if that representation is valid and the size is zero. // This is done automatically and need not be called by users. void xml_update() const; // Output stream. std::ostream& ostr(std::ostream& lhs, std::string indent) const; }; // Equality bool operator==(const EventIdList& lhs, const EventIdList& rhs); // Inequality bool operator!=(const EventIdList& lhs, const EventIdList& rhs); // Output stream. std::ostream& operator<<(std::ostream& lhs, const EventIdList& rhs); #endif