Implementing a two-way Turing Machine tape as an improvement to std::deque

8 views
Skip to first unread message

olcott

unread,
May 12, 2022, 6:51:33 PMMay 12
to
C/C++ people please critique this as the basis for an improvement to
std::deque. It seems to have the key functionality of std::deque and
does it much more simply while saving time and space.
https://www.cplusplus.com/reference/deque/deque/

#define tape_element unsigned char

class Tape_Type
{
private:
int Tape_Head = 0; // Can be negative
std::vector<tape_element> Left; // Stores left expansion
std::vector<tape_element> Right; // Stores right expansion
tape_element & operator[](int index);

public:
void move_left(); // Tape_Head--; Left.push_back(0); as needed
void move_right(); // Tape_Head++; Left.push_back(0); as needed
void Write(tape_element Y){ this->operator[](Tape_Head) = Y; };
tape_element Read() { return this->operator[](Tape_Head); };
Tape_Type(){ Right.push_back('_'); } // constructor
void Output();
};

tape_element& Tape_Type::operator[](int index)
{
if (index > 0)
return Right[index];
int Left_Index = ((index * -1) -1);
return Left[Left_Index];
}

void Tape_Type::Output()
{
printf("Tape_Type::Output()\n");

if (Left.size())
{
int Last_One = Left.size() - 1;
for (int N = Last_One; N >= 0; N--)
{
int TH = (N + 1) * -1; // determine Tape_Head from N
printf("[%04d]:%c Left[%02d]\n", TH, Left[N], N);
}
}
if (Right.size())
for (int N = 0; N < Right.size(); N++)
printf("[%04d]:%c Right[%02d]\n", N, Right[N], N);
}

void Tape_Type::move_left()
{
Tape_Head--;
int Left_Index = ((Tape_Head * -1) -1);
if (Left_Index == Left.size())
Left.push_back('_');
}

void Tape_Type::move_right()
{
Tape_Head++;
if (Tape_Head == Right.size())
Right.push_back('_');
}



--
Copyright 2022 Pete Olcott

"Talent hits a target no one else can hit;
Genius hits a target no one else can see."
Arthur Schopenhauer

Mr Flibble

unread,
May 12, 2022, 6:56:14 PMMay 12
to
It might be a more appropriate solution than std::deque for your
specific use-case however it is NOT an improvement to std::deque for
the general case -- see my reply in the other thread for why.

/Flibble

olcott

unread,
May 12, 2022, 7:09:48 PMMay 12
to
I didn't see any reason why it would not make a better std::deque.

Mr Flibble

unread,
May 12, 2022, 7:22:21 PMMay 12
to
Because it doesn't meet the complexity and referential integrity
requirements of std::deque.

/Flibble

Richard Damon

unread,
May 12, 2022, 7:23:50 PMMay 12
to
Looks at what indexing with an index value of 0 does.

Operator [] want to test index >= 0, not > 0

olcott

unread,
May 12, 2022, 7:33:06 PMMay 12
to
Good catch. I just wrote that function as a simplification.

olcott

unread,
May 12, 2022, 7:38:58 PMMay 12
to
It is faster then std::deque.
I couldn't find what you mean by referential integrity it has too many
different meanings. invalidating iterators seemed to be what you mean
otherwise I have no idea.

Mr Flibble

unread,
May 12, 2022, 7:40:52 PMMay 12
to
On Thu, 12 May 2022 18:38:49 -0500
I see you are choosing to ignore my reply in the other thread. OK, I
will repost why you are wrong here:

* referential integrity and iterator invalidation are different
things: when you add or remove elements to either end of a
std::deque iterators are indeed invalidated however references to
existing elements remain valid
* If you do 1000 push_fronts followed by 1000 push_backs followed by
1000 pop_fronts all is good however your next pop_front will have
linear complexity, O(n), as it will necessitate removal of the first
element of the right std::vector.

/Flibble

olcott

unread,
May 12, 2022, 7:49:51 PMMay 12
to
Mine words the same way and has the added benefit of contiguous storage.

> * If you do 1000 push_fronts followed by 1000 push_backs followed by
> 1000 pop_fronts all is good however your next pop_front will have
> linear complexity, O(n), as it will necessitate removal of the first
> element of the right std::vector.

I don't think that this applies to the way that I implemented it.
pop_front pops from the end of Left. pop_back pops from the end of
Right. This is the key aspect that I used from David's two-stack approach.

Mr Flibble

unread,
May 12, 2022, 7:53:44 PMMay 12
to
On Thu, 12 May 2022 18:49:43 -0500
Then it is totally different to what std::deque offers: std::deque
allows ALL elements to be either popped from the front or back. Try
reading AND UNDERSTANDING what I wrote again.

