C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Deque as two vectors

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Jim Apple
Guest





PostPosted: Mon Nov 17, 2003 8:13 pm    Post subject: Deque as two vectors Reply with quote



Everywhere I see implementations of std::deque, it uses a map/block
structure. Is there any reason why it can't be implemented as two
vectors, one for the front, and one for the back?

Jim
--
to email me, use only the 'j' followed by the red fruit

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
P.J. Plauger
Guest





PostPosted: Mon Nov 17, 2003 9:56 pm    Post subject: Re: Deque as two vectors Reply with quote



"Jim Apple" <jpearapplebanana (AT) freeshell (DOT) org> wrote


Quote:
Everywhere I see implementations of std::deque, it uses a map/block
structure. Is there any reason why it can't be implemented as two
vectors, one for the front, and one for the back?

I'm guessing that you intend the front vector to store elements in
reverse order, so that push_front has amortized constant time.
If so, that approach meets deque requirements in many cases, but
not all. What happens, for example, if you do enough pop_front
calls to empty the front vector (or enough pop_back calls to
empty the back vector)? Subsequent pops then get just as expensive
as for a vector.

Maybe there's a strategy for handling or avoiding this case, but I
suspect that it starts looking as expensive in complexity as
maintaining the traditional map of blocks.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Ken Alverson
Guest





PostPosted: Tue Nov 18, 2003 1:11 am    Post subject: Re: Deque as two vectors Reply with quote



"Jim Apple" <jpearapplebanana (AT) freeshell (DOT) org> wrote

Quote:
Everywhere I see implementations of std::deque, it uses a map/block
structure. Is there any reason why it can't be implemented as two
vectors, one for the front, and one for the back?

Because deques don't just grow out from and shrink to a center location. You
can constantly push on the end and pop off the beginning. Unless I'm missing
something, your two vector solution wouldn't handle that situation
efficiently.

Ken


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
John Potter
Guest





PostPosted: Tue Nov 18, 2003 4:17 am    Post subject: Re: Deque as two vectors Reply with quote

On Mon, 17 Nov 2003 20:13:08 +0000 (UTC), [email]jpearapplebanana (AT) freeshell (DOT) org[/email]
(Jim Apple) wrote:

Quote:
Everywhere I see implementations of std::deque, it uses a map/block
structure. Is there any reason why it can't be implemented as two
vectors, one for the front, and one for the back?

Insert(begin(), x) and insert(end(), x) are required to be constant time
making exactly one call to the copy ctor. It is not allowed to
reallocate and copy as vector does.

John

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Ben Hutchings
Guest





PostPosted: Wed Nov 26, 2003 9:01 pm    Post subject: Re: Deque as two vectors Reply with quote

John Potter wrote:
Quote:
On Mon, 17 Nov 2003 20:13:08 +0000 (UTC), [email]jpearapplebanana (AT) freeshell (DOT) org[/email]
(Jim Apple) wrote:

Everywhere I see implementations of std::deque, it uses a map/block
structure. Is there any reason why it can't be implemented as two
vectors, one for the front, and one for the back?

Insert(begin(), x) and insert(end(), x) are required to be constant time
making exactly one call to the copy ctor.

Where the standard refers to time complexity, it really means the
complexity of the number of operations performed on elements (23.1/2).

Quote:
It is not allowed to reallocate and copy as vector does.

However, it may do that with the map or some other hidden information, so
the time complexity as normally understood is likely to be "amortised
constant", with the worst case being linear.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
John Potter
Guest





PostPosted: Mon Dec 01, 2003 1:11 am    Post subject: Re: Deque as two vectors Reply with quote

On Wed, 26 Nov 2003 21:01:24 +0000 (UTC), [email]do-not-spam-benh (AT) bwsint (DOT) com[/email]
(Ben Hutchings) wrote:

Quote:
John Potter wrote:
On Mon, 17 Nov 2003 20:13:08 +0000 (UTC), [email]jpearapplebanana (AT) freeshell (DOT) org[/email]
(Jim Apple) wrote:

Everywhere I see implementations of std::deque, it uses a map/block
structure. Is there any reason why it can't be implemented as two
vectors, one for the front, and one for the back?

Insert(begin(), x) and insert(end(), x) are required to be constant time
making exactly one call to the copy ctor.

Where the standard refers to time complexity, it really means the
complexity of the number of operations performed on elements (23.1/2).

That is the point. Exactly one call to the copy ctor is allowed. No
reallocation of space containing T's is allowed. Impossible to
implement deque with two vectors.

Quote:
It is not allowed to reallocate and copy as vector does.

However, it may do that with the map or some other hidden information, so
the time complexity as normally understood is likely to be "amortised
constant", with the worst case being linear.

Sure and when the T is large enough to produce a map of pointers to
arrays of one T, it could even be quadradic. So what? You still can't
implement it with two vectors.

John

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
James Kuyper
Guest





PostPosted: Mon Dec 01, 2003 1:12 am    Post subject: Re: Deque as two vectors Reply with quote

[email]do-not-spam-benh (AT) bwsint (DOT) com[/email] (Ben Hutchings) wrote in message news:<slrnbs96o2.1r4.do-not-spam-benh (AT) tin (DOT) bwsint.com>...
Quote:
John Potter wrote:
....
Insert(begin(), x) and insert(end(), x) are required to be constant time
making exactly one call to the copy ctor.

Where the standard refers to time complexity, it really means the
complexity of the number of operations performed on elements (23.1/2).

What he said is almost an exact quote of 23.2.1.3p3. To be exact, it
says "Inserting a single element at the beginning or end of a deque
always takes constant time and causes a single call to the copy
constructor of T".

Quote:
It is not allowed to reallocate and copy as vector does.

However, it may do that with the map or some other hidden information, so
the time complexity as normally understood is likely to be "amortised
constant", with the worst case being linear.

In general yes, but not when it makes more specific statements, such
as "a single call to the copy construct of T". Note also that "An
insert at either end of the deque ... has no effect on the validity of
references to elements of the deque." That's can't be done if there's
any reallocation triggered by inserting those elements. That's a hard
thing to ensure in conjunction with the other complexity requirements,
if the underlying implementation were that of using two vectors.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.