Last active
November 17, 2015 18:27
-
-
Save vladimirgamalyan/f9b11f50c4d0e487cc9e to your computer and use it in GitHub Desktop.
Split <totalSize> by <partSize>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
class PartSlicer | |
{ | |
public: | |
PartSlicer(size_t totalSize, size_t partSize) : totalSize(totalSize), partSize(partSize), offset(0) {} | |
void nextPart() | |
{ | |
if (offset < totalSize) | |
offset += partSize; | |
} | |
size_t getPartIndex() const | |
{ | |
return (offset / partSize); | |
} | |
bool hasData() const | |
{ | |
return (offset < totalSize); | |
} | |
size_t getOffset() const | |
{ | |
return (offset > totalSize) ? totalSize : offset; | |
} | |
size_t getPartSize() const | |
{ | |
size_t result = totalSize - getOffset(); | |
if (result > partSize) | |
result = partSize; | |
return result; | |
} | |
size_t getPartCount() const | |
{ | |
return (totalSize + partSize - 1) / partSize; | |
} | |
size_t getPartLeft() const | |
{ | |
return (getPartCount() - getPartIndex()); | |
} | |
float getProgress() const | |
{ | |
return getOffset() * 100.f / totalSize; | |
} | |
private: | |
size_t totalSize; | |
size_t partSize; | |
size_t offset; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment