vector.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. (c) Copyright 2001-2009 The world wide DirectFB Open Source Community (directfb.org)
  3. (c) Copyright 2000-2004 Convergence (integrated media) GmbH
  4. All rights reserved.
  5. Written by Denis Oliver Kropp <dok@directfb.org>,
  6. Andreas Hundt <andi@fischlustig.de>,
  7. Sven Neumann <neo@directfb.org>,
  8. Ville Syrjälä <syrjala@sci.fi> and
  9. Claudio Ciccani <klan@users.sf.net>.
  10. This library is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU Lesser General Public
  12. License as published by the Free Software Foundation; either
  13. version 2 of the License, or (at your option) any later version.
  14. This library is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with this library; if not, write to the
  20. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. Boston, MA 02111-1307, USA.
  22. */
  23. #ifndef __FUSION__VECTOR_H__
  24. #define __FUSION__VECTOR_H__
  25. #include <limits.h>
  26. #include <fusion/types.h>
  27. #include <direct/debug.h>
  28. typedef struct {
  29. int magic;
  30. void **elements;
  31. int count;
  32. int capacity;
  33. FusionSHMPoolShared *pool;
  34. } FusionVector;
  35. void fusion_vector_init ( FusionVector *vector,
  36. int capacity,
  37. FusionSHMPoolShared *pool );
  38. void fusion_vector_destroy ( FusionVector *vector );
  39. DirectResult fusion_vector_add ( FusionVector *vector,
  40. void *element );
  41. DirectResult fusion_vector_insert ( FusionVector *vector,
  42. void *element,
  43. int index );
  44. DirectResult fusion_vector_move ( FusionVector *vector,
  45. int from,
  46. int to );
  47. DirectResult fusion_vector_remove ( FusionVector *vector,
  48. int index );
  49. DirectResult fusion_vector_remove_last( FusionVector *vector );
  50. static inline bool
  51. fusion_vector_has_elements( const FusionVector *vector )
  52. {
  53. D_MAGIC_ASSERT( vector, FusionVector );
  54. return vector->count > 0;
  55. }
  56. static inline bool
  57. fusion_vector_is_empty( const FusionVector *vector )
  58. {
  59. D_MAGIC_ASSERT( vector, FusionVector );
  60. return vector->count == 0;
  61. }
  62. static inline int
  63. fusion_vector_size( const FusionVector *vector )
  64. {
  65. D_MAGIC_ASSERT( vector, FusionVector );
  66. return vector->count;
  67. }
  68. static inline void *
  69. fusion_vector_at( const FusionVector *vector, int index )
  70. {
  71. D_MAGIC_ASSERT( vector, FusionVector );
  72. D_ASSERT( index >= 0 );
  73. D_ASSERT( index < vector->count );
  74. return vector->elements[index];
  75. }
  76. static inline bool
  77. fusion_vector_contains( const FusionVector *vector, const void *element )
  78. {
  79. int i;
  80. int count;
  81. void * const *elements;
  82. D_MAGIC_ASSERT( vector, FusionVector );
  83. D_ASSERT( element != NULL );
  84. count = vector->count;
  85. elements = vector->elements;
  86. /* Start with more recently added elements. */
  87. for (i=count-1; i>=0; i--)
  88. if (elements[i] == element)
  89. return true;
  90. return false;
  91. }
  92. static inline int
  93. fusion_vector_index_of( const FusionVector *vector, const void *element )
  94. {
  95. int i;
  96. int count;
  97. void * const *elements;
  98. D_MAGIC_ASSERT( vector, FusionVector );
  99. D_ASSERT( element != NULL );
  100. count = vector->count;
  101. elements = vector->elements;
  102. /* Start with more recently added elements. */
  103. for (i=count-1; i>=0; i--)
  104. if (elements[i] == element)
  105. return i;
  106. /*
  107. * In case the return value isn't checked
  108. * this will most likely generate a bad address.
  109. */
  110. return INT_MIN >> 2;
  111. }
  112. #define fusion_vector_foreach(element, index, vector) \
  113. for ((index) = 0; \
  114. (index) < (vector).count && (element = (vector).elements[index]); \
  115. (index)++)
  116. #define fusion_vector_foreach_reverse(element, index, vector) \
  117. for ((index) = (vector).count - 1; \
  118. (index) >= 0 && (vector).count && \
  119. (vector).elements && \
  120. (element = (vector).elements[index]); \
  121. (index)--)
  122. #endif