Skip to content

Instantly share code, notes, and snippets.

@vladimirgamalyan
Last active November 17, 2015 18:27
Show Gist options
  • Save vladimirgamalyan/f9b11f50c4d0e487cc9e to your computer and use it in GitHub Desktop.
Save vladimirgamalyan/f9b11f50c4d0e487cc9e to your computer and use it in GitHub Desktop.
Split <totalSize> by <partSize>
#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