Source

neglect / neglect / scripts / make_swizzle_includes.py

import os
import time
from itertools import product

vec_sets = {
    2:  ["xy", "rg", "st"],
    3:  ["xyz", "rgb", "stp"],
    4:  ["xyzw", "rgba", "stpq"]
}

def swizzle_permutations(set):
    for x in xrange(2, 5):
        for p in product(set, repeat=x):
            yield x, ''.join(p)


os.chdir((os.path.dirname(__file__) or '.') + '/../cpplib/include/neglect')

for vector_size, sets in vec_sets.iteritems():
    with open('_vector%d_swizzle.inc' % vector_size, 'w') as f:
        f.write('// this file was automatically generated by\n'
                '// %s on %s.  DO NOT EDIT\n\n' %
                (os.path.basename(__file__), time.ctime()))
        for set in sets:
            for result_size, name in swizzle_permutations(set):
                if name == set:
                    f.write('const vector<T, %d> &%s() const { return *this; }\n' % (
                        result_size,
                        name
                    ))
                else:
                    f.write('vector<T, %d> %s() const { return vector<T, %d>(%s); }\n' % (
                        result_size,
                        name,
                        result_size,
                        ', '.join(name)
                    ))