/******************************************************************************/ /* */ /* SEGMENT.HH */ /* */ /******************************************************************************/ // Shourov K. Chatterji // shourov@ligo.caltech.edu // $Id: segment.hh,v 1.1 2006/11/03 15:33:59 shourov Exp $ /* Protect against double inclusion */ /* -------------------------------- */ #ifndef SEGMENT_HH #define SEGMENT_HH /* Include header files */ /* -------------------- */ #include #include #include #include #include #include /* Begin namespace segment */ /* ----------------------- */ namespace segment { /* Begin class Segment */ /* ------------------- */ class Segment { /* Public interface of class Segment */ /* --------------------------------- */ public: /* Constructor and destructor declarations */ /* --------------------------------------- */ // default segment constructor Segment(); // default segment destructor virtual ~Segment(); // segment copy constructor Segment(const Segment& segment); // segment assignment operator Segment& operator=(const Segment& segment); // full segment constructor Segment(double startTime, double stopTime); /* Mutator method declarations */ /* --------------------------- */ // set properties of segment void set(double startTime, double stopTime); // set start time of segment void setStartTime(double startTime); // set stop time of segment void setStopTime(double stopTime); // time shift segment void timeShift(double timeShift); // round times down to the nearest sample void align(double sampleFrequency = 1.0); /* Accessor method declarations */ /* ---------------------------- */ // get start time of segment double getStartTime() const; // get stop time of segment double getStopTime() const; // get duration of segment double getDuration() const; /* Friend function declarations */ /* ---------------------------- */ // write segment to output stream friend std::ostream& operator<<(std::ostream& ostream, const Segment& segment); // read segment from input stream friend std::istream& operator>>(std::istream& istream, Segment& segment); /* Protected interface of class Segment */ /* ------------------------------------ */ protected: /* Private interface of class Segment */ /* ---------------------------------- */ private: /* State variables */ /* --------------- */ // start time of segment double startTime; // stop time of segment double stopTime; /* End class Segment */ /* ----------------- */ }; /* Non-member function declarations */ /* -------------------------------- */ // write segment to output stream std::ostream& operator<<(std::ostream& ostream, const Segment& segment); // read segment from input stream std::istream& operator>>(std::istream& istream, Segment& segment); /* Constructor and destructor definitions */ /* -------------------------------------- */ // default segment constructor Segment::Segment() : startTime(0.0), stopTime(0.0) { } // default segment destructor Segment::~Segment() { } // segment copy constructor Segment::Segment(const Segment& segment) : startTime(segment.startTime), stopTime(segment.stopTime) { } // segment assignment operator Segment& Segment::operator=(const Segment& segment) { if (this != &segment) { startTime = segment.startTime; stopTime = segment.stopTime; } return *this; } // full segment constructor Segment::Segment(double startTime, double stopTime) : startTime(startTime), stopTime(stopTime) { } /* Mutator method definitions */ /* -------------------------- */ // set properties of segment void Segment::set(double startTime, double stopTime) { this->startTime = startTime; this->stopTime = stopTime; } // set start time of segment inline void Segment::setStartTime(double startTime) { this->startTime = startTime; } // set stop time of segment inline void Segment::setStopTime(double stopTime) { this->stopTime = stopTime; } // time shift segment inline void Segment::timeShift(double timeShift) { startTime += timeShift; stopTime += timeShift; } // round times down to the nearest sample inline void Segment::align(double sampleFrequency) { startTime = std::floor(startTime * sampleFrequency) / sampleFrequency; stopTime = std::floor(stopTime * sampleFrequency) / sampleFrequency; } /* Accessor method definitions */ /* --------------------------- */ // get start time of segment inline double Segment::getStartTime() const { return startTime; } // get stop time of segment inline double Segment::getStopTime() const { return stopTime; } // get duration of segment inline double Segment::getDuration() const { return stopTime - startTime; } /* Non-member function definitions */ /* ------------------------------- */ // write segment to output stream std::ostream& operator<<(std::ostream& ostream, const Segment& segment) { std::ios_base::fmtflags originalFlags = ostream.flags(); std::streamsize originalPrecision = ostream.precision(); char originalFill = ostream.fill(); ostream.flags(std::ios_base::dec | std::ios_base::showpoint); ostream << std::right; ostream << std::fixed; if (segment.startTime == -std::numeric_limits::infinity()) { ostream << std::setfill(' '); } else { ostream << std::setfill('0'); } ostream << std::setw(19) << std::setprecision(9) << segment.startTime; ostream << " "; if (segment.stopTime == +std::numeric_limits::infinity()) { ostream << std::setfill(' '); } else { ostream << std::setfill('0'); } ostream << std::setw(19) << std::setprecision(9) << segment.stopTime; ostream << std::endl; ostream.flags(originalFlags); ostream.precision(originalPrecision); ostream.fill(originalFill); return ostream; } // read segment from input stream std::istream& operator>>(std::istream& istream, Segment& segment) { std::string lineString; std::getline(istream, lineString, '\n'); std::istringstream lineStringStream(lineString); std::string entryString; lineStringStream >> entryString; if ((entryString == "-inf") || (entryString == "-Inf")) { segment.startTime = -std::numeric_limits::infinity(); } else { std::istringstream entryStringStream(entryString); entryStringStream >> segment.startTime; if (entryStringStream.fail()) { istream.setstate(std::ios_base::failbit); return istream; } } lineStringStream >> entryString; if ((entryString == "inf") || (entryString == "Inf")) { segment.stopTime = +std::numeric_limits::infinity(); } else { std::istringstream entryStringStream(entryString); entryStringStream >> segment.stopTime; if (entryStringStream.fail()) { istream.setstate(std::ios_base::failbit); return istream; } } return istream; } /* End namespace segment */ /* --------------------- */ } /* End protection against double inclusion */ /* --------------------------------------- */ #endif