|
| 1 | +/* |
| 2 | + * Copyright 2025 Daniel Cederberg |
| 3 | + * |
| 4 | + * This file is part of the PSLP project (LP Presolver). |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#ifndef VEC_MACROS_H |
| 20 | +#define VEC_MACROS_H |
| 21 | + |
| 22 | +#include <assert.h> |
| 23 | +#include <stdio.h> |
| 24 | +#include <stdlib.h> |
| 25 | +#include <string.h> |
| 26 | + |
| 27 | +// Macro to define a generic vector class |
| 28 | +#define DEFINE_VECTOR(TYPE, TYPE_NAME) \ |
| 29 | + typedef struct TYPE_NAME##Vec \ |
| 30 | + { \ |
| 31 | + TYPE *data; \ |
| 32 | + size_t len; \ |
| 33 | + size_t capacity; \ |
| 34 | + } TYPE_NAME##Vec; \ |
| 35 | + \ |
| 36 | + __attribute__((unused)) static TYPE_NAME##Vec *TYPE_NAME##Vec_new( \ |
| 37 | + size_t capacity) \ |
| 38 | + { \ |
| 39 | + assert(capacity > 0); \ |
| 40 | + TYPE_NAME##Vec *vec = (TYPE_NAME##Vec *) malloc(sizeof(TYPE_NAME##Vec)); \ |
| 41 | + if (vec == NULL) return NULL; \ |
| 42 | + vec->data = (TYPE *) malloc(capacity * sizeof(TYPE)); \ |
| 43 | + if (vec->data == NULL) \ |
| 44 | + { \ |
| 45 | + free(vec); \ |
| 46 | + return NULL; \ |
| 47 | + } \ |
| 48 | + \ |
| 49 | + vec->len = 0; \ |
| 50 | + vec->capacity = capacity; \ |
| 51 | + return vec; \ |
| 52 | + } \ |
| 53 | + \ |
| 54 | + static inline void TYPE_NAME##Vec_free(TYPE_NAME##Vec *vec) \ |
| 55 | + { \ |
| 56 | + free(vec->data); \ |
| 57 | + free(vec); \ |
| 58 | + } \ |
| 59 | + \ |
| 60 | + static inline void TYPE_NAME##Vec_clear_no_resize(TYPE_NAME##Vec *vec) \ |
| 61 | + { \ |
| 62 | + vec->len = 0; \ |
| 63 | + } \ |
| 64 | + \ |
| 65 | + static inline void TYPE_NAME##Vec_append(TYPE_NAME##Vec *vec, TYPE value) \ |
| 66 | + { \ |
| 67 | + if (vec->len >= vec->capacity) \ |
| 68 | + { \ |
| 69 | + vec->capacity *= 2; \ |
| 70 | + assert(vec->capacity > 0); \ |
| 71 | + TYPE *temp = (TYPE *) realloc(vec->data, vec->capacity * sizeof(TYPE)); \ |
| 72 | + if (temp == NULL) \ |
| 73 | + { \ |
| 74 | + TYPE_NAME##Vec_free(vec); \ |
| 75 | + fprintf(stderr, "Error: realloc failed\n"); \ |
| 76 | + exit(1); \ |
| 77 | + } \ |
| 78 | + \ |
| 79 | + vec->data = temp; \ |
| 80 | + } \ |
| 81 | + \ |
| 82 | + vec->data[vec->len++] = value; \ |
| 83 | + } \ |
| 84 | + \ |
| 85 | + static inline void TYPE_NAME##Vec_append_array(TYPE_NAME##Vec *vec, \ |
| 86 | + const TYPE *values, size_t n) \ |
| 87 | + { \ |
| 88 | + if (vec->len + n > vec->capacity) \ |
| 89 | + { \ |
| 90 | + size_t new_capacity = vec->capacity > 0 ? vec->capacity : 1; \ |
| 91 | + while (vec->len + n > new_capacity) \ |
| 92 | + { \ |
| 93 | + new_capacity *= 2; \ |
| 94 | + } \ |
| 95 | + \ |
| 96 | + TYPE *temp = (TYPE *) realloc(vec->data, new_capacity * sizeof(TYPE)); \ |
| 97 | + if (temp == NULL) \ |
| 98 | + { \ |
| 99 | + TYPE_NAME##Vec_free(vec); \ |
| 100 | + fprintf(stderr, "Error: realloc failed\n"); \ |
| 101 | + exit(1); \ |
| 102 | + } \ |
| 103 | + \ |
| 104 | + vec->data = temp; \ |
| 105 | + vec->capacity = new_capacity; \ |
| 106 | + } \ |
| 107 | + \ |
| 108 | + memcpy(vec->data + vec->len, values, n * sizeof(TYPE)); \ |
| 109 | + vec->len += n; \ |
| 110 | + } \ |
| 111 | + __attribute__((unused)) static int TYPE_NAME##Vec_contains( \ |
| 112 | + const TYPE_NAME##Vec *vec, TYPE value) \ |
| 113 | + { \ |
| 114 | + for (size_t i = 0; i < vec->len; ++i) \ |
| 115 | + { \ |
| 116 | + if (vec->data[i] == value) \ |
| 117 | + { \ |
| 118 | + return 1; /* Element found */ \ |
| 119 | + } \ |
| 120 | + } \ |
| 121 | + return 0; /* Element not found */ \ |
| 122 | + } |
| 123 | + |
| 124 | +#endif |
0 commit comments