summaryrefslogtreecommitdiff
path: root/include/asterisk
diff options
context:
space:
mode:
authorJenkins2 <jenkins2@gerrit.asterisk.org>2017-04-27 15:44:45 -0500
committerGerrit Code Review <gerrit2@gerrit.digium.api>2017-04-27 15:44:45 -0500
commit49b2d1bde5a17363d631d79296fa36a5c40b0628 (patch)
treea7da848d8c3927bc1127f34fe2e620497524807c /include/asterisk
parent413de95ab247ed9b48fc48f440e5c397146e07cb (diff)
parentcf3429b93444293d131cd0c90df285e7644907d9 (diff)
Merge "vector: defaults and indexes"
Diffstat (limited to 'include/asterisk')
-rw-r--r--include/asterisk/vector.h62
1 files changed, 62 insertions, 0 deletions
diff --git a/include/asterisk/vector.h b/include/asterisk/vector.h
index 83732e7c8..2de84d295 100644
--- a/include/asterisk/vector.h
+++ b/include/asterisk/vector.h
@@ -48,6 +48,9 @@
size_t current; \
}
+/*! \brief Integer vector definition */
+AST_VECTOR(ast_vector_int, int);
+
/*!
* \brief Define a vector structure with a read/write lock
*
@@ -241,6 +244,29 @@
})
/*!
+ * \brief Default a vector up to size with the given value.
+ *
+ * \note If a size of 0 is given then all elements in the given vector are set.
+ * \note The vector will grow to the given size if needed.
+ *
+ * \param vec Vector to default.
+ * \param size The number of elements to default
+ * \param value The default value to set each element to
+ */
+#define AST_VECTOR_DEFAULT(vec, size, value) ({ \
+ int res = 0; \
+ typeof((size)) __size = (size) ? (size) : AST_VECTOR_SIZE(vec); \
+ size_t idx; \
+ for (idx = 0; idx < __size; ++idx) { \
+ res = AST_VECTOR_REPLACE(vec, idx, value); \
+ if (res == -1) { \
+ break; \
+ } \
+ } \
+ res; \
+})
+
+/*!
* \brief Insert an element at a specific position in a vector, growing the vector if needed.
*
* \param vec Vector to insert into.
@@ -553,6 +579,42 @@
})
/*!
+ * \brief Get the nth index from a vector that matches the given comparison
+ *
+ * \param vec Vector to get from.
+ * \param nth The nth index to find
+ * \param value Value to pass into comparator.
+ * \param cmp Comparator function/macros (called as \c cmp(elem, value))
+ *
+ * \return a pointer to the element that was found or NULL
+ */
+#define AST_VECTOR_GET_INDEX_NTH(vec, nth, value, cmp) ({ \
+ int res = -1; \
+ size_t idx; \
+ typeof(nth) __nth = (nth); \
+ typeof(value) __value = (value); \
+ for (idx = 0; idx < (vec)->current; ++idx) { \
+ if (cmp((vec)->elems[idx], __value) && !(--__nth)) { \
+ res = (int)idx; \
+ break; \
+ } \
+ } \
+ res; \
+})
+
+/*!
+ * \brief Get the 1st index from a vector that matches the given comparison
+ *
+ * \param vec Vector to get from.
+ * \param value Value to pass into comparator.
+ * \param cmp Comparator function/macros (called as \c cmp(elem, value))
+ *
+ * \return a pointer to the element that was found or NULL
+ */
+#define AST_VECTOR_GET_INDEX(vec, value, cmp) \
+ AST_VECTOR_GET_INDEX_NTH(vec, 1, value, cmp)
+
+/*!
* \brief Get an element from a vector that matches the given comparison
*
* \param vec Vector to get from.