Skip to content
Open
Show file tree
Hide file tree
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
42 changes: 26 additions & 16 deletions ddx/cow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@
//--------------------------------------------
#include <vector>
#include <stdio.h>
#include <cstdlib>
#include <cstring>

template <int I, int N, typename Fn>
inline void apply_all_stomachs_impl( Fn&& fn )
{
fn( I );
if constexpr( I + 1 < N )
apply_all_stomachs_impl<I + 1, N>( (Fn&&)fn );
}

template <int N, typename Fn>
inline void apply_all_stomachs( Fn&& fn )
{
apply_all_stomachs_impl<0, N>( (Fn&&)fn );
}

int const num_stomachs = 7;

Expand Down Expand Up @@ -211,59 +227,54 @@ bool exec( int instruction )
// Oom
case 14:
{
for( int i=0; i<num_stomachs; ++i )
{
apply_all_stomachs<num_stomachs>( [&]( int i ) {
if( mem_poses[i] == memory[i].begin() )
quit( true );
else
mem_poses[i]--;
}
} );
break;
}

// oOm
case 15:
{
for( int i=0; i<num_stomachs; ++i )
{
apply_all_stomachs<num_stomachs>( [&]( int i ) {
mem_poses[i]++;
if( mem_poses[i] == memory[i].end() )
{
memory[i].push_back(0);
mem_poses[i] = memory[i].end();
mem_poses[i]--;
}
}
} );
break;
}

// OoM
case 16:
{
for( int i=0; i<num_stomachs; ++i )
{
apply_all_stomachs<num_stomachs>( [&]( int i ) {
(*mem_poses[i])--;
}
} );
break;
}

// oOM
case 17:
{
for( int i=0; i<num_stomachs; ++i)
{
apply_all_stomachs<num_stomachs>( [&]( int i ) {
(*mem_poses[i])++;
}
} );
break;
}

// ooo
case 18:
{
for( int i=0; i<num_stomachs; ++i)
{
apply_all_stomachs<num_stomachs>( [&]( int i ) {
(*mem_poses[i]) = 0;
}
} );
break;
}

Expand Down Expand Up @@ -421,4 +432,3 @@ int main( int argc, char** argv )
return 0;
}


Loading