Optimize temporal memory

Fixes https://github.com/numenta/nupic/issues/1410.