change PIMap::at and add const to PIMap and PIBinaryLog
This commit is contained in:
@@ -319,16 +319,24 @@ public:
|
||||
inline T & operator[](const Key & key) {
|
||||
bool f(false);
|
||||
const ssize_t i = _find(key, f);
|
||||
if (f) return pim_content[pim_index[i].index];
|
||||
if (f) return _value(i);
|
||||
pim_content.push_back(T());
|
||||
pim_index.insert(i, MapIndex(key, pim_content.size() - 1));
|
||||
return pim_content.back();
|
||||
}
|
||||
|
||||
//! \~english Same as \a value().
|
||||
//! \~russian Синоним \a value().
|
||||
//! \~english Read only access to element by `key`.
|
||||
//! \~russian Доступ исключительно на чтение к элементу по ключу `key`.
|
||||
//! \~\note
|
||||
//! \~english Element with key `key` must exists, otherwise will be undefined behavior.
|
||||
//! \~russian Элемент по ключу `key` должен существовать,
|
||||
//! иначе это приведёт к неопределённому поведению программы и ошибкам памяти.
|
||||
//! \~\sa \a operator[](), \a value(), \a key()
|
||||
inline T at(const Key & key) const { return value(key); }
|
||||
inline const T & at(const Key & key) const {
|
||||
bool f(false);
|
||||
const ssize_t i = _find(key, f);
|
||||
return _value(i);
|
||||
}
|
||||
|
||||
//! \~english Remove element with key `key` from the array and return it.
|
||||
//! \~russian Удаляет элемент с ключом `key` из массива и возвращает его.
|
||||
@@ -336,7 +344,7 @@ public:
|
||||
bool f(false);
|
||||
const ssize_t i = _find(key, f);
|
||||
if (!f) return default_;
|
||||
T ret(pim_content[pim_index[i].index]);
|
||||
const T ret = _value(i);
|
||||
_remove(i);
|
||||
return ret;
|
||||
}
|
||||
@@ -410,7 +418,8 @@ public:
|
||||
//! заданному в передаваемой функции `test`.
|
||||
inline PIMap<Key, T> & removeWhere(std::function<bool(const Key & key, const T & value)> test) {
|
||||
for (int i = 0; i < pim_index.size_s(); ++i) {
|
||||
if (test(pim_index[i].key, pim_content[pim_index[i].index])) {
|
||||
const auto & mi(pim_index[i]);
|
||||
if (test(mi.key, pim_content[mi.index])) {
|
||||
_remove(i);
|
||||
--i;
|
||||
}
|
||||
@@ -454,7 +463,7 @@ public:
|
||||
bool f(false);
|
||||
const ssize_t i = _find(key, f);
|
||||
if (f) {
|
||||
pim_content[pim_index[i].index] = value;
|
||||
_value(i) = value;
|
||||
} else {
|
||||
pim_content.push_back(value);
|
||||
pim_index.insert(i, MapIndex(key, pim_content.size() - 1));
|
||||
@@ -466,7 +475,7 @@ public:
|
||||
bool f(false);
|
||||
const ssize_t i = _find(key, f);
|
||||
if (f) {
|
||||
pim_content[pim_index[i].index] = std::move(value);
|
||||
_value(i) = std::move(value);
|
||||
} else {
|
||||
pim_content.push_back(std::move(value));
|
||||
pim_index.insert(i, MapIndex(key, pim_content.size() - 1));
|
||||
@@ -483,7 +492,7 @@ public:
|
||||
bool f(false);
|
||||
const ssize_t i = _find(pair.first, f);
|
||||
if (f) {
|
||||
pim_content[pim_index[i].index] = pair.second;
|
||||
_value(i) = pair.second;
|
||||
} else {
|
||||
pim_content.push_back(pair.second);
|
||||
pim_index.insert(i, MapIndex(pair.first, pim_content.size() - 1));
|
||||
@@ -496,7 +505,7 @@ public:
|
||||
Key k(std::move(pair.first));
|
||||
const ssize_t i = _find(k, f);
|
||||
if (f) {
|
||||
pim_content[pim_index[i].index] = std::move(pair.second);
|
||||
_value(i) = std::move(pair.second);
|
||||
} else {
|
||||
pim_content.push_back(std::move(pair.second));
|
||||
pim_index.insert(i, MapIndex(k, pim_content.size() - 1));
|
||||
@@ -512,7 +521,7 @@ public:
|
||||
bool f(false);
|
||||
const ssize_t i = _find(key, f);
|
||||
if (!f) return default_;
|
||||
return pim_content[pim_index[i].index];
|
||||
return _value(i);
|
||||
}
|
||||
|
||||
//! \~english Returns an array of values of all elements
|
||||
@@ -525,8 +534,9 @@ public:
|
||||
//! совпадает с `value` или `default_` если такого элемента нет.
|
||||
inline Key key(const T & value, const Key & default_ = Key()) const {
|
||||
for (int i = 0; i < pim_index.size_s(); ++i) {
|
||||
if (pim_content[pim_index[i].index] == value) {
|
||||
return pim_index[i].key;
|
||||
const auto & mi(pim_index[i]);
|
||||
if (pim_content[mi.index] == value) {
|
||||
return mi.key;
|
||||
}
|
||||
}
|
||||
return default_;
|
||||
@@ -547,7 +557,8 @@ public:
|
||||
//! \~russian Выполняет функцию `void f(const Key & key, const T & value)` для каждого элемента массива.
|
||||
inline void forEach(std::function<void(const Key & key, const T & value)> f) const {
|
||||
for (int i = 0; i < pim_index.size_s(); ++i) {
|
||||
f(pim_index[i].key, pim_content[pim_index[i].index]);
|
||||
const auto & mi(pim_index[i]);
|
||||
f(mi.key, pim_content[mi.index]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -560,7 +571,8 @@ public:
|
||||
PIMap<Key2, T2> ret;
|
||||
ret.reserve(size());
|
||||
for (int i = 0; i < pim_index.size_s(); ++i) {
|
||||
ret.insert(f(pim_index[i].key, pim_content[pim_index[i].index]));
|
||||
const auto & mi(pim_index[i]);
|
||||
ret.insert(f(mi.key, pim_content[mi.index]));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -574,7 +586,8 @@ public:
|
||||
PIVector<ST> ret;
|
||||
ret.reserve(size());
|
||||
for (int i = 0; i < pim_index.size_s(); ++i) {
|
||||
ret << f(pim_index[i].key, pim_content[pim_index[i].index]);
|
||||
const auto & mi(pim_index[i]);
|
||||
ret << f(mi.key, pim_content[mi.index]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -586,8 +599,10 @@ public:
|
||||
inline PIMap<Key, T> filter(std::function<bool(const Key & key, const T & value)> test) const {
|
||||
PIMap<Key, T> ret;
|
||||
for (int i = 0; i < pim_index.size_s(); ++i) {
|
||||
if (test(pim_index[i].key, pim_content[pim_index[i].index])) {
|
||||
ret.insert(pim_index[i].key, pim_content[pim_index[i].index]);
|
||||
const auto & mi(pim_index[i]);
|
||||
const auto & v(pim_content[mi.index]);
|
||||
if (test(mi.key, v)) {
|
||||
ret.insert(mi.key, v);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
@@ -650,7 +665,8 @@ private:
|
||||
|
||||
inline const value_type _pair(ssize_t index) const {
|
||||
if (index < 0 || index >= pim_index.size_s()) return value_type();
|
||||
return value_type(pim_index[index].key, pim_content[pim_index[index].index]);
|
||||
const auto & mi(pim_index[index]);
|
||||
return value_type(mi.key, pim_content[mi.index]);
|
||||
}
|
||||
|
||||
inline Key & _key(ssize_t index) { return pim_index[index].key; }
|
||||
|
||||
@@ -177,8 +177,9 @@ bool PIBinaryLog::threadedRead(const uchar * readed, ssize_t size) {
|
||||
if (!canRead() || isEnd()) return PIIODevice::threadedRead(readed, size);
|
||||
is_thread_ok = false;
|
||||
logmutex.lock();
|
||||
PISystemTime pt, lastrec_timestamp = lastrecord.timestamp;
|
||||
const PISystemTime lastrec_timestamp = lastrecord.timestamp;
|
||||
logmutex.unlock();
|
||||
PISystemTime pt;
|
||||
double delay;
|
||||
switch (play_mode) {
|
||||
case PlayRealTime:
|
||||
@@ -258,7 +259,7 @@ PIString PIBinaryLog::getLogfilePath(const PIString & log_dir, const PIString &
|
||||
<< "Creating directory" << dir.path();
|
||||
dir.make(true);
|
||||
}
|
||||
PIString npath = log_dir + PIDir::separator + prefix + PIDateTime::current().toString("yyyy_MM_dd__hh_mm_ss");
|
||||
const PIString npath = log_dir + PIDir::separator + prefix + PIDateTime::current().toString("yyyy_MM_dd__hh_mm_ss");
|
||||
PIString cnpath = npath + ".binlog";
|
||||
int i = 1;
|
||||
while (PIFile::isExists(cnpath)) {
|
||||
@@ -317,7 +318,7 @@ int PIBinaryLog::writeBinLog(int id, const void * data, int size) {
|
||||
}
|
||||
if (is_pause) return 0;
|
||||
logmutex.lock();
|
||||
int res = writeRecord(id, PISystemTime::current() - startlogtime, data, size);
|
||||
const int res = writeRecord(id, PISystemTime::current() - startlogtime, data, size);
|
||||
switch (split_mode) {
|
||||
case SplitSize:
|
||||
if (log_size > split_size) createNewFile();
|
||||
@@ -341,7 +342,7 @@ int PIBinaryLog::writeBinLog(int id, const void * data, int size) {
|
||||
int PIBinaryLog::writeBinLog_raw(int id, const PISystemTime & time, const void * data, int size) {
|
||||
if (size <= 0 || !canWrite()) return -1;
|
||||
logmutex.lock();
|
||||
int res = writeRecord(id, time, data, size);
|
||||
const int res = writeRecord(id, time, data, size);
|
||||
logmutex.unlock();
|
||||
if (res > 0)
|
||||
return size;
|
||||
@@ -386,7 +387,7 @@ PIByteArray PIBinaryLog::readBinLog(int id, PISystemTime * time, int * readed_id
|
||||
|
||||
int PIBinaryLog::readBinLog(int id, void * read_to, int max_size, PISystemTime * time, int * readed_id) {
|
||||
if (max_size <= 0 || read_to == 0) return -1;
|
||||
PIByteArray ba = readBinLog(id, time, readed_id);
|
||||
const PIByteArray ba = readBinLog(id, time, readed_id);
|
||||
if (ba.isEmpty()) return -1;
|
||||
int sz = piMini(max_size, ba.size());
|
||||
memcpy(read_to, ba.data(), sz);
|
||||
@@ -404,7 +405,7 @@ void PIBinaryLog::setHeader(const PIByteArray & header) {
|
||||
}
|
||||
|
||||
|
||||
PIByteArray PIBinaryLog::getHeader() {
|
||||
PIByteArray PIBinaryLog::getHeader() const {
|
||||
return index.info.user_header;
|
||||
}
|
||||
|
||||
@@ -432,7 +433,7 @@ ssize_t PIBinaryLog::readDevice(void * read_to, ssize_t max_size) {
|
||||
piCoutObj << "Read record error";
|
||||
return -1;
|
||||
}
|
||||
ssize_t sz = piMini(max_size, br.data.size());
|
||||
const ssize_t sz = piMini(max_size, br.data.size());
|
||||
if (sz < br.data.size_s()) piCoutObj << "too small read buffer:" << max_size << ", data size:" << br.data.size();
|
||||
memcpy(read_to, br.data.data(), sz);
|
||||
return sz;
|
||||
@@ -445,7 +446,7 @@ ssize_t PIBinaryLog::writeDevice(const void * data, ssize_t size) {
|
||||
|
||||
|
||||
void PIBinaryLog::restart() {
|
||||
bool th = isThreadedRead();
|
||||
const bool th = isThreadedRead();
|
||||
if (th) stopThreadedRead();
|
||||
if (!canRead()) return;
|
||||
logmutex.unlock();
|
||||
@@ -464,9 +465,9 @@ void PIBinaryLog::restart() {
|
||||
|
||||
bool PIBinaryLog::writeFileHeader() {
|
||||
if (file.write(binlog_sig, PIBINARYLOG_SIGNATURE_SIZE) <= 0) return false;
|
||||
uchar version = PIBINARYLOG_VERSION;
|
||||
const uchar version = PIBINARYLOG_VERSION;
|
||||
if (file.write(&version, 1) <= 0) return false;
|
||||
uint32_t sz = user_header.size();
|
||||
const uint32_t sz = user_header.size();
|
||||
file.write(&sz, 4);
|
||||
file.write(user_header);
|
||||
file.flush();
|
||||
@@ -578,7 +579,7 @@ int PIBinaryLog::writeRecord(int id, PISystemTime time, const void * data, int s
|
||||
++index.info.records_count;
|
||||
is_indexed = true;
|
||||
}
|
||||
int ret = file.write(logdata.data(), logdata.size());
|
||||
const int ret = file.write(logdata.data(), logdata.size());
|
||||
file.flush();
|
||||
write_count++;
|
||||
log_size = file.size();
|
||||
@@ -638,7 +639,7 @@ void PIBinaryLog::parseLog(PIFile * f, PIBinaryLog::BinLogInfo * info, PIVector<
|
||||
f->read(&sz, 4);
|
||||
if (sz > 0) {
|
||||
PIByteArray user_hdr = f->read(sz);
|
||||
if (info) info->user_header = user_hdr;
|
||||
if (info) info->user_header.swap(user_hdr);
|
||||
}
|
||||
}
|
||||
PIByteArray ba;
|
||||
@@ -646,7 +647,7 @@ void PIBinaryLog::parseLog(PIFile * f, PIBinaryLog::BinLogInfo * info, PIVector<
|
||||
br.id = 0;
|
||||
br.size = 0;
|
||||
bool first = true;
|
||||
size_t hdr_size = sizeof(Record) - sizeof(PIByteArray);
|
||||
constexpr size_t hdr_size = sizeof(Record) - sizeof(PIByteArray);
|
||||
ba.resize(hdr_size);
|
||||
while (1) {
|
||||
ba.resize(hdr_size);
|
||||
@@ -721,7 +722,7 @@ bool PIBinaryLog::cutBinLog(const PIBinaryLog::BinLogInfo & src, const PIString
|
||||
<< "Error, can't open" << src.path;
|
||||
return false;
|
||||
}
|
||||
PIVector<int> ids = src.records.keys();
|
||||
const PIVector<int> ids = src.records.keys();
|
||||
slog.seekTo(from);
|
||||
PIBinaryLog dlog;
|
||||
dlog.createNewFile(dst);
|
||||
@@ -825,7 +826,7 @@ bool PIBinaryLog::joinBinLogsSerial(const PIStringList & src,
|
||||
|
||||
bool PIBinaryLog::createIndex() {
|
||||
logmutex.lock();
|
||||
llong cp = file.pos();
|
||||
const llong cp = file.pos();
|
||||
file.seekToBegin();
|
||||
index.clear();
|
||||
parseLog(&file, &index.info, &index.index);
|
||||
@@ -870,7 +871,7 @@ void PIBinaryLog::seekTo(int rindex) {
|
||||
|
||||
|
||||
bool PIBinaryLog::seek(const PISystemTime & time) {
|
||||
int ci = posForTime(time);
|
||||
const int ci = posForTime(time);
|
||||
if (ci >= 0) {
|
||||
seekTo(ci);
|
||||
return true;
|
||||
@@ -930,9 +931,9 @@ PIString PIBinaryLog::constructFullPathDevice() const {
|
||||
|
||||
|
||||
void PIBinaryLog::configureFromFullPathDevice(const PIString & full_path) {
|
||||
PIStringList pl = full_path.split(":");
|
||||
const PIStringList pl = full_path.split(":");
|
||||
for (int i = 0; i < pl.size_s(); ++i) {
|
||||
PIString p(pl[i]);
|
||||
const PIString p(pl[i]);
|
||||
switch (i) {
|
||||
case 0: setLogDir(p); break;
|
||||
case 1: setFilePrefix(p); break;
|
||||
@@ -979,7 +980,7 @@ void PIBinaryLog::propertyChanged(const char * s) {
|
||||
default_id = property("defaultID").toInt();
|
||||
rapid_start = property("rapidStart").toBool();
|
||||
play_mode = (PlayMode)property("playMode").toInt();
|
||||
double ps = property("playSpeed").toDouble();
|
||||
const double ps = property("playSpeed").toDouble();
|
||||
play_speed = ps > 0. ? 1. / ps : 0.;
|
||||
play_delay = property("playDelay").toSystemTime();
|
||||
split_mode = (SplitMode)property("splitMode").toInt();
|
||||
|
||||
@@ -251,7 +251,7 @@ public:
|
||||
void setHeader(const PIByteArray & header);
|
||||
|
||||
//! Get custom file header
|
||||
PIByteArray getHeader();
|
||||
PIByteArray getHeader() const;
|
||||
|
||||
#ifdef DOXYGEN
|
||||
//! Read one message from binlog file, with ID contains in "filterID" or any ID, if "filterID" is empty
|
||||
@@ -462,9 +462,9 @@ inline PICout operator<<(PICout s, const PIBinaryLog::BinLogInfo & bi) {
|
||||
}
|
||||
s << "read records " << bi.records_count << " in " << bi.records.size() << " types, log size " << bi.log_size;
|
||||
s << "\nlog start " << bi.start_time << " , log end " << bi.end_time;
|
||||
PIVector<int> keys = bi.records.keys();
|
||||
const auto keys = bi.records.keys();
|
||||
for (int i: keys) {
|
||||
PIBinaryLog::BinLogRecordInfo bri = bi.records.at(i);
|
||||
const auto & bri(bi.records.at(i));
|
||||
s << "\n record id " << bri.id << " , count " << bri.count;
|
||||
s << "\n record start " << bri.start_time << " , end " << bri.end_time;
|
||||
s << "\n record size " << bri.minimum_size << " - " << bri.maximum_size;
|
||||
|
||||
Reference in New Issue
Block a user