473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

add_new() for array like push_back() for vector

Hi,

vector<T> v (100);
T *a = new T [100];

v.push_back(T());
Is it possible to add new T to array to get _contiguous_ storage area (for 101 element in an example above)?

--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html

Jul 22 '05 #1
18 3131

"Alex Vinokur" <al****@big.foot.com> wrote in message
news:c5*************@ID-79865.news.uni-berlin.de...
Hi,

vector<T> v (100);
T *a = new T [100];

v.push_back(T());
Is it possible to add new T to array to get _contiguous_ storage area (for 101 element in an example above)?


No, all you can do is allocate a new 101 element array and copy elements
from the old array to the new array.

But what's the problem? vector is contiguous already, just use a vector.

john
Jul 22 '05 #2

"Alex Vinokur" <al****@big.foot.com> wrote in message
news:c5*************@ID-79865.news.uni-berlin.de...
Hi,

vector<T> v (100);
T *a = new T [100];

v.push_back(T());
Is it possible to add new T to array to get _contiguous_ storage area (for 101 element in an example above)?


No, all you can do is allocate a new 101 element array and copy elements
from the old array to the new array.

But what's the problem? vector is contiguous already, just use a vector.

john
Jul 22 '05 #3
"Alex Vinokur" <al****@big.foot.com> wrote in message
news:c5*************@ID-79865.news.uni-berlin.de...
Hi,

vector<T> v (100);
T *a = new T [100];

v.push_back(T());
Is it possible to add new T to array to get _contiguous_ storage area (for

101 element in an example above)?

yes. A vector always has contiguous storage. Just say

const int sz = 100;
std::copy( a, a+sz, std::back_inserter( v ) );

br

Thorsten

(Btw, one should *always* stay away from builtin arrays in C++, unless there
is a *very* good reason.
Use boost::array<> and std::vector<> instead.)
Jul 22 '05 #4
"Alex Vinokur" <al****@big.foot.com> wrote in message
news:c5*************@ID-79865.news.uni-berlin.de...
Hi,

vector<T> v (100);
T *a = new T [100];

v.push_back(T());
Is it possible to add new T to array to get _contiguous_ storage area (for

101 element in an example above)?

yes. A vector always has contiguous storage. Just say

const int sz = 100;
std::copy( a, a+sz, std::back_inserter( v ) );

br

Thorsten

(Btw, one should *always* stay away from builtin arrays in C++, unless there
is a *very* good reason.
Use boost::array<> and std::vector<> instead.)
Jul 22 '05 #5

"John Harrison" <jo*************@hotmail.com> wrote in message news:c5*************@ID-196037.news.uni-berlin.de...

"Alex Vinokur" <al****@big.foot.com> wrote in message
news:c5*************@ID-79865.news.uni-berlin.de...
Hi,

vector<T> v (100);
T *a = new T [100];

v.push_back(T());
Is it possible to add new T to array to get _contiguous_ storage area (for

101 element in an example above)?


No, all you can do is allocate a new 101 element array and copy elements
from the old array to the new array.

But what's the problem? vector is contiguous already, just use a vector.


I would like to impove performance of algorithm "Computing very long Fibonacci numbers" at
http://groups.google.com/groups?selm....uni-berlin.de.

In particular I want to try
to replace vector<ulong> units_ in class BigInt
by an array of ulong's.

[snip]

--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html

Jul 22 '05 #6

"John Harrison" <jo*************@hotmail.com> wrote in message news:c5*************@ID-196037.news.uni-berlin.de...

"Alex Vinokur" <al****@big.foot.com> wrote in message
news:c5*************@ID-79865.news.uni-berlin.de...
Hi,

vector<T> v (100);
T *a = new T [100];

v.push_back(T());
Is it possible to add new T to array to get _contiguous_ storage area (for

101 element in an example above)?


No, all you can do is allocate a new 101 element array and copy elements
from the old array to the new array.

But what's the problem? vector is contiguous already, just use a vector.


I would like to impove performance of algorithm "Computing very long Fibonacci numbers" at
http://groups.google.com/groups?selm....uni-berlin.de.

