pointergrid.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef pointergrid_H_INCLUDED
  2. #define pointergrid_H_INCLUDED
  3. #include "mem.h"
  4. /* A PointerGrid is a non-intrusive two dimensional array that can store
  5. * pointers, or alternatively any integer up to intptr_t of size. The pointed-to
  6. * data is not "owned" by the PointerGrid struct, when setting or deleting the
  7. * caller must do the needed cleanup. */
  8. /** Interface of the TMATRIX. */
  9. struct PointerGrid_ {
  10. intptr_t ** data;
  11. int w;
  12. int h;
  13. };
  14. typedef struct PointerGrid_ PointerGrid;
  15. /** Allocates a new, uninitialized TMATRIX. */
  16. PointerGrid * pointergrid_alloc(void);
  17. /** Cleans up and deinitializes the TMATRIX self */
  18. PointerGrid * pointergrid_done(PointerGrid * self, MemDestructor * destroy);
  19. /** Calls PointerGrid_done, and then frees the TMATRIX self */
  20. PointerGrid * pointergrid_free(PointerGrid * self);
  21. /** Returns the width of the TMATRIX. Returns -1 is self isn't propery
  22. * initialized.
  23. */
  24. int pointergrid_w(PointerGrid * self);
  25. /** Returns the height of the TMATRIX.
  26. * Returns -1 if self isn't propery initialized. */
  27. int pointergrid_h(PointerGrid * self);
  28. /** Initializes a TMATRIX to be of the given width and height.
  29. * Returns NULL on error, or if h or w are less than 1.
  30. * Returns self on sucess.
  31. */
  32. PointerGrid * pointergrid_init(PointerGrid * self, int w, int h);
  33. /** Allocates a new matrix and initializes it. */
  34. PointerGrid * pointergrid_new(int w, int h);
  35. /**
  36. * Return true if the arguments x and or y are out of range, or
  37. * if self itself isn't properly initialized. Return false if OK.
  38. */
  39. int pointergrid_outofrange(PointerGrid * self, int w, int h);
  40. /** Gets an element from the matrix. Does no checking whatsoever.
  41. * Use only where the inputs are guaranteed to be valid, where speed is
  42. * of the essence.
  43. */
  44. void * pointergrid_getraw(PointerGrid * self, int x, int y);
  45. /** Gets a row of elements from the matrix. Does no checking whatsoever.
  46. * Use only where the inputs are guaranteed to be valid, where speed is
  47. * of the essence.
  48. */
  49. void ** pointergrid_rowraw(PointerGrid * self, int y);
  50. /** Gets an element from the matrix self.
  51. * If all goes well, this function returns 0, and sets the value to
  52. * (*result).
  53. * If w and h are out of range, or the matrix isn't correctly initialized,
  54. * does nothing and returns -1 in stead.
  55. */
  56. int pointergrid_get(PointerGrid * self, int w, int h, void * * result);
  57. /** Puts an element in the matrix self. Does no checking whatsoever.
  58. * Use only where the inputs are guaranteed to be valid, where speed is
  59. * of the essence.
  60. */
  61. void pointergrid_setraw(PointerGrid * self, int w, int h, void * el);
  62. /** Puts and element info the matrix self.
  63. * If all goes well, this function returns 0.
  64. * If w and h are out of range, or the matrix isn't correctly initialized,
  65. * does nothing and returns -1 in stead.
  66. */
  67. int pointergrid_put(PointerGrid * self, int w, int h, void * el);
  68. void pointergrid_putraw(PointerGrid * self, int x, int y, void * el);
  69. /** Copies the data from one matrix to the other.
  70. * If the matrixes are of unequal size, copies the smallest of both
  71. * heights and widths.
  72. * Returns 0 on succes, -1 on error.
  73. */
  74. int pointergrid_copy(PointerGrid * self, PointerGrid * other);
  75. int pointergrid_nullall(PointerGrid * self, MemDestructor * destroy);
  76. void * pointergrid_store(PointerGrid * self, int x, int y, void * el);
  77. void * pointergrid_fetch(PointerGrid * self, int x, int y);
  78. intptr_t pointergrid_get_raw_int(PointerGrid * self, int x, int y);
  79. intptr_t * pointergrid_row_raw_int(PointerGrid * self, int y);
  80. int pointergrid_get_int(PointerGrid * self, int x, int y, int * result);
  81. void pointergrid_put_raw_int(PointerGrid * self, int x, int y, int el);
  82. int pointergrid_put_int(PointerGrid * self, int x, int y, int el);
  83. int pointergrid_store_int(PointerGrid * self, int x, int y, int el);
  84. int pointergrid_fetch_int(PointerGrid * self, int x, int y);
  85. int pointergrid_zero_all(PointerGrid * self);
  86. #endif