OSDN Git Service

Change internal data structures.
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Tue, 30 Jun 2009 17:00:42 +0000 (02:00 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Tue, 30 Jun 2009 17:00:42 +0000 (02:00 +0900)
src/thread_list_model.cxx
src/thread_list_model.hxx

index 26fdb25..da9b8bb 100644 (file)
@@ -82,27 +82,15 @@ struct GetColumnGType {
 
 namespace {
 struct CompareNumber {
-  CompareNumber(boost::unordered_map<std::string, ModelColumns>& cols) :
-    cols_(cols) {}
-
-  bool operator()(const std::string& lhs, const std::string& rhs) const {
-    boost::unordered_map<std::string, ModelColumns>::const_iterator it = cols_.find(lhs);
-    assert(it != cols_.end());
-    const ModelColumns& left_cols = it->second;
-
-    it = cols_.find(rhs);
-    assert(it != cols_.end());
-    const ModelColumns& right_cols = it->second;
-
-    int left_num = model_column::field<model_column::Number>(left_cols);
-    int right_num = model_column::field<model_column::Number>(right_cols);
+  bool operator()(const ModelColumns& lhs, const ModelColumns& rhs) const {
+    int left_num = model_column::field<model_column::Number>(lhs);
+    int right_num = model_column::field<model_column::Number>(rhs);
 
     if (left_num == 0) left_num = boost::integer_traits<int>::const_max;
     if (right_num == 0) right_num = boost::integer_traits<int>::const_max;
 
     return left_num < right_num;
   }
-  boost::unordered_map<std::string, ModelColumns>& cols_;
 };
 }
 
@@ -121,12 +109,12 @@ ThreadListModel::~ThreadListModel() {}
 
 void ThreadListModel::append(const ModelColumns& record) {
   const model_column::ID::type& id = model_column::field<model_column::ID>(record);
-  StoreType::iterator it = list_.find(id);
-  if (it != list_.end()) {
-    list_[id] = record;
+  OrderType::iterator it = order_.find(id);
+  if (it != order_.end()) {
+    list_[it->second] = record;
   } else {
-    list_[id] = record;
-    order_.push_back(model_column::field<model_column::ID>(record));
+    list_.push_back(record);
+    order_[id] = list_.size()-1;
 
     const size_t index = list_.size() -1;
     iterator iter;
@@ -160,15 +148,22 @@ void ThreadListModel::set_buffer(
     append(cols);
   }
 
-  boost::unordered_map<std::string, int> old_order;
-  for (size_t i = 0; i != order_.size(); ++i) old_order[order_[i]] = i;
+  OrderType old_order = order_;
 
-  std::sort(order_.begin(), order_.end(), CompareNumber(list_));
+  std::sort(list_.begin(), list_.end(), CompareNumber());
+
+  for (size_t i = 0; i != list_.size(); i++) {
+    const model_column::ID::type& id =
+      model_column::field<model_column::ID>(list_[i]);
+    order_[id] = i;
+  }
 
   std::vector<int> new_order;
   new_order.reserve(order_.size());
 
-  BOOST_FOREACH(const std::string& id, order_) {
+  BOOST_FOREACH(const ModelColumns& record, list_) {
+    const model_column::ID::type& id =
+      model_column::field<model_column::ID>(record);
     new_order.push_back(old_order[id]);
   }
 
@@ -178,39 +173,37 @@ void ThreadListModel::set_buffer(
 
 void ThreadListModel::get_buffer(
     boost::unordered_map<model_column::ID::type, ModelColumns>& buffer) const {
-  buffer = list_;
+  buffer.clear();
+  BOOST_FOREACH(const ModelColumns& record, list_) {
+    const model_column::ID::type& id =
+      model_column::field<model_column::ID>(record);
+    buffer[id] = record;
+  }
 }
 
 const ModelColumns& ThreadListModel::get_model_columns(const size_t row_index) const {
   assert(row_index < list_.size());
-
-  const std::string& id = order_[row_index];
-  StoreType::const_iterator it = list_.find(id);
-
-  assert(it != list_.end());
-
-  return it->second;
+  return list_[row_index];
 }
 
 void ThreadListModel::update_row(const ModelColumns& record) {
   const model_column::ID::type& id = model_column::field<model_column::ID>(record);
 
-  StoreType::iterator it = list_.find(id);
-  if (it != list_.end()) {
-    model_column::field<model_column::Title>(it->second) =
+  OrderType::iterator it = order_.find(id);
+  if (it != order_.end()) {
+    const size_t row_index = it->second;
+
+    model_column::field<model_column::Title>(list_[row_index]) =
       model_column::field<model_column::Title>(record);
-    model_column::field<model_column::LineCount>(it->second) =
+    model_column::field<model_column::LineCount>(list_[row_index]) =
       model_column::field<model_column::LineCount>(record);
-    OrderType::iterator jt = std::find(order_.begin(), order_.end(), id);
-    if (jt != order_.end()) {
-      const size_t row_index = std::distance(order_.begin(), jt);
-      Gtk::TreePath path;
-      path.append_index(row_index);
-      iterator iter;
-      iter.set_stamp(1);
-      iter.gobj()->user_data = GINT_TO_POINTER(row_index);
-      row_changed(path, iter);
-    }
+
+    Gtk::TreePath path;
+    path.append_index(row_index);
+    iterator iter;
+    iter.set_stamp(1);
+    iter.gobj()->user_data = GINT_TO_POINTER(row_index);
+    row_changed(path, iter);
   } else {
     append(record);
   }
@@ -269,12 +262,8 @@ void ThreadListModel::get_value_vfunc(const iterator& iter, const int column,
   const size_t index = GPOINTER_TO_INT(iter.gobj()->user_data);
   if (index >= order_.size()) return;
 
-  const std::string& id = order_[index];
-
-  StoreType::const_iterator it = list_.find(id);
-  assert(it != list_.end());
+  const ModelColumns& record = list_[index];
 
-  const ModelColumns& record = it->second;
   model_column::GetColumnData functor(record, column, value);
   boost::mpl::for_each<model_column::List>(functor);
 }
index fb968d0..03ea42b 100644 (file)
@@ -115,8 +115,8 @@ protected:
 
 private:
 
-  typedef boost::unordered_map<model_column::ID::type, ModelColumns> StoreType;
-  typedef std::vector<model_column::ID::type> OrderType;
+  typedef std::vector<ModelColumns> StoreType;
+  typedef boost::unordered_map<model_column::ID::type, size_t> OrderType;
 
   StoreType list_;
   OrderType order_;