In particular I want to try
to replace vector<ulong> units_ in class BigInt
by an array of ulong's.

[snip]

--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html

Jul 22 '05 #7
> >
But what's the problem? vector is contiguous already, just use a vector.

I would like to impove performance of algorithm "Computing very long

Fibonacci numbers" at http://groups.google.com/groups?selm....uni-berlin.de.
In particular I want to try
to replace vector<ulong> units_ in class BigInt
by an array of ulong's.


Not sure that would give you any improvement. But of course it all depends
on your compiler and STL implementation.

One possible improvement is

BigInt Fibonacci::get_number (uint n_i)
{
fibs_.reserve(n_i + 1); // <<<------------ add this
const uint cur_size = fibs_.size ();

which will help avoid repeatedly resizing the fibs_ vector.

john
Jul 22 '05 #8
> >
But what's the problem? vector is contiguous already, just use a vector.

I would like to impove performance of algorithm "Computing very long

Fibonacci numbers" at http://groups.google.com/groups?selm....uni-berlin.de.
In particular I want to try
to replace vector<ulong> units_ in class BigInt
by an array of ulong's.


Not sure that would give you any improvement. But of course it all depends
on your compiler and STL implementation.

One possible improvement is

BigInt Fibonacci::get_number (uint n_i)
{
fibs_.reserve(n_i + 1); // <<<------------ add this
const uint cur_size = fibs_.size ();

which will help avoid repeatedly resizing the fibs_ vector.

john
Jul 22 '05 #9

"John Harrison" <jo*************@hotmail.com> wrote in message news:c5*************@ID-196037.news.uni-berlin.de...

But what's the problem? vector is contiguous already, just use a vector.

I would like to impove performance of algorithm "Computing very long

Fibonacci numbers" at

http://groups.google.com/groups?selm....uni-berlin.de.

In particular I want to try
to replace vector<ulong> units_ in class BigInt
by an array of ulong's.


Not sure that would give you any improvement. But of course it all depends
on your compiler and STL implementation.

One possible improvement is

BigInt Fibonacci::get_number (uint n_i)
{
fibs_.reserve(n_i + 1); // <<<------------ add this


This improvement is very significant (see below).
John, thank you very much.
const uint cur_size = fibs_.size ();

which will help avoid repeatedly resizing the fibs_ vector.

john


===========================
------------------------------
Windows 2000
CYGWIN_NT-5.0 1.5.5(0.94/3/2)
GNU g++ version 3.3.1 (cygming special)
------------------------------

------ Compilation ------
$ g++ [Optimaze option] -mno-cygwin foo.cpp
-------------------------

Computing Fibonacci[50,000].
Comparative performance.
-----------------------------------------------
| Original | Algorithm with |
Optimization | Algorithm | John Harrison's |
| | improvement |
----------------|-----------------------------|
No optimization | 6.118 | 5.027 |
Optimization O1 | 3.344 | 2.423 |
Optimization O2 | 3.214 | 2.323 |
Optimization O2 | 3.294 | 2.293 |
-----------------------------------------------
Thanks again.

--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html

Jul 22 '05 #10

"John Harrison" <jo*************@hotmail.com> wrote in message news:c5*************@ID-196037.news.uni-berlin.de...

But what's the problem? vector is contiguous already, just use a vector.

I would like to impove performance of algorithm "Computing very long

Fibonacci numbers" at

http://groups.google.com/groups?selm....uni-berlin.de.

In particular I want to try
to replace vector<ulong> units_ in class BigInt
by an array of ulong's.


Not sure that would give you any improvement. But of course it all depends
on your compiler and STL implementation.

One possible improvement is

BigInt Fibonacci::get_number (uint n_i)
{
fibs_.reserve(n_i + 1); // <<<------------ add this


This improvement is very significant (see below).
John, thank you very much.
const uint cur_size = fibs_.size ();

which will help avoid repeatedly resizing the fibs_ vector.

john


===========================
------------------------------
Windows 2000
CYGWIN_NT-5.0 1.5.5(0.94/3/2)
GNU g++ version 3.3.1 (cygming special)
------------------------------

------ Compilation ------
$ g++ [Optimaze option] -mno-cygwin foo.cpp
-------------------------

Computing Fibonacci[50,000].
Comparative performance.
-----------------------------------------------
| Original | Algorithm with |
Optimization | Algorithm | John Harrison's |
| | improvement |
----------------|-----------------------------|
No optimization | 6.118 | 5.027 |
Optimization O1 | 3.344 | 2.423 |
Optimization O2 | 3.214 | 2.323 |
Optimization O2 | 3.294 | 2.293 |
-----------------------------------------------
Thanks again.

--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html

Jul 22 '05 #11
"Alex Vinokur" <al****@big.foot.com> wrote in message
news:c5*************@ID-79865.news.uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> wrote in message

news:c5*************@ID-196037.news.uni-berlin.de...
>
> But what's the problem? vector is contiguous already, just use a vector. >

I would like to impove performance of algorithm "Computing very long

Fibonacci numbers" at

http://groups.google.com/groups?selm....uni-berlin.de.

Just a quick question: why don't just use the formula for a Fibonacci
number? It's

Fib_n = 1/sqrt(5) * ((1 + sqrt(5))/2)^n - ((1-sqrt(5))/2)^n)

its been known for almost 300 years :-)

br

Thorsten
Jul 22 '05 #12
"Alex Vinokur" <al****@big.foot.com> wrote in message
news:c5*************@ID-79865.news.uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> wrote in message

news:c5*************@ID-196037.news.uni-berlin.de...
>
> But what's the problem? vector is contiguous already, just use a vector. >

I would like to impove performance of algorithm "Computing very long

Fibonacci numbers" at

http://groups.google.com/groups?selm....uni-berlin.de.

Just a quick question: why don't just use the formula for a Fibonacci
number? It's

Fib_n = 1/sqrt(5) * ((1 + sqrt(5))/2)^n - ((1-sqrt(5))/2)^n)

its been known for almost 300 years :-)

