<div dir="ltr">Yes, you can incorporate this code into Sage if you like.<br><br>Alex<br><br><div class="gmail_quote">On Wed, Aug 13, 2008 at 2:44 AM, William Stein <span dir="ltr">&lt;<a href="mailto:wstein@gmail.com">wstein@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="Ih2E3d">On Wed, Aug 13, 2008 at 2:25 AM, Alex Mendes da Costa<br>
&lt;<a href="mailto:alexmdac@gmail.com">alexmdac@gmail.com</a>&gt; wrote:<br>
&gt; Hi,<br>
&gt;<br>
&gt; I&#39;m using cython to wrap DJB&#39;s primegen library<br>
&gt; (<a href="http://cr.yp.to/primegen.html" target="_blank">http://cr.yp.to/primegen.html</a>).<br>
<br>
</div>Are you going to contribute this code and the wrapper<br>
to Sage (<a href="http://sagemath.org" target="_blank">http://sagemath.org</a>)? &nbsp;I&#39;ve wanted to add<br>
primegen to Sage for a long time -- in fact, I asked<br>
DJB about its license on a panel discussion and<br>
in his response he released primegen under a public<br>
domain license.<br>
<br>
&nbsp;-- William<br>
<div><div></div><div class="Wj3C7c"><br>
&gt;<br>
&gt; Here&#39;s my pyx file:<br>
&gt;<br>
&gt; cdef extern from &quot;primegen.h&quot;:<br>
&gt; &nbsp; &nbsp; ctypedef unsigned long uint32<br>
&gt; &nbsp; &nbsp; ctypedef unsigned long long uint64<br>
&gt;<br>
&gt; &nbsp; &nbsp; ctypedef struct primegen:<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; uint32 buf[16][2048]<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; uint64 p[512]<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; int num<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; int pos<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; uint64 base<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; uint64 L<br>
&gt;<br>
&gt; &nbsp; &nbsp; void primegen_init(primegen* pg)<br>
&gt; &nbsp; &nbsp; uint64 primegen_next(primegen* pg)<br>
&gt; &nbsp; &nbsp; uint64 primegen_peek(primegen* pg)<br>
&gt; &nbsp; &nbsp; uint64 primegen_count(primegen* pg, uint64 to)<br>
&gt; &nbsp; &nbsp; void primegen_skipto(primegen* pg, uint64 to)<br>
&gt;<br>
&gt; cdef class PrimeGen:<br>
&gt; &nbsp; &nbsp; cdef primegen pg<br>
&gt;<br>
&gt; &nbsp; &nbsp; def __new__(self):<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; primegen_init(&amp;<a href="http://self.pg" target="_blank">self.pg</a>)<br>
&gt;<br>
&gt; &nbsp; &nbsp; def next(self):<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; return primegen_next(&amp;<a href="http://self.pg" target="_blank">self.pg</a>)<br>
&gt;<br>
&gt; &nbsp; &nbsp; def peek(self):<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; return primegen_peek(&amp;<a href="http://self.pg" target="_blank">self.pg</a>)<br>
&gt;<br>
&gt; &nbsp; &nbsp; def count(self, uint64 to):<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; return primegen_count(&amp;<a href="http://self.pg" target="_blank">self.pg</a>, to)<br>
&gt;<br>
&gt; &nbsp; &nbsp; def skipto(self, uint64 to):<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; primegen_skipto(&amp;<a href="http://self.pg" target="_blank">self.pg</a>, to)<br>
&gt;<br>
&gt; Notice that the class has a method called &quot;next&quot;. &nbsp;This means that it should<br>
&gt; be usable as an iterator. &nbsp;Here&#39;s what happens if I try to do that:<br>
&gt;<br>
&gt; Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)<br>
&gt; [GCC 4.0.1 (Apple Inc. build 5465)] on darwin<br>
&gt; Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.<br>
&gt;&gt;&gt;&gt; from primegen import PrimeGen<br>
&gt;&gt;&gt;&gt; class C:<br>
&gt; ... &nbsp; def __iter__(self):<br>
&gt; ... &nbsp; &nbsp; return PrimeGen()<br>
&gt; ...<br>
&gt;&gt;&gt;&gt; for i in C():<br>
&gt; ... &nbsp; print i<br>
&gt; ...<br>
&gt; Traceback (most recent call last):<br>
&gt; &nbsp; File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;<br>
&gt; TypeError: __iter__ returned non-iterator of type &#39;primegen.PrimeGen&#39;<br>
&gt;<br>
&gt; How come this doesn&#39;t work? &nbsp;The generated class definitely has the &quot;next&quot;<br>
&gt; method required by the iterator protocol.<br>
&gt;<br>
&gt;&gt;&gt;&gt; p = PrimeGen()<br>
&gt;&gt;&gt;&gt; dir(p)<br>
&gt; [&#39;__class__&#39;, &#39;__delattr__&#39;, &#39;__doc__&#39;, &#39;__getattribute__&#39;, &#39;__hash__&#39;,<br>
&gt; &#39;__init__&#39;, &#39;__new__&#39;, &#39;__reduce__&#39;, &#39;__reduce_ex__&#39;, &#39;__repr__&#39;,<br>
&gt; &#39;__setattr__&#39;, &#39;__str__&#39;, &#39;count&#39;, &#39;next&#39;, &#39;peek&#39;, &#39;skipto&#39;]<br>
&gt;<br>
&gt; Thanks,<br>
&gt; Alex<br>
</div></div><div class="Ih2E3d">&gt; _______________________________________________<br>
&gt; Cython-dev mailing list<br>
&gt; <a href="mailto:Cython-dev@codespeak.net">Cython-dev@codespeak.net</a><br>
&gt; <a href="http://codespeak.net/mailman/listinfo/cython-dev" target="_blank">http://codespeak.net/mailman/listinfo/cython-dev</a><br>
&gt;<br>
&gt;<br>
<br>
<br>
<br>
</div><font color="#888888">--<br>
William Stein<br>
Associate Professor of Mathematics<br>
University of Washington<br>
<a href="http://wstein.org" target="_blank">http://wstein.org</a><br>
</font><div><div></div><div class="Wj3C7c">_______________________________________________<br>
Cython-dev mailing list<br>
<a href="mailto:Cython-dev@codespeak.net">Cython-dev@codespeak.net</a><br>
<a href="http://codespeak.net/mailman/listinfo/cython-dev" target="_blank">http://codespeak.net/mailman/listinfo/cython-dev</a><br>
</div></div></blockquote></div><br></div>