Search code examples
c++data-structuresvala

What is the best way to store a list of small instructions?


I want to do a small artificial life simulator in C++ or Vala. I can't decide how to write a gene class. A gene consists of a set of assembler-like instructions that are stored in a very small class, like:

rnd 0 10
add .0 20
mov .0 $accel

First, should I use linked list of array (std::list or std::vector)?

Second, should I encapsulate the array into the class or leave it public? If I choose the former, I will have to wrap many of the functions of the list. The latter violates encapsulation.


Solution

    1. Almost surely a vector; a list would give you only overhead (in terms of memory used and time spent navigating the list), giving you no advantage (I don't think your genes need to have code continuously inserted in the middle, and even so if the data is small the vector still wins).

    2. If the clients of the class need to manipulate almost freely the list of instructions anyway I see no use in encapsulating it and then adding tons of small wrapper functions.