[Groonga-commit] groonga/grnxx at 4c217bc [master] Add reserve_with_same_value_size() to reduce redundant code. (#130)

Back to archive index

susumu.yata null+****@clear*****
Fri Dec 26 18:00:26 JST 2014


susumu.yata	2014-12-26 18:00:26 +0900 (Fri, 26 Dec 2014)

  New Revision: 4c217bc5913e83d4057c59ee9dd9e952d5f81013
  https://github.com/groonga/grnxx/commit/4c217bc5913e83d4057c59ee9dd9e952d5f81013

  Message:
    Add reserve_with_same_value_size() to reduce redundant code. (#130)

  Modified files:
    lib/grnxx/impl/column/scalar/int.cpp
    lib/grnxx/impl/column/scalar/int.hpp

  Modified: lib/grnxx/impl/column/scalar/int.cpp (+52 -74)
===================================================================
--- lib/grnxx/impl/column/scalar/int.cpp    2014-12-26 17:23:47 +0900 (a0b4cdf)
+++ lib/grnxx/impl/column/scalar/int.cpp    2014-12-26 18:00:26 +0900 (318bf4b)
@@ -421,25 +421,7 @@ void Column<Int>::reserve(size_t size, Int value) {
       case 8: {
         if ((raw >= min_value_8()) && (raw <= max_value_8())) {
           // 8 -> 8.
-          if (size > capacity_) {
-            size_t new_capacity = (capacity_ != 0) ? capacity_ : 1;
-            while (new_capacity < size) {
-              new_capacity *= 2;
-            }
-            void *new_buffer =
-                std::realloc(buffer_, sizeof(int8_t) * new_capacity);
-            if (!new_buffer) {
-              throw "Memory allocation failed";
-            }
-            buffer_ = new_buffer;
-            capacity_ = new_capacity;
-          }
-          if (size > size_) {
-            for (size_t i = size_; i < size; ++i) {
-              values_8_[i] = na_value_8();
-            }
-            size_ = size;
-          }
+          reserve_with_same_value_size(size);
         } else if ((raw >= min_value_16()) && (raw <= max_value_16())) {
           // 8 -> 16.
           size_t new_capacity = (capacity_ != 0) ? capacity_ : 1;
@@ -521,25 +503,7 @@ void Column<Int>::reserve(size_t size, Int value) {
       case 16: {
         if ((raw >= min_value_16()) && (raw <= max_value_16())) {
           // 16 -> 16.
-          if (size > capacity_) {
-            size_t new_capacity = (capacity_ != 0) ? capacity_ : 1;
-            while (new_capacity < size) {
-              new_capacity *= 2;
-            }
-            void *new_buffer =
-                std::realloc(buffer_, sizeof(int16_t) * new_capacity);
-            if (!new_buffer) {
-              throw "Memory allocation failed";
-            }
-            buffer_ = new_buffer;
-            capacity_ = new_capacity;
-          }
-          if (size > size_) {
-            for (size_t i = size_; i < size; ++i) {
-              values_16_[i] = na_value_16();
-            }
-            size_ = size;
-          }
+          reserve_with_same_value_size(size);
         } else if ((raw >= min_value_32()) && (raw <= max_value_32())) {
           // 16 -> 32.
           size_t new_capacity = (capacity_ != 0) ? capacity_ : 1;
@@ -596,25 +560,7 @@ void Column<Int>::reserve(size_t size, Int value) {
       case 32: {
         if ((raw >= min_value_32()) && (raw <= max_value_32())) {
           // 32 -> 32.
-          if (size > capacity_) {
-            size_t new_capacity = (capacity_ != 0) ? capacity_ : 1;
-            while (new_capacity < size) {
-              new_capacity *= 2;
-            }
-            void *new_buffer =
-                std::realloc(buffer_, sizeof(int32_t) * new_capacity);
-            if (!new_buffer) {
-              throw "Memory allocation failed";
-            }
-            buffer_ = new_buffer;
-            capacity_ = new_capacity;
-          }
-          if (size > size_) {
-            for (size_t i = size_; i < size; ++i) {
-              values_32_[i] = na_value_32();
-            }
-            size_ = size;
-          }
+          reserve_with_same_value_size(size);
         } else {
           // 32 -> 64.
           size_t new_capacity = (capacity_ != 0) ? capacity_ : 1;
@@ -645,31 +591,63 @@ void Column<Int>::reserve(size_t size, Int value) {
       }
       default: {
         // 64 -> 64.
-        if (size > capacity_) {
-          size_t new_capacity = (capacity_ != 0) ? capacity_ : 1;
-          while (new_capacity < size) {
-            new_capacity *= 2;
-          }
-          void *new_buffer =
-              std::realloc(buffer_, sizeof(Int) * new_capacity);
-          if (!new_buffer) {
-            throw "Memory allocation failed";
-          }
-          buffer_ = new_buffer;
-          capacity_ = new_capacity;
+        reserve_with_same_value_size(size);
+        break;
+      }
+    }
+  }
+}
+
+void Column<Int>::reserve_with_same_value_size(size_t size) {
+  if (size > capacity_) {
+    size_t new_capacity = (capacity_ != 0) ? capacity_ : 1;
+    while (new_capacity < size) {
+      new_capacity *= 2;
+    }
+    void *new_buffer =
+        std::realloc(buffer_, (value_size_ / 8) * new_capacity);
+    if (!new_buffer) {
+      throw "Memory allocation failed";
+    }
+    buffer_ = new_buffer;
+    capacity_ = new_capacity;
+  }
+  if (size > size_) {
+    switch (value_size_) {
+      case 8: {
+        for (size_t i = size_; i < size; ++i) {
+          values_8_[i] = na_value_8();
         }
-        if (size > size_) {
-          for (size_t i = size_; i < size; ++i) {
-            values_64_[i] = Int::na();
-          }
-          size_ = size;
+        break;
+      }
+      case 16: {
+        for (size_t i = size_; i < size; ++i) {
+          values_16_[i] = na_value_16();
+        }
+        break;
+      }
+      case 32: {
+        for (size_t i = size_; i < size; ++i) {
+          values_32_[i] = na_value_32();
+        }
+        break;
+      }
+      default: {
+        for (size_t i = size_; i < size; ++i) {
+          values_64_[i] = Int::na();
         }
         break;
       }
     }
+    size_ = size;
   }
 }
 
+void Column<Int>::reserve_with_different_value_size(size_t size,
+                                                    size_t value_size) {
+  // TODO
+}
+
 Int Column<Int>::parse_datum(const Datum &datum) {
   switch (datum.type()) {
     case NA_DATA: {

  Modified: lib/grnxx/impl/column/scalar/int.hpp (+2 -0)
===================================================================
--- lib/grnxx/impl/column/scalar/int.hpp    2014-12-26 17:23:47 +0900 (e021e78)
+++ lib/grnxx/impl/column/scalar/int.hpp    2014-12-26 18:00:26 +0900 (9aaaa9b)
@@ -93,6 +93,8 @@ class Column<Int> : public ColumnBase {
 
   // Reserve memory for a new value.
   void reserve(size_t size, Int value);
+  void reserve_with_same_value_size(size_t size);
+  void reserve_with_different_value_size(size_t size, size_t value_size);
 
   // Parse "datum" as Int.
   //
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index