[stringtemplate-interest] how to fix longstanding bug

Terence Parr parrt at cs.usfca.edu
Tue Jul 25 09:23:10 PDT 2006


On Jul 25, 2006, at 1:18 AM, Oliver Flege wrote:

> Hi,
>
> Kunle Odutola wrote:
>>> Suggestions?  I'd really like at least the iterators I create to be
>>> "reusable" within same template.
>>
>> Only sure way is to wrap *all* incoming iterators in a collection  
>> of some
>> sort. A bit like RestCollection does. Heavy-ish overhead unless,  
>> it is
>> possible to detect in advance if an iterator is indeed used  
>> repeatedly.
>>
>> C# also has the option of simply Reset()-ing iterators but that  
>> breaks with
>> "positioned iterators" (i.e. iterators deliberately positioned at  
>> a specific
>> point in the underlying collection).
>
> in my local (Java) ST code, I fixed this issue by introducing an  
> STIterator class
> in the StringTemplate class (see below).
> That class is used to wrap any iterator that is either set with  
> setAttribute or
> that is created during template evaluation; its reset() method is  
> called whenever
> a local attribute is retrieved in StringTemplate#get that is-a  
> STIterator, thus
> referencing an iterable object more than once always yields a  
> "fresh" iterator.
>
> The cost for copying all elements into a private list upon  
> construction
> is (at least for me) negligible:
> On a 2GHz P4 machine, I can create 1000 STIterators with 100  
> elements each in about 10ms.

Well, ANTLR is noticing a slow down now for building big iterated  
arrays of elements.  I need to be careful not to slow everything down  
just to handle this case.  Damn!  Hm....

Nice solution for your needs though!  Good work.

Ter


More information about the stringtemplate-interest mailing list