common/array: code refactoring.

Use a default constructor with reservation.
This commit is contained in:
Hermet Park 2023-12-12 21:05:08 +09:00 committed by Hermet Park
parent 528ea0d587
commit 0aa39111ad
7 changed files with 12 additions and 16 deletions

View file

@ -38,6 +38,11 @@ struct Array
Array(){} Array(){}
Array(int32_t size)
{
reserve(size);
}
Array(const Array& rhs) Array(const Array& rhs)
{ {
reset(); reset();

View file

@ -329,8 +329,7 @@ static void _repeat(LottieGroup* parent, unique_ptr<Shape> path, RenderContext*
{ {
auto repeater = ctx->repeater; auto repeater = ctx->repeater;
Array<Shape*> shapes; Array<Shape*> shapes(repeater->cnt);
shapes.reserve(repeater->cnt);
for (int i = 0; i < repeater->cnt; ++i) { for (int i = 0; i < repeater->cnt; ++i) {
auto multiplier = repeater->offset + static_cast<float>(i); auto multiplier = repeater->offset + static_cast<float>(i);

View file

@ -85,13 +85,10 @@ struct LottieGradient
uint32_t populate(ColorStop& color) uint32_t populate(ColorStop& color)
{ {
uint32_t alphaCnt = (color.input->count - (colorStops.count * 4)) / 2; uint32_t alphaCnt = (color.input->count - (colorStops.count * 4)) / 2;
Array<Fill::ColorStop> output; Array<Fill::ColorStop> output(colorStops.count + alphaCnt);
output.reserve(colorStops.count + alphaCnt);
uint32_t cidx = 0; //color count uint32_t cidx = 0; //color count
uint32_t clast = colorStops.count * 4; uint32_t clast = colorStops.count * 4;
uint32_t aidx = clast; //alpha count uint32_t aidx = clast; //alpha count
Fill::ColorStop cs; Fill::ColorStop cs;
//merge color stops. //merge color stops.

View file

@ -237,8 +237,7 @@ void LottieParser::getValue(ColorStop& color)
{ {
if (peekType() == kArrayType) enterArray(); if (peekType() == kArrayType) enterArray();
color.input = new Array<float>; color.input = new Array<float>(context->gradient->colorStops.count);
color.input->reserve(context->gradient->colorStops.count);
while (nextArrayValue()) color.input->push(getFloat()); while (nextArrayValue()) color.input->push(getFloat());
} }

View file

@ -622,8 +622,7 @@ GlRenderPass* GlRenderer::currentPass()
void GlRenderer::prepareCmpTask(GlRenderTask* task, float opacity) void GlRenderer::prepareCmpTask(GlRenderTask* task, float opacity)
{ {
// we use 1:1 blit mapping since compositor fbo is same size as root fbo // we use 1:1 blit mapping since compositor fbo is same size as root fbo
Array<float> vertices; Array<float> vertices(5 * 4);
vertices.reserve(5 * 4);
float left = -1.f; float left = -1.f;
float top = 1.f; float top = 1.f;
@ -655,8 +654,7 @@ void GlRenderer::prepareCmpTask(GlRenderTask* task, float opacity)
vertices.push(1.f); vertices.push(1.f);
vertices.push(0.f); vertices.push(0.f);
Array<uint32_t> indices; Array<uint32_t> indices(6);
indices.reserve(6);
indices.push(0); indices.push(0);
indices.push(1); indices.push(1);

View file

@ -123,8 +123,7 @@ struct Scene::Impl
this->renderer = &renderer; this->renderer = &renderer;
if (clipper) { if (clipper) {
Array<RenderData> rds; Array<RenderData> rds(paints.size());
rds.reserve(paints.size());
for (auto paint : paints) { for (auto paint : paints) {
rds.push(paint->pImpl->update(renderer, transform, clips, opacity, flag, true)); rds.push(paint->pImpl->update(renderer, transform, clips, opacity, flag, true));
} }

View file

@ -683,8 +683,7 @@ TvgBinCounter TvgSaver::serializeChildren(Iterator* it, const Matrix* pTransform
TvgBinCounter cnt = 0; TvgBinCounter cnt = 0;
//Merging shapes. the result is written in the children. //Merging shapes. the result is written in the children.
Array<const Paint*> children; Array<const Paint*> children(it->count());
children.reserve(it->count());
children.push(it->next()); children.push(it->next());
while (auto child = it->next()) { while (auto child = it->next()) {