12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #include "cappedstorage.h"
- #include <cstring>
- #include <iostream>
- #include <easylogging++.h>
- #include "SystemConfig.h"
-
- CappedStorage::CappedStorage(bool _biased): biased(_biased)
- {
- if(biased){
- X.resize(0, 2);
- }
- else
- X.resize(0, 1);
- accessMtx = std::make_unique<std::mutex>();
- }
-
- void CappedStorage::lock(){
- accessMtx->lock();
- }
-
- void CappedStorage::unlock(){
- accessMtx->unlock();
- }
-
- void CappedStorage::store(const std::vector<uint16_t>& d)
- {
- std::scoped_lock lock(*accessMtx);
- float value = 0;
- std::memcpy(&value, d.data(), 4);
-
- const double time = c::time_point_cast<c::seconds>(c::system_clock::now()).time_since_epoch().count();
-
- X.conservativeResize(X.rows()+1, X.cols());
- y.conservativeResize(y.rows()+1, 1);
- y(y.rows()-1, 0) = value;
-
- if(biased){
- X(X.rows()-1, 0) = 1;
- X(X.rows()-1, 1) = time;
- }
- else
- X(X.rows()-1, 0) = time;
- }
-
-
- void CappedStorage::clear()
- {
- std::scoped_lock lock(*accessMtx);
- X.resize(0, Eigen::NoChange_t());
- }
-
- //Stream operator for 1 dimensional X Matrix
- std::ostream& operator<<(std::ostream& os, const CappedStorage& c){
- std::scoped_lock lock(*(c.accessMtx));
- int col =0;
- if(c.biased){
- col = 1;
- }
-
- for (int i = 0; i < c.X.rows(); i++) {
- os << c.X(i, col) << ",";
- }
- os << std::endl;
- for (int i = 0; i < c.y.size(); i++) {
- os << c.y(i, 0) << ",";
- }
- os << std::endl;
-
- return os;
- }
|