[asterisk-commits] file: branch group/media_formats r406485 - /team/group/media_formats/include/...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sat Jan 25 09:13:15 CST 2014


Author: file
Date: Sat Jan 25 09:13:09 2014
New Revision: 406485

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=406485
Log:
Extend vector support to allow insertion at a specific position and ordered removals.

Modified:
    team/group/media_formats/include/asterisk/vector.h

Modified: team/group/media_formats/include/asterisk/vector.h
URL: http://svnview.digium.com/svn/asterisk/team/group/media_formats/include/asterisk/vector.h?view=diff&rev=406485&r1=406484&r2=406485
==============================================================================
--- team/group/media_formats/include/asterisk/vector.h (original)
+++ team/group/media_formats/include/asterisk/vector.h Sat Jan 25 09:13:09 2014
@@ -116,6 +116,39 @@
 })
 
 /*!
+ * \brief Insert an element at a specific position in a vector, growing the vector if needed.
+ *
+ * \param vec Vector to insert into.
+ * \param idx Position to insert at.
+ * \param elem Element to insert.
+ *
+ * \return 0 on success.
+ * \return Non-zero on failure.
+ */
+#define AST_VECTOR_INSERT(vec, idx, elem) ({					\
+ 	int res = 0;												\
+ 	do {														\
+ 		if ((idx + 1) > (vec)->max) {							\
+ 			size_t new_max = (vec)->max ? 2 * (vec)->max : 1;	\
+			typeof((vec)->elems) new_elems = ast_realloc(		\
+				(vec)->elems, new_max * sizeof(*new_elems));	\
+			if (new_elems) {									\
+				(vec)->elems = new_elems;						\
+				(vec)->max = new_max;							\
+			} else {											\
+				res = -1;										\
+				break;											\
+			}													\
+ 		}														\
+ 		(vec)->elems[(idx)] = (elem);							\
+ 		if ((idx + 1) > (vec)->current) {						\
+ 			(vec)->current = (idx) + 1;							\
+ 		}														\
+ 	} while(0);													\
+ 	res;														\
+})
+
+/*!
  * \brief Remove an element from a vector by index.
  *
  * Note that elements in the vector may be reordered, so that the remove can
@@ -131,6 +164,25 @@
 	ast_assert(__idx < (vec)->current);			\
 	res = (vec)->elems[__idx];				\
 	(vec)->elems[__idx] = (vec)->elems[--(vec)->current];	\
+	res;							\
+})
+
+/*!
+ * \brief Remove an element from a vector by index while maintaining order.
+ *
+ * \param vec Vector to remove from.
+ * \param idx Index of the element to remove.
+ * \return The element that was removed.
+ */
+#define AST_VECTOR_REMOVE_ORDERED(vec, idx) ({		\
+	typeof((vec)->elems[0]) res;				\
+	size_t __idx = (idx);					\
+	size_t __move;							\
+	ast_assert(__idx < (vec)->current);	\
+	res = (vec)->elems[__idx];				\
+	__move = ((vec)->current - (__idx) - 1) * sizeof(typeof((vec)->elems)); \
+	memmove((vec)->elems[__idx], (vec)->elems[__idx + 1], __move); \
+	(vec)->current--;										\
 	res;							\
 })
 
@@ -162,6 +214,32 @@
 })
 
 /*!
+ * \brief Remove an element from a vector that matches the given comparison while maintaining order
+ *
+ * \param vec Vector to remove from.
+ * \param value Value to pass into comparator.
+ * \param cmp Comparator function/macros (called as \c cmp(elem, value))
+ * \param cleanup How to cleanup a removed element macro/function.
+ *
+ * \return 0 if element was removed.
+ * \return Non-zero if element was not in the vector.
+ */
+#define AST_VECTOR_REMOVE_CMP_ORDERED(vec, value, cmp, cleanup) ({	\
+	int res = -1;							\
+	size_t idx;							\
+	typeof(value) __value = (value);				\
+	for (idx = 0; idx < (vec)->current; ++idx) {			\
+		if (cmp((vec)->elems[idx], __value)) {			\
+			cleanup((vec)->elems[idx]);			\
+			AST_VECTOR_REMOVE_ORDERED((vec), idx);	\
+			res = 0;					\
+			break;						\
+		}							\
+	}								\
+	res;								\
+})
+
+/*!
  * \brief Default comparator for AST_VECTOR_REMOVE_ELEM_UNORDERED()
  *
  * \param elem Element to compare against
@@ -193,6 +271,21 @@
  */
 #define AST_VECTOR_REMOVE_ELEM_UNORDERED(vec, elem, cleanup) ({	\
 	AST_VECTOR_REMOVE_CMP_UNORDERED((vec), (elem),		\
+		AST_VECTOR_ELEM_DEFAULT_CMP, cleanup);		\
+})
+
+/*!
+ * \brief Remove an element from a vector while maintaining order.
+ *
+ * \param vec Vector to remove from.
+ * \param elem Element to remove
+ * \param cleanup How to cleanup a removed element macro/function.
+ *
+ * \return 0 if element was removed.
+ * \return Non-zero if element was not in the vector.
+ */
+#define AST_VECTOR_REMOVE_ELEM_ORDERED(vec, elem, cleanup) ({	\
+	AST_VECTOR_REMOVE_CMP_ORDERED((vec), (elem),		\
 		AST_VECTOR_ELEM_DEFAULT_CMP, cleanup);		\
 })
 




More information about the asterisk-commits mailing list