Guest User

cvector.h

a guest
Oct 31st, 2019
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.84 KB | None | 0 0
  1. #ifndef _CVECTOR_H_
  2. #define _CVECTOR_H_
  3.  
  4. /*
  5.   cvector: C vector implementation, almost a copy of the STL vector itself but
  6.   with some small changes with how the operator overloaded operations are done.
  7.  
  8.   Please read notes near the bottom of this file for directions/use of regarding
  9.   operator overloaded operations, there is some things that ain't exacly like
  10.   the STL vector, some things are done in other ways (like comparing contents
  11.   of two different vectors) etc.
  12.  
  13.   Author: xyz (heulanith @ bitbucket).
  14. */
  15.  
  16. #define CVECTOR_16BIT_INT // DEFAULT :>
  17. #ifdef CVECTOR_16BIT_INT
  18. #define CVECTOR_UINT unsigned short
  19. #else
  20. #define CVECTOR_UINT unsigned int
  21. #endif
  22.  
  23. #define CVECTOR_VOID void
  24.  
  25. // config-bits
  26. #define CVECTOR_DYN_ALLOC 1
  27.  
  28. /* CVECTOR SPECIFIC */
  29. struct t_cvector* cvector_alloc(struct t_cvector *pvector,
  30.                                 CVECTOR_UINT element_size, CVECTOR_UINT capacity);
  31. void cvector_free(struct t_cvector *v);
  32.  
  33. /* ITERATORS */
  34. CVECTOR_VOID* cvector_begin(struct t_cvector *v);
  35. CVECTOR_VOID* cvector_end(struct t_cvector *v);
  36.  
  37. /* CAPACITY */
  38. //CVECTOR_UINT cvector_grow_capacity(struct t_cvector *v, CVECTOR_UINT n);
  39. CVECTOR_UINT cvector_size(struct t_cvector *v);
  40. CVECTOR_UINT cvector_capacity(struct t_cvector *v);
  41. CVECTOR_UINT cvector_empty(struct t_cvector *v);
  42. CVECTOR_UINT cvector_resize(struct t_cvector *v, CVECTOR_UINT n, CVECTOR_VOID* data);
  43. CVECTOR_UINT cvector_reserve(struct t_cvector *v, CVECTOR_UINT n);
  44.  
  45. /* ELEMENT ACCESS */
  46. CVECTOR_VOID* cvector_at(struct t_cvector *v, CVECTOR_UINT i);
  47. CVECTOR_VOID* cvector_front(struct t_cvector *v);
  48. CVECTOR_VOID* cvector_back(struct t_cvector *v);
  49.  
  50. /* MODIFIERS */
  51. CVECTOR_UINT cvector_assign(struct t_cvector *d, struct t_cvector *s);
  52. //fill, copy, range
  53. CVECTOR_UINT cvector_push_back(struct t_cvector *v, CVECTOR_VOID *data);
  54. CVECTOR_UINT cvector_pop_back(struct t_cvector *v);
  55. CVECTOR_UINT cvector_insert(struct t_cvector *v, CVECTOR_UINT i, CVECTOR_VOID *data);
  56. CVECTOR_UINT cvector_erase(struct t_cvector *v, CVECTOR_UINT i);
  57. CVECTOR_UINT cvector_swap(struct t_cvector *v, CVECTOR_UINT a, CVECTOR_UINT b);
  58. void cvector_clear(struct t_cvector *v);
  59.  
  60. /* vector container structure */
  61. struct t_cvector {
  62.  CVECTOR_UINT elements;
  63.  CVECTOR_UINT capacity;
  64.  CVECTOR_UINT element_size;
  65.  CVECTOR_VOID *data;
  66.  CVECTOR_VOID **iterators;
  67.  
  68.  /* used in cvector_free(...) to know wether the cvector structure was dynamically
  69.     allocated by malloc() in cvector_alloc() OR if it was referred to from already
  70.     allocated memory of size (sizeof(struct t_cvector)) */
  71.  unsigned char conf_bits;
  72.  
  73.  /* OPERATOR OVERLOADING FOR EASE OF USE
  74.    
  75.     Explanations of all operator operations comes below:
  76.     First off we got the vector-to-vector operations, those does NOT compare
  77.     the elements in the vector as the STL vector does - instead they compare the
  78.     size (number of elements) of the vectors. This is because we can never know
  79.     the type of data that is stored in the vector without templates (which use
  80.     alot of space on harddrive) or ugly hacks. Instead it is entirely logic
  81.     to write those operator overloaded operations in the structure the data
  82.     that is stored in the vector belongs to in the first place - then it is
  83.     easy to use the vector's [] overloaded operator to compare to elements of
  84.     the same type with each other, that kind of stuff doesn't belong in this
  85.     code anyway as all sorts of structures or datatypes should be treated
  86.     individually when those operators are used.
  87.    
  88.     Otherwise the [] and = operators all got the same function as the
  89.     STL vector's counterparts.
  90.  */
  91.  
  92.  /*struct t_cvector operator =(struct t_cvector r) {
  93.   cvector_clear(this);
  94.   cvector_assign(this, &r);
  95.   //return a;
  96.  }*/
  97.  
  98.  /*CVECTOR_VOID* operator [](CVECTOR_UINT i) {
  99.   return cvector_at(this, i);
  100.  }*/
  101. };
  102.  
  103. #endif
Add Comment
Please, Sign In to add comment