Browse Source

Added range implementation

Luiserebii 10 months ago
parent
commit
3143925f59
Signed by: Luiserebii <luis@serebii.io> GPG Key ID: 58A745E0A5422230
2 changed files with 19 additions and 8 deletions
  1. 4
    0
      Makefile
  2. 15
    8
      include/vector.h

+ 4
- 0
Makefile View File

@@ -1,3 +1,7 @@
1 1
 .PHONY: lint
2 2
 lint:
3 3
 	./scripts/lint.sh
4
+
5
+.PHONY: test
6
+test: 
7
+	cd test && make test

+ 15
- 8
include/vector.h View File

@@ -157,7 +157,7 @@
157 157
      * For complex data type vectors, this is more space-efficient than the copy                         \
158 158
      * performed by insert.                                                                              \
159 159
      */                                                                                                  \
160
-    void prefix##insert_r##suffix(struct_name* v, vector_type* p, const vector_type* val);                               \
160
+    void prefix##insert_r##suffix(struct_name* v, vector_type* p, const vector_type* v);                               \
161 161
                                                                                                          \
162 162
     /*                                                                                                   \
163 163
      * Inserts the range of values in [first, last), before the pointer passed.                          \
@@ -344,15 +344,22 @@
344 344
         } \
345 345
         *(it + 1) = *it; \
346 346
         *p = *val; \
347
-    }
347
+    } \
348 348
                                                                                                          \
349
-    /*                                                                                                   \
350
-     * Inserts the range of values in [first, last), before the pointer passed.                          \
351
-     */                                                                                                  \
352
-    void prefix##insert_range##suffix(struct_name* v, vector_type* p, const vector_type* begin, const vector_type* end); \
349
+    void prefix##insert_range##suffix(struct_name* v, vector_type* p, const vector_type* begin, const vector_type* end) { \
350
+        if(avail == tail) { \
351
+            prefix##grow##suffix(v, prefix##capacity##suffix(v) + 1); \
352
+        } \
353
+        vector_type* it = a + (prefix##size##suffix(v) - 1); \
354
+        size_t sz = end - begin; \
355
+        for(; it > p; --it) { \
356
+            *(it + sz) = *it; \
357
+        } \
358
+        *(it + sz) = *it; \
359
+        /* Copy [begin, end) into [p, p + sz) */ \
360
+        algorithm_min_copy(vector_type*, begin, end, p); \
361
+    }    \
353 362
                                                                                                      \
354
-
355
-
356 363
     void prefix##pop_back##suffix(struct_name* v) {                                                  \
357 364
         assert(prefix##size##suffix(v) != 0);                                                        \
358 365
         --v->avail;                                                                                  \

Loading…
Cancel
Save