BLI_utildefines_iter.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * ***** END GPL LICENSE BLOCK *****
  19. */
  20. #ifndef __BLI_UTILDEFINES_ITER_H__
  21. #define __BLI_UTILDEFINES_ITER_H__
  22. /** \file BLI_utildefines_iter.h
  23. * \ingroup bli
  24. *
  25. * General looping helpers, use `BLI_FOREACH` prefix.
  26. */
  27. /**
  28. * Even value distribution.
  29. *
  30. * \a src must be larger than \a dst,
  31. * \a dst defines the number of iterations, their values are evenly spaced.
  32. *
  33. * The following pairs represent (src, dst) arguments and the values they loop over.
  34. * <pre>
  35. * (19, 4) -> [2, 7, 11. 16]
  36. * (100, 5) -> [9, 29, 49, 69, 89]
  37. * (100, 3) -> [16, 49, 83]
  38. * (100, 100) -> [0..99]
  39. * </pre>
  40. * \note this is mainly useful for numbers that might not divide evenly into eachother.
  41. */
  42. #define BLI_FOREACH_SPARSE_RANGE(src, dst, i) \
  43. for (int _src = (src), _src2 = _src * 2, _dst2 = (dst) * 2, _error = _dst2 - _src, i = 0, _delta; \
  44. ((void)(_delta = divide_floor_i(_error, _dst2)), \
  45. (void)(i -= _delta), \
  46. (i < _src)); \
  47. _error -= (_delta * _dst2) + _src2)
  48. #endif /* __BLI_UTILDEFINES_ITER_H__ */