br

Thorsten
Jul 22 '05 #13

"Thorsten Ottosen" <ne*****@cs.auc.dk> wrote in message news:40***********************@news.optusnet.com.a u...
"Alex Vinokur" <al****@big.foot.com> wrote in message
news:c5*************@ID-79865.news.uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> wrote in message

news:c5*************@ID-196037.news.uni-berlin.de...
> >
> > But what's the problem? vector is contiguous already, just use a vector. > >
>
> I would like to impove performance of algorithm "Computing very long
Fibonacci numbers" at
>

http://groups.google.com/groups?selm....uni-berlin.de.

Just a quick question: why don't just use the formula for a Fibonacci
number? It's

Fib_n = 1/sqrt(5) * ((1 + sqrt(5))/2)^n - ((1-sqrt(5))/2)^n)

its been known for almost 300 years :-)

[snip]

I wanted
* to check what (and how fast) can we compute when using _explicit_ Fibonacci formula,
* to create and to test class BigInt,
* to use this algorithm to compute _all_ Fibonacci numbers 1-n (not only Fibonaccci[n])

--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html

Jul 22 '05 #14

"Thorsten Ottosen" <ne*****@cs.auc.dk> wrote in message news:40***********************@news.optusnet.com.a u...
"Alex Vinokur" <al****@big.foot.com> wrote in message
news:c5*************@ID-79865.news.uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> wrote in message

news:c5*************@ID-196037.news.uni-berlin.de...
> >
> > But what's the problem? vector is contiguous already, just use a vector. > >
>
> I would like to impove performance of algorithm "Computing very long
Fibonacci numbers" at
>

http://groups.google.com/groups?selm....uni-berlin.de.

Just a quick question: why don't just use the formula for a Fibonacci
number? It's

Fib_n = 1/sqrt(5) * ((1 + sqrt(5))/2)^n - ((1-sqrt(5))/2)^n)

its been known for almost 300 years :-)

[snip]

I wanted
* to check what (and how fast) can we compute when using _explicit_ Fibonacci formula,
* to create and to test class BigInt,
* to use this algorithm to compute _all_ Fibonacci numbers 1-n (not only Fibonaccci[n])

--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html

