void CSTLMemoryTester::test_contain_assign() { #ifdef NEED_OPERATOR_FUNC typedef std::vector<CMyTestData> DataContainer; DataContainer srcDataContainer; //除了一开始的时候是构造函数,其他时候都是调用拷贝构造 DECLARE_MYTEST_DATA_COUNT_CHECKER(ConstructCheck,ctConstructCount, 5, __FILE__, __LINE__); { for (long i = 0; i < 5; i++) { srcDataContainer.push_back(CMyTestData(i, 0)); } { DECLARE_MYTEST_DATA_COUNT_CHECKER(ConstructCheck,ctConstructCount, 0, __FILE__, __LINE__); //赋值时会对容器中的每一个元素调用拷贝构造函数 -- DECLARE_MYTEST_DATA_COUNT_CHECKER(CopyConstructCheck,ctCopyConstructCount, 5, __FILE__, __LINE__); //超出dstDataContainer的生存周期后释放 DECLARE_MYTEST_DATA_COUNT_CHECKER(DestructCheck,ctDestructCount, 5, __FILE__, __LINE__); DataContainer dstDataContainer; dstDataContainer = srcDataContainer; } { DECLARE_MYTEST_DATA_COUNT_CHECKER(ConstructCheck,ctConstructCount, 0, __FILE__, __LINE__); //assign时会对容器中的每一个元素调用拷贝构造函数 -- DECLARE_MYTEST_DATA_COUNT_CHECKER(CopyConstructCheck,ctCopyConstructCount, 5, __FILE__, __LINE__); //超出dstDataContainer的生存周期后释放 DECLARE_MYTEST_DATA_COUNT_CHECKER(DestructCheck,ctDestructCount, 5, __FILE__, __LINE__); DataContainer dstDataContainer; dstDataContainer.assign(srcDataContainer.begin(), srcDataContainer.end()); } { DECLARE_MYTEST_DATA_COUNT_CHECKER(ConstructCheck,ctConstructCount, 0, __FILE__, __LINE__); DECLARE_MYTEST_DATA_COUNT_CHECKER(CopyConstructCheck,ctCopyConstructCount, 5, __FILE__, __LINE__); DECLARE_MYTEST_DATA_COUNT_CHECKER(DestructCheck,ctDestructCount, 5, __FILE__, __LINE__); DataContainer dstDataContainer; dstDataContainer.reserve(srcDataContainer.size()); std::copy(srcDataContainer.begin(), srcDataContainer.end(), std::back_insert_iterator<DataContainer>(dstDataContainer)); } } #endif }
// remove all elements = val void erase(T const& val) { size_type counter; typename std::vector<T>::iterator pos; for (difference_type i = m_offsets.size()-2; i>=0; --i) { for (difference_type j = m_offsets[i+1]-m_offsets[i]-1; j>=0; --j) { pos = m_data.begin() + m_offsets[i] + j; if (*pos == val) { m_data.erase(pos); for (int k = i+1; k < m_offsets.size(); ++k) --m_offsets[k]; } } } }
// insert new column if necessary T& operator() (size_type row, size_type col) { size_type const sr = size(row); if (col < sr) return m_data[ m_offsets[row] + col]; else { Int const n_new = col - sr + 1; typename std::vector<T>::iterator pos = m_data.begin() + m_offsets[row+1]; if (m_data.end()-pos < 0) // pos never can be greater than m_data.end(), otherwise m_offsets is wrong throw; m_data.insert(pos, n_new, T()); for (size_type k = row+1; k < m_offsets.size(); ++k) m_offsets[k] += n_new; return m_data[ m_offsets[row] + col]; } }
int CXMLDb::Insert(DataContainerList &DataList) { pthread_mutex_lock(&FileMutex); if(!LoadBackupFile()) { Log.error("Unable to Load XML file\n"); pthread_mutex_unlock(&FileMutex); return false; } while (!DataList.empty()) { DataContainer Data = DataList.front(); XMLNode *ProbeData = m_BackupFile->NewElement("pd"); for( DataContainer::iterator ii=Data.begin(); ii!=Data.end(); ++ii) { string name = (*ii).first; string value = (*ii).second; ProbeData->ToElement()->SetAttribute(name.c_str(),value.c_str()); } m_BackupFile->FirstChildElement()->InsertEndChild(ProbeData); DataList.pop_front(); } if(!SaveBackupFile()) { Log.error("Unable to save XML file\n"); pthread_mutex_unlock(&FileMutex); return false; } pthread_mutex_unlock(&FileMutex); return true; }
int CMysqlDb::Insert(DataContainerList &DataList) { if(DataList.size() == 0) return true; while (!DataList.empty()) { string ColumnsString = "("; string ValuesString = "("; DataContainer Data = DataList.front(); DataContainer::iterator LastElement = Data.end(); --LastElement; for( DataContainer::iterator ii=Data.begin(); ii!=Data.end(); ++ii) { ColumnsString += (*ii).first; ValuesString += (*ii).second; if(ii == LastElement) { ColumnsString += ")"; ValuesString += ")"; } else { ColumnsString += ","; ValuesString += ","; } } string query = "INSERT IGNORE INTO " + m_table + " " + ColumnsString + " VALUES " + ValuesString + ";"; pthread_mutex_lock(&queryMutex); /* send SQL query */ if (mysql_query(&m_conn, query.c_str())) { int error = mysql_errno(&m_conn); pthread_mutex_unlock(&queryMutex); Log.log("MySQL Error (%d): %s", error, mysql_error(&m_conn)); // Server error (fatal). Most probably because of a bug or something like that // Discard the entry so the program doesn't stop sending data because of it if(error >= 1000 && error < 2000) { Log.log("[%s]\n", query.c_str()); DataList.pop_front(); continue; } // Client error (transient). Do not delete the results and retry sending them else return false; } else pthread_mutex_unlock(&queryMutex); DataList.pop_front(); } return true; }
std::vector<T> getData(size_type row) const { typename std::vector<T>::iterator beg = m_data.begin() + m_offsets[row]; typename std::vector<T>::iterator end = beg + size(row); return std::vector<T> (beg,end); }