This challenge is a harder version of this one.
A variable number of trits is an array of 0 or more trits (a trit is a ternary digit). So
[0, 2, 2, 1] is a variable number of trits, but so is
Write a function or program that, given an non-negative integer returns a variable number of trits such that every integer has a one-to-one (bijective) mapping with an array.
There are an infinite amount of such mappings, you are free to construct one as you please, but it must be one-to-one. Your mapping must conceptually be one-to-one for an arbitrarily sized integer, but it's OK if your implementation fails for large integers due to numerical limits of types in your preferred language (e.g. C's
As an example of what is not a one-to-one mapping, is simply listing the ternary digits of the integer. In such a system 5 becomes
[1, 2] (because
1*3^1 + 2*3^0 = 5), but it's not one-to-one, because
[0, 1, 2] also means 5 (because
0*3^2 + 1*3^1 + 2*3^0 = 5).
It should be fairly obvious that a mapping is not one-to-one if it skips an integer (e.g. it doesn't work for 5), but I'd like to make it clear that skipping a variable trit array is also not one-to-one. You must map to every possible variable trit array, including
You will be awarded the official Super Smaht® Award™ if your answer includes an algorithm (not necessarily in your golfed program) that not only works for base 2 or 3, but for all bases.
Shortest code in bytes wins.