ccChunk.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //##########################################################################
  2. //# #
  3. //# CLOUDCOMPARE #
  4. //# #
  5. //# This program is free software; you can redistribute it and/or modify #
  6. //# it under the terms of the GNU General Public License as published by #
  7. //# the Free Software Foundation; version 2 or later of the License. #
  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. //# COPYRIGHT: Daniel Girardeau-Montaut #
  15. //# #
  16. //##########################################################################
  17. #ifndef CC_CHUNK_HEADER
  18. #define CC_CHUNK_HEADER
  19. //System
  20. #include <vector>
  21. //! Fake chunked array management
  22. class ccChunk
  23. {
  24. public:
  25. static const size_t SIZE_POWER = 16;
  26. static const size_t SIZE = (1 << SIZE_POWER); //~ 64K
  27. inline static size_t Count(size_t elementCount) { return (elementCount >> SIZE_POWER) + ((elementCount & (SIZE - 1)) ? 1 : 0); }
  28. inline static size_t Size(size_t chunkIndex, size_t elementCount) { return (chunkIndex + 1 < Count(elementCount) ? SIZE : elementCount - chunkIndex * SIZE); }
  29. inline static size_t Size(size_t chunkIndex, size_t chunkCount, size_t elementCount) { return (chunkIndex + 1 < chunkCount ? SIZE : elementCount - chunkIndex * SIZE); }
  30. inline static size_t StartPos(size_t chunkIndex) { return chunkIndex * SIZE; }
  31. template<typename T> inline static T* Start(std::vector<T>& buffer, size_t chunkIndex) { return buffer.data() + StartPos(chunkIndex); }
  32. template<typename T> inline static const T* Start(const std::vector<T>& buffer, size_t chunkIndex) { return buffer.data() + StartPos(chunkIndex); }
  33. template<typename T> inline static size_t Count(const std::vector<T>& buffer) { return Count(buffer.size()); }
  34. template<typename T> inline static size_t Size(size_t chunkIndex, const std::vector<T>& buffer) { return Size(chunkIndex, buffer.size()); }
  35. };
  36. #endif //CC_CHUNK_HEADER