Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace variable length arrays with std::vector #24

Merged
merged 1 commit into from
Oct 3, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions middle-pgsql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,36 +777,40 @@ int middle_pgsql_t::relations_set(osmid_t id, struct member *members, int member
struct keyval member_list;
char buf[64];

osmid_t node_parts[member_count],
way_parts[member_count],
rel_parts[member_count];
int node_count = 0, way_count = 0, rel_count = 0;

std::vector<osmid_t> all_parts(member_count), node_parts, way_parts, rel_parts;
node_parts.reserve(member_count);
way_parts.reserve(member_count);
rel_parts.reserve(member_count);

keyval::initList( &member_list );

osmid_t all_parts[member_count];
int all_count = 0;
keyval::initList( &member_list );
for( i=0; i<member_count; i++ )
{
char tag = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea what tag is for? Seems to be set but not used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, oops! I got my }s mixed up.

switch( members[i].type )
{
case OSMTYPE_NODE: node_parts[node_count++] = members[i].id; tag = 'n'; break;
case OSMTYPE_WAY: way_parts[way_count++] = members[i].id; tag = 'w'; break;
case OSMTYPE_RELATION: rel_parts[rel_count++] = members[i].id; tag = 'r'; break;
case OSMTYPE_NODE: node_count++; node_parts.push_back(members[i].id); tag = 'n'; break;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using push_back on empty std::vector means it's likely to re-allocate often. Might be better to do the same as the old code and call std::vector::reserve() before the loop.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, will add & repush.

case OSMTYPE_WAY: way_count++; way_parts.push_back(members[i].id); tag = 'w'; break;
case OSMTYPE_RELATION: rel_count++; rel_parts.push_back(members[i].id); tag = 'r'; break;
default: fprintf( stderr, "Internal error: Unknown member type %d\n", members[i].type ); util::exit_nicely();
}
sprintf( buf, "%c%" PRIdOSMID, tag, members[i].id );
keyval::addItem( &member_list, buf, members[i].role, 0 );
}
memcpy( all_parts+all_count, node_parts, node_count*sizeof(osmid_t) ); all_count+=node_count;
memcpy( all_parts+all_count, way_parts, way_count*sizeof(osmid_t) ); all_count+=way_count;
memcpy( all_parts+all_count, rel_parts, rel_count*sizeof(osmid_t) ); all_count+=rel_count;

int all_count = 0;
std::copy( node_parts.begin(), node_parts.end(), all_parts.begin() );
std::copy( way_parts.begin(), way_parts.end(), all_parts.begin() + node_count );
std::copy( rel_parts.begin(), rel_parts.end(), all_parts.begin() + node_count + way_count);
all_count = node_count + way_count + rel_count;

if( rel_table->copyMode )
{
char *tag_buf = strdup(pgsql_store_tags(tags,1));
const char *member_buf = pgsql_store_tags(&member_list,1);
char *parts_buf = pgsql_store_nodes(all_parts, all_count);
char *parts_buf = pgsql_store_nodes(&all_parts[0], all_count);
int length = strlen(member_buf) + strlen(tag_buf) + strlen(parts_buf) + 64;
buffer = (char *)alloca(length);
if( snprintf( buffer, length, "%" PRIdOSMID "\t%d\t%d\t%s\t%s\t%s\n",
Expand All @@ -825,7 +829,7 @@ int middle_pgsql_t::relations_set(osmid_t id, struct member *members, int member
ptr += sprintf(ptr, "%d", node_count ) + 1;
paramValues[2] = ptr;
sprintf( ptr, "%d", node_count+way_count );
paramValues[3] = pgsql_store_nodes(all_parts, all_count);
paramValues[3] = pgsql_store_nodes(&all_parts[0], all_count);
paramValues[4] = pgsql_store_tags(&member_list,0);
if( paramValues[4] )
paramValues[4] = strdup(paramValues[4]);
Expand Down