Skip to content

Commit

Permalink
add sizehint(Vector, sz). fixes JuliaLang#2230
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Mar 4, 2013
1 parent cc4ae19 commit 6596099
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,11 @@ function resize!(a::Vector, nl::Integer)
return a
end

function sizehint(a::Vector, sz::Integer)
ccall(:jl_array_sizehint, Void, (Any, Uint), a, sz)
a
end

function pop!(a::Vector)
if isempty(a)
error("pop!: array is empty")
Expand Down
2 changes: 1 addition & 1 deletion base/intset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function sizehint(s::IntSet, top::Integer)
end
s.limit = top
end
s.limit
s
end

function add!(s::IntSet, n::Integer)
Expand Down
9 changes: 9 additions & 0 deletions src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,15 @@ void jl_array_del_end(jl_array_t *a, size_t dec)
a->length -= dec; a->nrows -= dec;
}

void jl_array_sizehint(jl_array_t *a, size_t sz)
{
if (sz <= jl_array_len(a))
return;
size_t inc = sz - jl_array_len(a);
jl_array_grow_end(a, inc);
a->length -= inc; a->nrows -= inc;
}

void jl_array_grow_beg(jl_array_t *a, size_t inc)
{
// designed to handle the case of growing and shrinking at both ends
Expand Down
1 change: 1 addition & 0 deletions src/julia.expmap
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
jl_array_del_end;
jl_array_grow_beg;
jl_array_grow_end;
jl_array_sizehint;
jl_array_ptr;
jl_arrayref;
jl_arrayset;
Expand Down
1 change: 1 addition & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ DLLEXPORT void jl_array_grow_end(jl_array_t *a, size_t inc);
DLLEXPORT void jl_array_del_end(jl_array_t *a, size_t dec);
DLLEXPORT void jl_array_grow_beg(jl_array_t *a, size_t inc);
DLLEXPORT void jl_array_del_beg(jl_array_t *a, size_t dec);
DLLEXPORT void jl_array_sizehint(jl_array_t *a, size_t sz);
DLLEXPORT void *jl_value_ptr(jl_value_t *a);
void jl_cell_1d_push(jl_array_t *a, jl_value_t *item);

Expand Down

0 comments on commit 6596099

Please sign in to comment.