/Flibble

Ben

unread,
May 12, 2022, 8:07:05 PMMay 12
to
olcott <No...@NoWhere.com> writes:

I've removed the philosophy group and comp.lang.c as this is C++.

> C/C++ people please critique this as the basis for an improvement to
> std::deque.

You will get critiques of the code on whatever basis people feel
inclined to comment! You can't limit the comments to some particular
context.

> It seems to have the key functionality of std::deque and
> does it much more simply while saving time and space.

It does not have any of the functionality of std::deque. There is a
commonly used (though not particularly efficient) way to implement a
deque using two arrays, but this is not it.

> #define tape_element unsigned char

Use a typedef or using declaration. Also, I'd put the typedef in the
class as the type belongs to the class (as least that appears to be the
case from the name you've chosen).

> class Tape_Type
> {
> private:
> int Tape_Head = 0; // Can be negative
> std::vector<tape_element> Left; // Stores left expansion
> std::vector<tape_element> Right; // Stores right expansion
> tape_element & operator[](int index);
>
> public:
> void move_left(); // Tape_Head--; Left.push_back(0); as needed
> void move_right(); // Tape_Head++; Left.push_back(0); as needed

The comments are wrong since you push_back('_').

> void Write(tape_element Y){ this->operator[](Tape_Head) = Y; };
> tape_element Read() { return this->operator[](Tape_Head); };

I'd write (*this)[Tape_Head] but I suppose that's just a matter of style.

> Tape_Type(){ Right.push_back('_'); } // constructor

The constructor leaves a Tape_Type object in an unusable state, but
that's due to a bug in operator[]. Did you test?

> void Output();
> };
>
> tape_element& Tape_Type::operator[](int index)
> {
> if (index > 0)
> return Right[index];

Bug. You meant index >= 0 I think.

> int Left_Index = ((index * -1) -1);

Why not -index - 1?

> return Left[Left_Index];

Do you think introducing a new variable really make the code clearer
that simply writing

return Left[-index - 1];

? Personally, I'd write the whole thing as

return index >= 0 ? Right[index] : Left[-index - 1];

> }
>
> void Tape_Type::Output()
> {
> printf("Tape_Type::Output()\n");
>
> if (Left.size())
> {
> int Last_One = Left.size() - 1;
> for (int N = Last_One; N >= 0; N--)
> {
> int TH = (N + 1) * -1; // determine Tape_Head from N
> printf("[%04d]:%c Left[%02d]\n", TH, Left[N], N);
> }
> }
> if (Right.size())
> for (int N = 0; N < Right.size(); N++)
> printf("[%04d]:%c Right[%02d]\n", N, Right[N], N);
> }
>
> void Tape_Type::move_left()
> {
> Tape_Head--;
> int Left_Index = ((Tape_Head * -1) -1);
> if (Left_Index == Left.size())
> Left.push_back('_');
> }
>
> void Tape_Type::move_right()
> {
> Tape_Head++;
> if (Tape_Head == Right.size())
> Right.push_back('_');
> }

I find the capitalisation odd and unhelpful as there does not seem to be
any consistency about it.

--
Ben.
"le génie humain a des limites, quand la bêtise humaine n’en a pas"
Alexandre Dumas (fils)

olcott

unread,
May 12, 2022, 8:12:30 PMMay 12
to
I could allow all elements to be popped from the front or the back too.
Mine is much faster when you need far less than all elements.

olcott

unread,
May 12, 2022, 8:55:53 PMMay 12
to
On 5/12/2022 7:06 PM, Ben wrote:
> olcott <No...@NoWhere.com> writes:
>
> I've removed the philosophy group and comp.lang.c as this is C++.
>
>> C/C++ people please critique this as the basis for an improvement to
>> std::deque.
>
> You will get critiques of the code on whatever basis people feel
> inclined to comment! You can't limit the comments to some particular
> context.
>
>> It seems to have the key functionality of std::deque and
>> does it much more simply while saving time and space.
>
> It does not have any of the functionality of std::deque.

That is a ridiculously stupid thing to say. It has the key most
important functionality of a std:deque

Double ended queue
deque (usually pronounced like "deck") is an irregular acronym of
double-ended queue. Double-ended queues are sequence containers with
dynamic sizes that can be expanded or contracted on both ends (either
its front or its back).

All of the rest of the functionality of std::deque can be added as needed.

> There is a
> commonly used (though not particularly efficient) way to implement a
> deque using two arrays, but this is not it.
>
>> #define tape_element unsigned char
>
> Use a typedef or using declaration. Also, I'd put the typedef in the
> class as the type belongs to the class (as least that appears to be the
> case from the name you've chosen).
>
>> class Tape_Type
>> {
>> private:
>> int Tape_Head = 0; // Can be negative
>> std::vector<tape_element> Left; // Stores left expansion
>> std::vector<tape_element> Right; // Stores right expansion
>> tape_element & operator[](int index);
>>
>> public:
>> void move_left(); // Tape_Head--; Left.push_back(0); as needed
>> void move_right(); // Tape_Head++; Left.push_back(0); as needed
>
> The comments are wrong since you push_back('_').
>
>> void Write(tape_element Y){ this->operator[](Tape_Head) = Y; };
>> tape_element Read() { return this->operator[](Tape_Head); };
>
> I'd write (*this)[Tape_Head] but I suppose that's just a matter of style.
>
>> Tape_Type(){ Right.push_back('_'); } // constructor
>
> The constructor leaves a Tape_Type object in an unusable state, but
> that's due to a bug in operator[]. Did you test?
>

Yes I tested so I don't see what you mean.

>> void Output();
>> };
>>
>> tape_element& Tape_Type::operator[](int index)
>> {
>> if (index > 0)
>> return Right[index];
>
> Bug. You meant index >= 0 I think.

Yes Richard caught that. That was a new refactoring.
I found all of your suggestions very helpful.
I don't see the bug in the constructor.
I like mixed case because it is easier to read, mine not be consistent.

Here they are implemented.

//
// Tape_Type implements a two-way Turing machine tape.
// Right contains Tape_Head >= 0 values (right expansion)
// Left contains Tape_Head < 0 values (left expansion)
//
// Tape_Type has functionality very similar to std::deque
// yet implements this functionality much more simply.
// This saves time and space.
//
class Tape_Type
{
typedef unsigned char tape_element;
private:
int Tape_Head = 0; // Can be negative
std::vector<tape_element> Left; // Stores left expansion
std::vector<tape_element> Right; // Stores right expansion

tape_element& operator[](int index)
{
return index >= 0 ? Right[index] : Left[-index - 1];
}

public:
Tape_Type(){ Right.push_back('_'); } // constructor
void Write(tape_element Y){ (*this)[Tape_Head] = Y; };
tape_element Read() { return (*this)[Tape_Head]; };

void move_left()
{
Tape_Head--;
int Left_Index = ((Tape_Head * -1) -1);
if (Left_Index == Left.size())
Left.push_back('_');
}

void move_right()
{
Tape_Head++;
if (Tape_Head == Right.size())
Right.push_back('_');
}

void Output()
{
printf("Tape_Type::Output()\n");
if (Left.size())
{
int Last_One = Left.size() - 1;
for (int N = Last_One; N >= 0; N--)
{
int TH = (N + 1) * -1; // determine Tape_Head from N
printf("[%04d]:%c Left[%02d]\n", TH, Left[N], N);
}
}
if (Right.size())
for (int N = 0; N < Right.size(); N++)
printf("[%04d]:%c Right[%02d]\n", N, Right[N], N);
}

Mr Flibble

unread,
May 12, 2022, 8:58:19 PMMay 12
to
On Thu, 12 May 2022 19:12:22 -0500
What you have does not offer what std::deque offers so is not
equivalent to std::deque so can't be considered better than std::deque
for the general case (I don't care about your specific use-case).

/Flibble

Mr Flibble

unread,
May 12, 2022, 9:01:36 PMMay 12
to
On Thu, 12 May 2022 19:55:44 -0500
olcott <No...@NoWhere.com> wrote:

> On 5/12/2022 7:06 PM, Ben wrote:
> > olcott <No...@NoWhere.com> writes:
> >
> > I've removed the philosophy group and comp.lang.c as this is C++.
> >
> >> C/C++ people please critique this as the basis for an improvement
> >> to std::deque.
> >
> > You will get critiques of the code on whatever basis people feel
> > inclined to comment! You can't limit the comments to some
> > particular context.
> >
> >> It seems to have the key functionality of std::deque and
> >> does it much more simply while saving time and space.
> >
> > It does not have any of the functionality of std::deque.
>
> That is a ridiculously stupid thing to say. It has the key most
> important functionality of a std:deque
>
> Double ended queue
> deque (usually pronounced like "deck") is an irregular acronym of
> double-ended queue. Double-ended queues are sequence containers with
> dynamic sizes that can be expanded or contracted on both ends (either
> its front or its back).
>
> All of the rest of the functionality of std::deque can be added as
> needed.

Not with your chosen data structure of two std::vectors it can't as it
wouldn't meet the complexity and referential integrity
requirements offered by std::deque. I have told you this three times
now.

/Flibble

olcott

unread,
May 12, 2022, 9:34:49 PMMay 12
to
All these things can be added and we end up with a simple, faster,
std:deque that has most the conventional overhead and complexity abolished.

> equivalent to std::deque so can't be considered better than std::deque
> for the general case (I don't care about your specific use-case).
>
> /Flibble
>

It just a matter of defining more member functions.

olcott

unread,
May 12, 2022, 9:36:10 PMMay 12
to
It already has the same complexity.

> and referential integrity

and better referential integrity.

> requirements offered by std::deque. I have told you this three times
> now.
>
> /Flibble
>


Ben

unread,
May 12, 2022, 9:38:15 PMMay 12
to
olcott <No...@NoWhere.com> writes:

> On 5/12/2022 7:06 PM, Ben wrote:
>> olcott <No...@NoWhere.com> writes:
>> I've removed the philosophy group and comp.lang.c as this is C++.
>>
>>> C/C++ people please critique this as the basis for an improvement to
>>> std::deque.
>> You will get critiques of the code on whatever basis people feel
>> inclined to comment! You can't limit the comments to some particular
>> context.
>>
>>> It seems to have the key functionality of std::deque and
>>> does it much more simply while saving time and space.
>> It does not have any of the functionality of std::deque.
>
> That is a ridiculously stupid thing to say. It has the key most
> important functionality of a std:deque

Generally, when you think I am saying something absurd, you should
re-consider.

A deque has (at least) these operations:

front
back
pop_front
push_front
pop_back
push_back

Your Tape_Type has none of these. Even internally it does not maintain
the right data to be able to provide them.

> Double ended queue
> deque (usually pronounced like "deck") is an irregular acronym of
> double-ended queue. Double-ended queues are sequence containers with
> dynamic sizes that can be expanded or contracted on both ends (either
> its front or its back).
>
> All of the rest of the functionality of std::deque can be added as
> needed.

You mean you could implement a deque using two arrays if you chose to?
So what? All I am saying is that you haven't got a deque.

>> There is a
>> commonly used (though not particularly efficient) way to implement a
>> deque using two arrays, but this is not it.
>>
>>> #define tape_element unsigned char
>> Use a typedef or using declaration. Also, I'd put the typedef in the
>> class as the type belongs to the class (as least that appears to be the
>> case from the name you've chosen).
>>
>>> class Tape_Type
>>> {
>>> private:
>>> int Tape_Head = 0; // Can be negative
>>> std::vector<tape_element> Left; // Stores left expansion
>>> std::vector<tape_element> Right; // Stores right expansion
>>> tape_element & operator[](int index);
>>>
>>> public:
>>> void move_left(); // Tape_Head--; Left.push_back(0); as needed
>>> void move_right(); // Tape_Head++; Left.push_back(0); as needed
>> The comments are wrong since you push_back('_').
>>
>>> void Write(tape_element Y){ this->operator[](Tape_Head) = Y; };
>>> tape_element Read() { return this->operator[](Tape_Head); };
>> I'd write (*this)[Tape_Head] but I suppose that's just a matter of style.
>>
>>> Tape_Type(){ Right.push_back('_'); } // constructor
>> The constructor leaves a Tape_Type object in an unusable state, but
>> that's due to a bug in operator[]. Did you test?
>
> Yes I tested so I don't see what you mean.

Testing should have caught the bug in operator[].
There is none. I just said the constructor leaves the tape unusable
because of the bug in operator[].

> I like mixed case because it is easier to read, mine not be consistent.
>
> Here they are implemented.
>
> //
> // Tape_Type implements a two-way Turing machine tape.
> // Right contains Tape_Head >= 0 values (right expansion)
> // Left contains Tape_Head < 0 values (left expansion)
> //
> // Tape_Type has functionality very similar to std::deque

This comment is not correct.

> // yet implements this functionality much more simply.
> // This saves time and space.
> //
> class Tape_Type
> {
> typedef unsigned char tape_element;

You probably want that to be a public typedef. And now that it's in the
class, the name can be sorter. The usual convention is _t for types:

typedef unsigned char element_t;

I plugged your tape class into my C++ TM interpreter and it made it
slightly slower for the "big" BB(5) test compared with my naive tape
that just uses a single string.

Note: this is not a comment on the design of your tape. There's nothing
wrong with it (except a little inconvenience, see below). The speed
will be determined almost entirely by how std::string and std::vector
are implemented. You might see completely different timings just by
linking with a different library.

Mind you, I prefer using a string to all the other methods I've tried
because it's so convenient. You can set up the TM with an input simply
by writing

tape = input

and you can debug by printing the tape any time you like. And I use a
string_view to get the non-blank "result" out of the TM and the end of a
run. All easy to get round with a couple of functions, but still, since
it's fast I see no reason t change.

olcott

unread,
May 12, 2022, 10:37:12 PMMay 12
to
On 5/12/2022 8:38 PM, Ben wrote:
> olcott <No...@NoWhere.com> writes:
>
>> On 5/12/2022 7:06 PM, Ben wrote:
>>> olcott <No...@NoWhere.com> writes:
>>> I've removed the philosophy group and comp.lang.c as this is C++.
>>>
>>>> C/C++ people please critique this as the basis for an improvement to
>>>> std::deque.
>>> You will get critiques of the code on whatever basis people feel
>>> inclined to comment! You can't limit the comments to some particular
>>> context.
>>>
>>>> It seems to have the key functionality of std::deque and
>>>> does it much more simply while saving time and space.
>>> It does not have any of the functionality of std::deque.
>>
>> That is a ridiculously stupid thing to say. It has the key most
>> important functionality of a std:deque
>
> Generally, when you think I am saying something absurd, you should
> re-consider.
>
> A deque has (at least) these operations:
>
> front
> back
> push_front
> push_back
> pop_front
> pop_back
>
> Your Tape_Type has none of these. Even internally it does not maintain
> the right data to be able to provide them.

I also added a reserve so you could do a pre-allocated speed test.

It was easy to add these in terms of the current implementation:

public:
tape_element& front( ) { return Left.back(); }
tape_element& back() { return Right.back(); }
void pop_front() { Left.pop_back(); }
void pop_back() { Right.pop_back(); }
void push_front(tape_element& E) { Left.push_back(E); }
void push_back(tape_element& E) { Right.push_back(E); }

void reserve(unsigned int N)
{ Left.reserve(N); Right.reserve(N); }



>> Double ended queue
>> deque (usually pronounced like "deck") is an irregular acronym of
>> double-ended queue. Double-ended queues are sequence containers with
>> dynamic sizes that can be expanded or contracted on both ends (either
>> its front or its back).
>>
>> All of the rest of the functionality of std::deque can be added as
>> needed.
>
> You mean you could implement a deque using two arrays if you chose to?
> So what? All I am saying is that you haven't got a deque.

If I added these things then I would have a much better deque.
It is was a new refactoring of existing working code.
Oh, OK.

>
>> I like mixed case because it is easier to read, mine not be consistent.
>>
>> Here they are implemented.
>>
>> //
>> // Tape_Type implements a two-way Turing machine tape.
>> // Right contains Tape_Head >= 0 values (right expansion)
>> // Left contains Tape_Head < 0 values (left expansion)
>> //
>> // Tape_Type has functionality very similar to std::deque
>
> This comment is not correct.
>
>> // yet implements this functionality much more simply.
>> // This saves time and space.
>> //
>> class Tape_Type
>> {
>> typedef unsigned char tape_element;
>
> You probably want that to be a public typedef. And now that it's in the
> class, the name can be sorter. The usual convention is _t for types:
>
> typedef unsigned char element_t;
>
> I plugged your tape class into my C++ TM interpreter and it made it
> slightly slower for the "big" BB(5) test compared with my naive tape
> that just uses a single string.
>

std::string or char * ?
If char * was pre-allocated that would give it a big speed advantage.

I could add reserve(left/right):
void reserve(unsigned int N)
{ Left.reserve(N); Right.reserve(N); }

> Note: this is not a comment on the design of your tape. There's nothing
> wrong with it (except a little inconvenience, see below). The speed
> will be determined almost entirely by how std::string and std::vector
> are implemented. You might see completely different timings just by
> linking with a different library.
>
> Mind you, I prefer using a string to all the other methods I've tried
> because it's so convenient. You can set up the TM with an input simply
> by writing
>
> tape = input
>
> and you can debug by printing the tape any time you like. And I use a
> string_view to get the non-blank "result" out of the TM and the end of a
> run. All easy to get round with a couple of functions, but still, since
> it's fast I see no reason t change.
>

What about memory allocation?

Python

unread,
May 12, 2022, 10:39:43 PMMay 12
to
Could you add eggs, bacon and beans? Thanks Peter.


Mr Flibble

unread,
May 13, 2022, 3:02:51 AMMay 13
to
On Thu, 12 May 2022 20:34:41 -0500
You cannot implement all of std::deque's member functions meeting
std::deque requirements using your chosen data structure of two
std::vectors.

/Flibble

Mr Flibble

unread,
May 13, 2022, 3:04:39 AMMay 13
to
On Thu, 12 May 2022 20:36:02 -0500
You are a fucking obtuse idiot, mate.

/Flibble

tth

unread,
May 13, 2022, 3:10:26 AMMay 13
to
On 5/13/22 09:02, Mr Flibble wrote:

> You cannot implement all of std::deque's member functions meeting
> std::deque requirements using your chosen data structure of two
> std::vectors.

Not with any version of the C language.

--
+-------------------------------------------------------------------+
| sphinx of black quartz, judge my vow. |
+-------------------------------------------------------------------+

Ben

unread,
May 13, 2022, 7:01:44 AMMay 13
to
You have not provided the correct operations, so you have no idea how
hard it might be to do that. This keeps looking more and more like a
distraction to put off having to do what you said.

> public:
> tape_element& front( ) { return Left.back(); }
> tape_element& back() { return Right.back(); }
> void pop_front() { Left.pop_back(); }
> void pop_back() { Right.pop_back(); }
> void push_front(tape_element& E) { Left.push_back(E); }
> void push_back(tape_element& E) { Right.push_back(E); }
>
> void reserve(unsigned int N)
> { Left.reserve(N); Right.reserve(N); }

Not correct. Test first.

>>> Double ended queue
>>> deque (usually pronounced like "deck") is an irregular acronym of
>>> double-ended queue. Double-ended queues are sequence containers with
>>> dynamic sizes that can be expanded or contracted on both ends (either
>>> its front or its back).
>>>
>>> All of the rest of the functionality of std::deque can be added as
>>> needed.
>> You mean you could implement a deque using two arrays if you chose to?
>> So what? All I am saying is that you haven't got a deque.
>
> If I added these things then I would have a much better deque.

If you added them correctly and then removed the things that a deque
should not have. In other words, if you implemented a deque.

>>> Yes I tested so I don't see what you mean.
>> Testing should have caught the bug in operator[].
>
> It is was a new refactoring of existing working code.

A software engineer runs all the tests after every edit (or at least
before every commit). (I am not a software engineer.)

>> I plugged your tape class into my C++ TM interpreter and it made it
>> slightly slower for the "big" BB(5) test compared with my naive tape
>> that just uses a single string.
>>
>
> std::string or char * ?

std::string.

> If char * was pre-allocated that would give it a big speed advantage.

Nothing pre-allocated.

>> Mind you, I prefer using a string to all the other methods I've tried
>> because it's so convenient. You can set up the TM with an input simply
>> by writing
>> tape = input
>> and you can debug by printing the tape any time you like. And I use a
>> string_view to get the non-blank "result" out of the TM and the end of a
>> run. All easy to get round with a couple of functions, but still, since
>> it's fast I see no reason t change.
>
> What about memory allocation?

What about it? std::string does it.

olcott

unread,
May 13, 2022, 11:41:14 AMMay 13
to
They are all correct. I don't see how you can say that they are not when
you don't know my design.

>
>>>> Double ended queue
>>>> deque (usually pronounced like "deck") is an irregular acronym of
>>>> double-ended queue. Double-ended queues are sequence containers with
>>>> dynamic sizes that can be expanded or contracted on both ends (either
>>>> its front or its back).
>>>>
>>>> All of the rest of the functionality of std::deque can be added as
>>>> needed.
>>> You mean you could implement a deque using two arrays if you chose to?
>>> So what? All I am saying is that you haven't got a deque.
>>
>> If I added these things then I would have a much better deque.
>
> If you added them correctly and then removed the things that a deque
> should not have. In other words, if you implemented a deque.
>

I mere want to know that the most basic functionality of my deque is
(a) simpler (b) faster (c) smaller than a std:deque.
If Tape_Type.reserve() is used it should be your std::string.

>>>> Yes I tested so I don't see what you mean.
>>> Testing should have caught the bug in operator[].
>>
>> It is was a new refactoring of existing working code.
>
> A software engineer runs all the tests after every edit (or at least
> before every commit). (I am not a software engineer.)
>
>>> I plugged your tape class into my C++ TM interpreter and it made it
>>> slightly slower for the "big" BB(5) test compared with my naive tape
>>> that just uses a single string.
>>>
>>
>> std::string or char * ?
>
> std::string.
>
>> If char * was pre-allocated that would give it a big speed advantage.
>
> Nothing pre-allocated.
>
>>> Mind you, I prefer using a string to all the other methods I've tried
>>> because it's so convenient. You can set up the TM with an input simply
>>> by writing
>>> tape = input
>>> and you can debug by printing the tape any time you like. And I use a
>>> string_view to get the non-blank "result" out of the TM and the end of a
>>> run. All easy to get round with a couple of functions, but still, since
>>> it's fast I see no reason t change.
>>
>> What about memory allocation?
>
> What about it? std::string does it.
>

It is probably much slower than my system now that I added reserve.
I do like the idea of simplicity, it is my primary design goal.

Richard Damon

unread,
May 13, 2022, 11:56:45 AMMay 13
to
Except that you just published your "design" above.

Note, pop_front() needs to remove the first item in the queue/on the tape.

If all the contents of the tape are currently in the Right vector, and
none are on the Left, pop_front() needs to remove the first element of
the right vector, but your implementation doesn't do that.

You have made the INCORRECT assumption that the "0" point of the tape
will always exist.

For the Turing Machine, this isn't important because the Turing Machine
never needs to remove an item from the tape, at best it marks an element
as "empty", but for a general deque, that is a needed for a general
deque that meets the full specifications.

>
>>
>>>>> Double ended queue
>>>>> deque (usually pronounced like "deck") is an irregular acronym of
>>>>> double-ended queue. Double-ended queues are sequence containers with
>>>>> dynamic sizes that can be expanded or contracted on both ends (either
>>>>> its front or its back).
>>>>>
>>>>> All of the rest of the functionality of std::deque can be added as
>>>>> needed.
>>>> You mean you could implement a deque using two arrays if you chose to?
>>>> So what?  All I am saying is that you haven't got a deque.
>>>
>>> If I added these things then I would have a much better deque.
>>
>> If you added them correctly and then removed the things that a deque
>> should not have.  In other words, if you implemented a deque.
>>
>
> I mere want to know that the most basic functionality of my deque is
> (a) simpler (b) faster (c) smaller than a std:deque.
> If Tape_Type.reserve() is used it should be your std::string.
>

There is no definition of "most basic" that excludes the "required"
functionality of deque.

Remember, requirements ARE requirements, and when solving a problem, you
don't normally get to change them (Not and say you are still working on
the same problem).

olcott

unread,
May 13, 2022, 11:59:04 AMMay 13
to
// Tape_Type implements a two-way Turing machine tape.
// Right contains Tape_Head >= 0 values (right expansion)
// Left contains Tape_Head < 0 values (left expansion)
//
// Grows with Right.push_back() as Tape_Head increases above 0.
// Grows with Left.push_back() as Tape_Head decreases below 0.
//
// Tape_Type has functionality very similar to std::deque
// yet implements this functionality much more simply.
// This saves time and space.
//
class Tape_Type
{
public:
typedef unsigned char tape_element;

private:
int Tape_Head = 0; // Can be negative
std::vector<tape_element> Left; // Stores left expansion
std::vector<tape_element> Right; // Stores right expansion

tape_element& operator[](int index)
{
return index >= 0 ? Right[index] : Left[-index - 1];
}

public:
tape_element& front( ) { return Left.back(); }
tape_element& back() { return Right.back(); }
void pop_front() { Left.pop_back(); }
void pop_back() { Right.pop_back(); }
void push_front(tape_element& E) { Left.push_back(E); }
void push_back(tape_element& E) { Right.push_back(E); }
void reserve(unsigned int N)
{ Left.reserve(N); Right.reserve(N); }



olcott

unread,
May 13, 2022, 12:00:09 PMMay 13
to
Prove that it doesn't.

Mr Flibble

unread,
May 13, 2022, 12:02:42 PMMay 13
to
On Fri, 13 May 2022 10:58:56 -0500
And what happens if Left is empty, Right is non-empty and you call
pop_front()?

/Flibble

Mr Flibble

unread,
May 13, 2022, 12:04:52 PMMay 13
to
On Fri, 13 May 2022 11:00:01 -0500
Prove to me that you are not an idiot by explaining what happens if
vector Left is empty, vector Right is non-empty and you call pop_front?
Hint: as your design currently stands it will crash.

/Flibble

Ben

unread,
May 13, 2022, 12:35:41 PMMay 13
to
You posted the code. Have you fixed it now? I've not see correct code
for a deque yet.

>>>>> Double ended queue
>>>>> deque (usually pronounced like "deck") is an irregular acronym of
>>>>> double-ended queue. Double-ended queues are sequence containers with
>>>>> dynamic sizes that can be expanded or contracted on both ends (either
>>>>> its front or its back).
>>>>>
>>>>> All of the rest of the functionality of std::deque can be added as
>>>>> needed.
>>>> You mean you could implement a deque using two arrays if you chose to?
>>>> So what? All I am saying is that you haven't got a deque.
>>>
>>> If I added these things then I would have a much better deque.
>>
>> If you added them correctly and then removed the things that a deque
>> should not have. In other words, if you implemented a deque.
>
> I mere want to know that the most basic functionality of my deque is
> (a) simpler (b) faster (c) smaller than a std:deque.

And when you've correctly implemented a deque, when you've studied the
internals of std:deque, adn when you done extensive testing you can say
which of (a), (b) and (c) you have achieved.

> If Tape_Type.reserve() is used it should be your std::string.

Sure. But that's an odd test.

>>>>> Yes I tested so I don't see what you mean.
>>>> Testing should have caught the bug in operator[].
>>>
>>> It is was a new refactoring of existing working code.
>> A software engineer runs all the tests after every edit (or at least
>> before every commit). (I am not a software engineer.)
>>
>>>> I plugged your tape class into my C++ TM interpreter and it made it
>>>> slightly slower for the "big" BB(5) test compared with my naive tape
>>>> that just uses a single string.
>>>>
>>>
>>> std::string or char * ?
>> std::string.
>>
>>> If char * was pre-allocated that would give it a big speed advantage.
>> Nothing pre-allocated.
>>
>>>> Mind you, I prefer using a string to all the other methods I've tried
>>>> because it's so convenient. You can set up the TM with an input simply
>>>> by writing
>>>> tape = input
>>>> and you can debug by printing the tape any time you like. And I use a
>>>> string_view to get the non-blank "result" out of the TM and the end of a
>>>> run. All easy to get round with a couple of functions, but still, since
>>>> it's fast I see no reason t change.
>>>
>>> What about memory allocation?
>> What about it? std::string does it.
>
> It is probably much slower than my system now that I added reserve.
> I do like the idea of simplicity, it is my primary design goal.

Using a std::string for the tape is very simple.

olcott

unread,
May 13, 2022, 1:05:25 PMMay 13
to
Simply extend the definition of the member function.
Maybe std::deque performs better at this?

olcott

unread,
May 13, 2022, 1:12:12 PMMay 13
to
How does it compare for speed on your test code when you use
Tape_Type::reserve()?

Ben

unread,
May 13, 2022, 3:04:41 PMMay 13
to
Odd that you are prepared to guess.

>>> I do like the idea of simplicity, it is my primary design goal.
>>
>> Using a std::string for the tape is very simple.
>
> How does it compare for speed on your test code when you use
> Tape_Type::reserve()?

Using reserve makes no difference to the speed of either one in the only
test case I have that takes long enough to measure. This is what I'd
expect. The test case does 47,176,870 transitions and the tape ends up
being only 12,289 symbols long. The memory allocation will be a tiny
fraction of the cost. It's not measurable.

Of course that's just one test case. What TMs are you using to test?

olcott

unread,
May 13, 2022, 3:19:56 PMMay 13
to
On 5/13/2022 2:04 PM, Ben wrote:
> olcott <No...@NoWhere.com> writes:
>
>> On 5/13/2022 11:35 AM, Ben wrote:
>>> olcott <No...@NoWhere.com> writes:
>>>
>>>> On 5/13/2022 6:01 AM, Ben wrote:
>>>>> olcott <No...@NoWhere.com> writes:
>
>>>>>> What about memory allocation?
>>>>>
>>>>> What about it? std::string does it.
>>>>
>>>> It is probably much slower than my system now that I added reserve.
>
> Odd that you are prepared to guess.
>
>>>> I do like the idea of simplicity, it is my primary design goal.
>>>
>>> Using a std::string for the tape is very simple.
>>
>> How does it compare for speed on your test code when you use
>> Tape_Type::reserve()?
>
> Using reserve makes no difference to the speed of either one in the only
> test case I have that takes long enough to measure. This is what I'd
> expect. The test case does 47,176,870 transitions and the tape ends up
> being only 12,289 symbols long. The memory allocation will be a tiny
> fraction of the cost. It's not measurable.
>

So maybe yours is better. I am happy to find that you have some
significant programming skill, I never knew this before.

> Of course that's just one test case. What TMs are you using to test?
>

tm> read tm paren
tm> set tape ((()))
tm> set trace tape
tm> go

Chris M. Thomasson

unread,
May 13, 2022, 3:44:09 PMMay 13
to
Flip a coin; heads left, tails right.

Heads, try to pop from left.
Tails, try to pop from right.

Just joking around here, in a sense... ;^)

Actually, back in the day with my threading work, I have subdivided a
lock-free stack into regions to amortize things. It was nothing like a
deque. It was a long time ago, damn near 20 years. The cool part was
that multiple threads could push/pop items without interfering with one
another in a lot of use cases that respected locality. It was layered up:

per-thread container
hash bucket container
global container

Iirc, to pop an element from the container:

check per-thread
check hash bucket
check the global

if all checks fail, return nullptr, if not return the popped node.

I think I might still have this code on an old hard drive.
Reply all
Reply to author
Forward
0 new messages