Jul 22 '05 #15
>
I wanted
* to check what (and how fast) can we compute when using _explicit_ Fibonacci formula, * to create and to test class BigInt,
* to use this algorithm to compute _all_ Fibonacci numbers 1-n (not only Fibonaccci[n])


Another possible optimization, replace

default :
fibs_.push_back (BigInt (get_number (i - 2), get_number (i - 1)));
break;

with

default :
fibs_.push_back (BigInt (fibs_[i - 2], fibs_[i - 1]));
break;

When you are calculating Fib(n), Fib(n-1) and Fib(n-2) must already have
been calculated and stored so this is safe to do. Might save a little time
and can't do any harm.

Actually my intuition is that it will not save any significant time, but
worth a try.

john
Jul 22 '05 #16
>
I wanted
* to check what (and how fast) can we compute when using _explicit_ Fibonacci formula, * to create and to test class BigInt,
* to use this algorithm to compute _all_ Fibonacci numbers 1-n (not only Fibonaccci[n])


Another possible optimization, replace

default :
fibs_.push_back (BigInt (get_number (i - 2), get_number (i - 1)));
break;

with

default :
fibs_.push_back (BigInt (fibs_[i - 2], fibs_[i - 1]));
break;

When you are calculating Fib(n), Fib(n-1) and Fib(n-2) must already have
been calculated and stored so this is safe to do. Might save a little time
and can't do any harm.

Actually my intuition is that it will not save any significant time, but
worth a try.

john
Jul 22 '05 #17

"John Harrison" <jo*************@hotmail.com> wrote in message news:c5*************@ID-196037.news.uni-berlin.de...

I wanted
* to check what (and how fast) can we compute when using _explicit_

Fibonacci formula,
* to create and to test class BigInt,
* to use this algorithm to compute _all_ Fibonacci numbers 1-n (not only

Fibonaccci[n])


Another possible optimization, replace

default :
fibs_.push_back (BigInt (get_number (i - 2), get_number (i - 1)));
break;

with

default :
fibs_.push_back (BigInt (fibs_[i - 2], fibs_[i - 1]));
break;

When you are calculating Fib(n), Fib(n-1) and Fib(n-2) must already have
been calculated and stored so this is safe to do. Might save a little time
and can't do any harm.

Actually my intuition is that it will not save any significant time, but
worth a try.

john


I have tested that.
It is unclear if it saves some time, but indeed it does no harm.
I am going to use
* fibs_.push_back (BigInt (fibs_[i - 2], fibs_[i - 1]));

P.S. That was one of options in very old algorithm :
http://groups.google.com/groups?selm...40smc.vnet.net (method getFibNumber (int n_i)).

--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html



Jul 22 '05 #18

"John Harrison" <jo*************@hotmail.com> wrote in message news:c5*************@ID-196037.news.uni-berlin.de...

I wanted
* to check what (and how fast) can we compute when using _explicit_

Fibonacci formula,
* to create and to test class BigInt,
* to use this algorithm to compute _all_ Fibonacci numbers 1-n (not only

Fibonaccci[n])


Another possible optimization, replace

default :
fibs_.push_back (BigInt (get_number (i - 2), get_number (i - 1)));
break;

with

default :
fibs_.push_back (BigInt (fibs_[i - 2], fibs_[i - 1]));
break;

When you are calculating Fib(n), Fib(n-1) and Fib(n-2) must already have
been calculated and stored so this is safe to do. Might save a little time
and can't do any harm.

Actually my intuition is that it will not save any significant time, but
worth a try.

john


I have tested that.
It is unclear if it saves some time, but indeed it does no harm.
I am going to use
* fibs_.push_back (BigInt (fibs_[i - 2], fibs_[i - 1]));

P.S. That was one of options in very old algorithm :
http://groups.google.com/groups?selm...40smc.vnet.net (method getFibNumber (int n_i)).

--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html



Jul 22 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Alex Vinokur | last post by:
Hi, vector<T> v (100); T *a = new T ; v.push_back(T()); Is it possible to add new T to array to get _contiguous_ storage area (for 101 element in an example above)? -- Alex Vinokur
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.