[Question]
  [
### Background
The [move-to-front transform](https://en.wikipedia.org/wiki/Move-to-front_transform) (MTF) is a data encoding algorithm designed to improve the performance of entropy encoding techniques.
In the [bzip2 compression algorithm](http://en.wikipedia.org/wiki/Bzip2), it is applied after the [Burrows–Wheeler transform](http://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform) (as seen in [Burrows, Wheeler and Back](https://codegolf.stackexchange.com/questions/51397)), with the objective of turning groups of repeated characters into small, easily compressible non-negative integers.
### Definition
For the purpose of this challenge, we'll define the printable ASCII version of the MTF as follows:
Given an input string **s**, take an empty array **r**, the string **d** of all printable ASCII characters (0x20 to 0x7E) and repeat the following for each character **c** of **s**:
1. Append the index of **c** in **d** to **r**.
2. Move **c** to the front of **d**, i.e., remove **c** from **d** and prepend it to the remainder.
Finally, we take the elements of **r** as indexes in the *original* **d** and fetch the corresponding characters.
### Step-by-step example
```
INPUT: "CODEGOLF"
0. s = "CODEGOLF"
   d = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
   r = []
1. s = "ODEGOLF"
   d = "C !\"#$%&'()*+,-./0123456789:;<=>?@ABDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
   r = [35]
2. s = "DEGOLF"
   d = "OC !\"#$%&'()*+,-./0123456789:;<=>?@ABDEFGHIJKLMNPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
   r = [35 47]
3. s = "EGOLF"
   d = "DOC !\"#$%&'()*+,-./0123456789:;<=>?@ABEFGHIJKLMNPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
   r = [35 47 37]
4. s = "GOLF"
   d = "EDOC !\"#$%&'()*+,-./0123456789:;<=>?@ABFGHIJKLMNPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
   r = [35 47 37 38]
5. s = "OLF"
   d = "GEDOC !\"#$%&'()*+,-./0123456789:;<=>?@ABFHIJKLMNPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
   r = [35 47 37 38 40]
6. s = "LF"
   d = "OGEDC !\"#$%&'()*+,-./0123456789:;<=>?@ABFHIJKLMNPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
   r = [35 47 37 38 40 3]
7. s = "F"
   d = "LOGEDC !\"#$%&'()*+,-./0123456789:;<=>?@ABFHIJKMNPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
   r = [35 47 37 38 40 3 45]
8. s = ""
   d = "FLOGEDC !\"#$%&'()*+,-./0123456789:;<=>?@ABHIJKMNPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
   r = [35 47 37 38 40 3 45 41]
OUTPUT: "COEFH#MI"
```
### Task
Write a program or function that implements the printable ASCII MTF (as defined above).
### Test cases
```
Input:  Programming Puzzles & Code Golf
Output: Prpi"do lp%((uz rnu&3!P/o&$U$(p
Input:  NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN BATMAN!
Output: Na! !! !! !! !! !! !! !! !! !! !! !! !! !! !! !!"DDUP"%'
Input:  Two more questions and I have bzip2 in less than 100 bytes!
Output: Twp#o"si$sv#uvq(u$(l#o#W!r%w+$pz,xF%#,"x(. #0--'$GG ".z(**:
```
### Additional rules
* You cannot use any built-in operator that computes the MTF of a string.
* Your code may print a trailing newline if you choose STDOUT for output.
* Your code has to work for any input of 1000 or less printable ASCII characters (0x20 to 0x7E).
* Standard code golf rules apply. The shortest submission in bytes wins.
  
[Answer]
# CJam, 20
```
'¡,q{_C#c' ,C+@|}fC;
```
[Try it online](http://cjam.aditsu.net/#code='%C2%A1%2Cq%7B_C%23c'%20%2CC%2B%40%7C%7DfC%3B&input=CODEGOLF)
**Explanation:**
```
'¡,      make a string of characters with codes from 0 to 160 (a modified "d")
         could have been to 126 but stackexchange doesn't like the DEL character
q        read the input (s)
{…}fC    for each character C in s
  _      duplicate the d string
  C#     find the index of C in d
  c      convert to character (this is the result)
  ' ,    make a string of characters from 0 to 31
  C+     append C to the string
  @      bring d to the top
  |      set union, preserving order; effectively, C is moved to position 32
         this is the updated d string
;        pop the last d
```
[Answer]
# [Ostrich](https://github.com/KeyboardFire/ostrich-lang), ~~46~~ 45 chars
Don't have a version number in the header because this is actually just [the latest commit](https://github.com/KeyboardFire/ostrich-lang/commit/971773b59bf7c8b8d701adfe2a197e82b9d1f8b7). I added the `O` (ascii code to string) operator after releasing the latest version (but still before this challenge was posted).
```
{a95,{32+O}%:d3@{:x\.3@?3@\+\x-x\+}/;{d=}%s*}
```
Explanation:
```
a             this is the "r" array (a is short for [], empty array)
95,{32+O}%:d  this is the "d" array
3@{...}/      for each character in the input (as an "argument")...
  :x            store in variable x (stack is now [r d c])
  \.3@?         find index in d     (stack is now [r d idx])
  3@\+          append index to r   (stack is now [d modified_r])
  \x-           remove char from d, and then...
  x\+           prepend char to d   (stack is now [modified_r modified_d])
;             throw away modified_d
{d=}%         map r to indices of (original) d
s*            join (s is short for ``, empty string)
```
[Answer]
# SWI-Prolog, ~~239~~ ~~197~~ 189 bytes
```
a(S):-l([126],X),a(S,X,[],R),b(R,X).
a([A|T],X,S,R):-nth0(I,X,A,Z),(a(T,[A|Z],[I|S],R);R=[I|S]).
b([A|T],X):-(b(T,X);!),nth0(A,X,E),put(E).
l([B|R],Z):-A is B-1,X=[A,B|R],(A=32,Z=X;l(X,Z)).
```
Example: `a(`Two more questions and I have bzip2 in less than 100 bytes!`).` outputs:
```
Twp#o"si$sv#uvq(u$(l#o#W!r%w+$pz,xF%#,"x(. #0--'$GG ".z(**:
```
(and `true .` after it, obviously)
**Note:** your SWI-Prolog version has to be one of the newer ones in which the backquote ``` represents codes strings. Code strings used to be represented with double-quotes `"` in older versions.
[Answer]
# Python 3, 88
```
*d,=range(127)
for c in input():y=d.index(ord(c));d[:32]+=d.pop(y),;print(chr(y),end='')
```
Using some ideas from my CJam solution.  
-4 bytes belong to Sp3000 :)
[Answer]
# Python 2, ~~137~~ ~~110~~ 104
Wasn't hard to implement, but *maybe* still golfable?
[**Try it here**](http://ideone.com/cJJdh2)
```
e=d=map(chr,range(32,127))
r=""
for c in raw_input():n=e.index(c);r+=d[n];e=[e[n]]+e[:n]+e[n+1:]
print r
```
[Answer]
# [Stax](https://github.com/tomtheisen/stax), ~~18 15 14~~ 13 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
ä╦@╥◄/J[;&πi⌡
```
[Run and debug it](https://staxlang.xyz/#p=84cb40d2112f4a5b3b26e369f5&i=%22CODEGOLF%22%0A%22Programming+Puzzles+%26+Code+Golf%22%0A%22NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN+BATMAN%21%22%0A%22Two+more+questions+and+I+have+bzip2+in+less+than+100+bytes%21%22&m=2)
-3 bytes using a map, and a simpler algorithm.
-1 byte borrowing from Kevin Cruijssen's answer.
-1 byte by simplifying a stack operation.
[Answer]
# [Perl 5](https://www.perl.org/), 96 bytes
```
sub{$d=$o=join'',map chr,32..126;join'',map{$d=~s/\Q$_//;$d=$_.$d;substr$o,length$`,1}pop=~/./g}
```
[Try it online!](https://tio.run/##lY/NbtpAFIX3PMX1eMCD69hA1Cyw3DYNgUZNjCsRdVEiaurBuLFnhhkbiCOyzAPkEfMi1LhIWUe6m/udc@6PoDL9uMcLb6@K@SOOPMy9vzxhhmFloYA/S2md9my72ztz3/DB96Sc6Q88cxz3EJrZOHKrCSqXmFspZXG@xL@t7k5w4T05thPv9m5jwSVpAPxCF@PB5Wh8PUTgfYKquxx@02@u0J1Vq4HksQyzLGExBEVZplRBCy54RGHE00UdMgIpEhRxSEWTkKIEyYrWqRY4vIVvMRHGcZYf@u8q@Ho@uTn3tXrJivihBto7Cg0GtwFqGu3j@smGQ8YlhVVBVZ5wpiBkEVzBMlxTmJeJ6EHCoPpQQb4MGXQ7HZg/5FQdL3AmG6FzpBKs1nqxXpECk1Tn@k9NNjcfsCit7bCpW2hLbNA7JycGHo0A2SUxzb5z12g/VmdkDwQnTFiYbkXb@4Jnbg0BxzwHD1p4UevtAxYyYfl/ha7gkIDPgPg9vD6/1HjKEPQBmabpjycw/q5NWUX7R63y9@vUlFVGt7Hb/wM "Perl 5 – Try It Online")
Ungolfed with some explanation:
```
sub{
  $d=$o=join'',map chr,32..126;  #init strings d and o to chars between 0x32-0x7e
  join('',                       #join array of chars to string
    map {                        #map array of input chars
      $d=~s/\Q$_//;              #pick out current input char from d, \Q to avoid
                                 #...special regexp meaning of some chars
      $d=$_.$d;                  #prepend current input char to d
      substr$o,length$`,1        #map to char in original dict o at position of
                                 #...current input char in d
    }
    pop=~/./g                    #array of input chars
  )                              #return joined chars
}
```
The `\Q` isn't needed for the given test cases since they don't contain `.` (period) or other chars with special meaning in regular expressions. If they did, `\Q` is needed.
[Answer]
# Pyth, 24 bytes
```
JK>95CM127s@LKxL~J+d-Jdz
```
[Demonstration.](https://pyth.herokuapp.com/?code=JK%3E95CM127s%40LKxL~J%2Bd-Jdz&input=CODEGOLF&debug=0) [Test Harness.](https://pyth.herokuapp.com/?code=Fz%2Bz.zJK%3E95CM127s%40LKxL~J%2Bd-Jdz&input=Programming%20Puzzles%20%26%20Code%20Golf%0ANaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN%20BATMAN!%0ATwo%20more%20questions%20and%20I%20have%20bzip2%20in%20less%20than%20100%20bytes!&debug=0)
The first bit. `JK>95CM127` sets up the necessary list and saves it to `J` and `K`. `~J+d-Jd` performs the list updating, while `xL ... z` maps the input characters to their positions in the list. Finally, `s@LK` converts those indexes to characters in the original list.
[Answer]
# Haskell, 120 bytes
```
e#s=[b|(b,a)<-zip[0..]s,a==e]!!0
a=[' '..'~']
f=snd.foldl(\(d,r)e->(e:take(e#d)d++tail(drop(e#d)d),r++[a!!(e#d)]))(a,[])
```
Usage example: `f "CODEGOLF"` -> `"COEFH#MI"`
How it works: `#` is an index function that returns the position of `e` in `s` (can't use Haskell's native `elemIndex` because of an expensive `import`). The main function `f` follows a fold pattern where it updates the position string `d` and result string `r` as it walks through the input string. 
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 16 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
žQDUIεXskXyìÙU}è
```
Output as a list of characters.
[Try it online](https://tio.run/##ATEAzv9vc2FiaWX//8W@UURVSc61WHNrWHnDrMOZVX3DqP//Q09ERUdPTEb/LS1uby1sYXp5) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKS/f@j@wJdQivPbY0ozo6oPLzm8MzQ2sMr/nvp/I9WcvZ3cXX393FT0lEKKMpPL0rMzc3MS1cIKK2qykktVlBTcM5PSVVwz89JA6rwS/QjCSk4OYb4OvopArWGlOcr5OYXpSoUlqYWl2Tm5xUrJOalKHgqZCSWpSokVWUWGClk5ikA7SxWKMlIzFMwNDBQSKosSS1WVIr9r6ubl6@bk1hVCQA).
**Explanation:**
```
žQ           # Push the printable ASCII constant string
  DU         # Duplicate it, and pop and store this copy in variable `X`
    Iε       # Map over the characters of the input:
      Xsk    #  Get the index of the current character in `X`
      Xyì    #  Then prepend the current character to `X`
         Ù   #  Uniquify the characters in this new string
          U  #  Pop and store it as new `X`
     }è      # After the map: index the mapped integers into the printable ASCII string
             # (after which the resulting character list is output implicitly)
```
]