Hash tables, anyone?



I do not know if I am answering your question, but how about this...

f[n,w]:=0;

g(n,w,z) :=    f[n,w]:  f[n,w]+z

then you could  replace  terms of the form  z*exp(%i*w)*x^n    by   
g(n,w,z).