[Groonga-commit] groonga/grnxx at 69132f9 [master] Add Array.

Back to archive index

susumu.yata null+****@clear*****
Fri Jul 25 17:40:12 JST 2014


susumu.yata	2014-07-25 17:40:12 +0900 (Fri, 25 Jul 2014)

  New Revision: 69132f9c56bebe4b7ec052f663e00338d9ce95b4
  https://github.com/groonga/grnxx/commit/69132f9c56bebe4b7ec052f663e00338d9ce95b4

  Message:
    Add Array.

  Added files:
    include/grnxx/array.hpp
    lib/grnxx/array.cpp
  Modified files:
    include/grnxx/Makefile.am
    include/grnxx/error.hpp
    lib/grnxx/Makefile.am

  Modified: include/grnxx/Makefile.am (+1 -0)
===================================================================
--- include/grnxx/Makefile.am    2014-07-24 17:59:20 +0900 (86ddd8e)
+++ include/grnxx/Makefile.am    2014-07-25 17:40:12 +0900 (0d46479)
@@ -1,4 +1,5 @@
 pkginclude_HEADERS =	\
+	array.hpp	\
 	column.hpp	\
 	cursor.hpp	\
 	datum.hpp	\

  Added: include/grnxx/array.hpp (+127 -0) 100644
===================================================================
--- /dev/null
+++ include/grnxx/array.hpp    2014-07-25 17:40:12 +0900 (7d72f75)
@@ -0,0 +1,127 @@
+#ifndef GRNXX_ARRAY_HPP
+#define GRNXX_ARRAY_HPP
+
+#include <vector>
+
+#include "grnxx/types.hpp"
+
+namespace grnxx {
+
+class ArrayHelper {
+ public:
+  static void report_memory_error(Error *error);
+  static void report_empty_error(Error *error);
+};
+
+template <typename T>
+class Array {
+ public:
+  Array() : values_() {}
+  ~Array() {}
+
+  T &operator[](Int i) {
+    return values_[static_cast<size_t>(i)];
+  }
+  const T &operator[](Int i) const {
+    return values_[static_cast<size_t>(i)];
+  }
+
+  T &front() {
+    return values_.front();
+  }
+  const T &front() const {
+    return values_.front();
+  }
+
+  T &back() {
+    return values_.back();
+  }
+  const T &back() const {
+    return values_.back();
+  }
+
+  T *data() {
+    return values_.data();
+  }
+  const T *data() const {
+    return values_.data();
+  }
+
+  Int size() const {
+    return static_cast<Int>(values_.size());
+  }
+  Int capacity() const {
+    return static_cast<Int>(values_.capacity());
+  }
+
+  bool reserve(Error *error, Int new_size) try {
+    values_.reserve(new_size);
+    return true;
+  } catch (...) {
+    ArrayHelper::report_memory_error(error);
+    return false;
+  }
+
+  bool resize(Error *error, Int new_size) try {
+    values_.resize(new_size);
+    return true;
+  } catch (...) {
+    ArrayHelper::report_memory_error(error);
+    return false;
+  }
+  bool resize(Error *error, Int new_size, const T &value) try {
+    values_.resize(new_size, value);
+    return true;
+  } catch (...) {
+    ArrayHelper::report_memory_error(error);
+    return false;
+  }
+
+  bool shrink_to_fit(Error *error) try {
+    values_.shrink_to_fit();
+    return true;
+  } catch (...) {
+    ArrayHelper::report_memory_error(error);
+    return false;
+  }
+
+  void clear() {
+    values_.clear();
+  }
+
+  void erase(Int i) {
+    values_.erase(values_.begin() + i);
+  }
+
+  bool push_back(Error *error, const T &value) try {
+    values_.push_back(value);
+    return true;
+  } catch (...) {
+    ArrayHelper::report_memory_error(error);
+    return false;
+  }
+  bool push_back(Error *error, T &&value) try {
+    values_.push_back(std::move(value));
+    return true;
+  } catch (...) {
+    ArrayHelper::report_memory_error(error);
+    return false;
+  }
+  bool pop_back(Error *error) {
+    if (values_.size() == 0) {
+      ArrayHelper::report_empty_error(error);
+      return false;
+    }
+    values_.pop_back();
+    return true;
+  }
+
+ private:
+  std::vector<T> values_;
+};
+
+template class Array<Int>;
+
+}  // namespace grnxx
+
+#endif  // GRNXX_ARRAY_HPP

  Modified: include/grnxx/error.hpp (+11 -10)
===================================================================
--- include/grnxx/error.hpp    2014-07-24 17:59:20 +0900 (bedc58e)
+++ include/grnxx/error.hpp    2014-07-25 17:40:12 +0900 (55d147f)
@@ -6,16 +6,17 @@
 namespace grnxx {
 
 enum ErrorCode {
-  NO_ERROR,          // No error occurred.
-  NOT_FOUND,         // The target does not found.
-  ALREADY_EXISTS,    // The target already exists.
-  NOT_REMOVABLE,     // The target is not removable.
-  BROKEN,            // The database is broken.
-  NO_MEMORY,         // Memory allocation failed.
-  INVALID_NAME,      // The string is invalid as an object name.
-  NO_KEY_COLUMN,     // The table has no key column.
-  INVALID_ARGUMENT,  // Invalid argument.
-  NOT_SUPPORTED_YET  // The operation is not supported yet.
+  NO_ERROR,           // No error occurred.
+  NOT_FOUND,          // The target does not found.
+  ALREADY_EXISTS,     // The target already exists.
+  NOT_REMOVABLE,      // The target is not removable.
+  BROKEN,             // The database is broken.
+  NO_MEMORY,          // Memory allocation failed.
+  INVALID_NAME,       // The string is invalid as an object name.
+  NO_KEY_COLUMN,      // The table has no key column.
+  INVALID_ARGUMENT,   // Invalid argument.
+  INVALID_OPERATION,  // Invalid operation.
+  NOT_SUPPORTED_YET   // The operation is not supported yet.
 };
 
 // Many functions take a pointer to an Error object as the first argument

  Modified: lib/grnxx/Makefile.am (+1 -0)
===================================================================
--- lib/grnxx/Makefile.am    2014-07-24 17:59:20 +0900 (1d83fbc)
+++ lib/grnxx/Makefile.am    2014-07-25 17:40:12 +0900 (94e9584)
@@ -9,6 +9,7 @@ lib_LTLIBRARIES = libgrnxx.la
 libgrnxx_la_LDFLAGS = @AM_LTLDFLAGS@
 
 libgrnxx_la_SOURCES =			\
+	array.cpp			\
 	column.cpp			\
 	db.cpp				\
 	error.cpp			\

  Added: lib/grnxx/array.cpp (+15 -0) 100644
===================================================================
--- /dev/null
+++ lib/grnxx/array.cpp    2014-07-25 17:40:12 +0900 (7aca8df)
@@ -0,0 +1,15 @@
+#include "grnxx/array.hpp"
+
+#include "grnxx/error.hpp"
+
+namespace grnxx {
+
+void ArrayHelper::report_memory_error(Error *error) {
+  GRNXX_ERROR_SET(error, NO_MEMORY, "Memory allocation failed");
+}
+
+void ArrayHelper::report_empty_error(Error *error) {
+  GRNXX_ERROR_SET(error, INVALID_OPERATION, "Empty vector");
+}
+
+}  // namespace grnxx
-------------- next part --------------
HTML����������������������������...
Download 



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