Browse Source

toString for iterators

Kajetan Johannes Hammerle 1 year ago
parent
commit
dea260b979
6 changed files with 27 additions and 52 deletions
  1. 1 9
      data/Array.h
  2. 1 9
      data/ArrayList.h
  3. 8 14
      data/HashMap.h
  4. 1 11
      data/LinkedList.h
  5. 1 9
      data/List.h
  6. 15 0
      utils/ArrayString.h

+ 1 - 9
data/Array.h

@@ -52,15 +52,7 @@ namespace Core {
 
         template<int L>
         check_return Error toString(ArrayString<L>& s) const {
-            CORE_RETURN_ERROR(s.append("["));
-            for(int i = 0; i < N - 1; i++) {
-                CORE_RETURN_ERROR(s.append(data[i]));
-                CORE_RETURN_ERROR(s.append(", "));
-            }
-            if(N > 0) {
-                CORE_RETURN_ERROR(s.append(data[N - 1]));
-            }
-            return s.append("]");
+            return Core::toString(s, *this);
         }
     };
 }

+ 1 - 9
data/ArrayList.h

@@ -117,15 +117,7 @@ namespace Core {
 
         template<int L>
         check_return Error toString(ArrayString<L>& s) const {
-            CORE_RETURN_ERROR(s.append("["));
-            for(int i = 0; i < length - 1; i++) {
-                CORE_RETURN_ERROR(s.append(begin()[i]));
-                CORE_RETURN_ERROR(s.append(", "));
-            }
-            if(length > 0) {
-                CORE_RETURN_ERROR(s.append(begin()[length - 1]));
-            }
-            return s.append("]");
+            return Core::toString(s, *this);
         }
 
     private:

+ 8 - 14
data/HashMap.h

@@ -22,6 +22,13 @@ namespace Core {
                 return key;
             }
 
+            template<int L>
+            check_return Error toString(ArrayString<L>& s) const {
+                CORE_RETURN_ERROR(s.append(key));
+                CORE_RETURN_ERROR(s.append(" = "));
+                return s.append(value);
+            }
+
         private:
             template<typename... Args>
             Node(const K& key_, Args&&... args)
@@ -256,20 +263,7 @@ namespace Core {
 
         template<int L>
         check_return Error toString(ArrayString<L>& s) const {
-            CORE_RETURN_ERROR(s.append("["))
-            bool c = false;
-            for(const NodePointerList& list : nodePointers) {
-                for(const NodePointer& n : list) {
-                    if(c) {
-                        CORE_RETURN_ERROR(s.append(", "))
-                    }
-                    CORE_RETURN_ERROR(s.append(n->data.key))
-                    CORE_RETURN_ERROR(s.append(" = "))
-                    CORE_RETURN_ERROR(s.append(n->data.value))
-                    c = true;
-                }
-            }
-            return s.append("]");
+            return Core::toString(s, *this);
         }
 
     private:

+ 1 - 11
data/LinkedList.h

@@ -137,17 +137,7 @@ namespace Core {
 
         template<int L>
         check_return Error toString(ArrayString<L>& s) const {
-            CORE_RETURN_ERROR(s.append("["));
-            auto iter = begin();
-            for(int i = 0; i < length - 1; i++) {
-                CORE_RETURN_ERROR(s.append(*iter));
-                CORE_RETURN_ERROR(s.append(", "));
-                ++iter;
-            }
-            if(length > 0) {
-                CORE_RETURN_ERROR(s.append(*iter));
-            }
-            return s.append("]");
+            return Core::toString(s, *this);
         }
 
         void remove(Node*& n) {

+ 1 - 9
data/List.h

@@ -183,15 +183,7 @@ namespace Core {
 
         template<int L>
         check_return Error toString(ArrayString<L>& s) const {
-            CORE_RETURN_ERROR(s.append("["));
-            for(int i = 0; i < length - 1; i++) {
-                CORE_RETURN_ERROR(s.append(data[i]));
-                CORE_RETURN_ERROR(s.append(", "));
-            }
-            if(length > 0) {
-                CORE_RETURN_ERROR(s.append(data[length - 1]));
-            }
-            return s.append("]");
+            return Core::toString(s, *this);
         }
 
     private:

+ 15 - 0
utils/ArrayString.h

@@ -385,6 +385,21 @@ namespace Core {
             return append(static_cast<const char*>(buffer));
         }
     };
+
+    template<int L, typename Iterable>
+    check_return Error toString(ArrayString<L>& s, const Iterable& i) {
+        CORE_RETURN_ERROR(s.append("["));
+        auto current = i.begin();
+        auto end = i.end();
+        while(current != end) {
+            CORE_RETURN_ERROR(s.append(*current));
+            ++current;
+            if(current != end) {
+                CORE_RETURN_ERROR(s.append(", "));
+            }
+        }
+        return s.append("]");
+    }
 }
 
 template<int N>