[zypp-commit] r11678 - in /trunk/sat-solver/bindings: perl/tests/solvables.pl pool.i repo.i satsolver.i
Author: kkaempf Date: Thu Nov 13 13:16:21 2008 New Revision: 11678 URL: http://svn.opensuse.org/viewcvs/zypp?rev=11678&view=rev Log: provide array-like access to all solvables of Repo (or Pool) for iterator-less languages. remove get(-by-index) access, as it will not work in all cases. Added: trunk/sat-solver/bindings/perl/tests/solvables.pl Modified: trunk/sat-solver/bindings/pool.i trunk/sat-solver/bindings/repo.i trunk/sat-solver/bindings/satsolver.i Added: trunk/sat-solver/bindings/perl/tests/solvables.pl URL: http://svn.opensuse.org/viewcvs/zypp/trunk/sat-solver/bindings/perl/tests/solvables.pl?rev=11678&view=auto ============================================================================== --- trunk/sat-solver/bindings/perl/tests/solvables.pl (added) +++ trunk/sat-solver/bindings/perl/tests/solvables.pl Thu Nov 13 13:16:21 2008 @@ -0,0 +1,38 @@ +#!/usr/bin/perl + +# +# solvables.pl +# +# Test iterating over solvables of Pool or Repo +# +# + +use lib '../../../build/bindings/perl'; + +use satsolver; + +# Create Pool and Repository +my $pool = new satsolver::Pool; +$pool->set_arch( $sysarch ); +my $repo = $pool->create_repo('test') || die; + +# Add solvables to Repository +$repo->add_solv ("../../testdata/os11-biarch.solv"); + +# Print how much we have +print "Pool size: " . $pool->size() . ", count " . $pool->count() . "\n"; +print "Repo \"" . $repo->name() . "\" size " . $repo->size() . ", count " . $repo->count() . "\n"; + +my $poolcount = 0; +foreach my $solvable ($pool->solvables()) { + next if not defined $solvable; + $poolcount++; +} + +my $repocount = 0; +foreach my $solvable ($repo->solvables()) { + next if not defined $solvable; + $repocount++; +} + +print "Solvables in pool: " . $poolcount . ", in Repo " . $repocount . "\n"; Modified: trunk/sat-solver/bindings/pool.i URL: http://svn.opensuse.org/viewcvs/zypp/trunk/sat-solver/bindings/pool.i?rev=11678&r1=11677&r2=11678&view=diff ============================================================================== --- trunk/sat-solver/bindings/pool.i (original) +++ trunk/sat-solver/bindings/pool.i Thu Nov 13 13:16:21 2008 @@ -291,7 +291,9 @@ /* number of solvables in pool */ int size() - { return pool_size( $self ); } + { /* skip Ids 0(reserved) and 1(system) */ + return $self->nsolvables - 1 - 1; + } #if defined(SWIGRUBY) %rename( "installable?" ) installable( XSolvable *s ); @@ -301,28 +303,44 @@ int installable( XSolvable *s ) { return pool_installable( $self, pool_id2solvable( s->pool, s->id ) ); } -#if defined(SWIGRUBY) - /* %rename is rejected by swig for [] */ - %alias get "[]"; -#endif - /* - * get solvable by index (0..size-1) - * index is _not_ the internal id, but used as an array index - */ - XSolvable *get( int i ) - { return xsolvable_get( $self, i, NULL ); } + /* return number of iterations when iterating over solvables */ + int count() + { return pool_xsolvables_count( $self ); } #if defined(SWIGRUBY) void each() { pool_xsolvables_iterate( $self, generic_xsolvables_iterate_callback, NULL ); } #endif +/* Nah, thats not for Ruby. Use Repo#each in Ruby */ +#if !defined(SWIGRUBY) + XSolvable **solvables() { + int count = pool_xsolvables_count( $self ); + Solvable *s; + Id p; + int i = 0; + XSolvable **xs = (XSolvable **) malloc((count + 1) * sizeof(XSolvable **)); + + for (p = 2, s = $self->solvables + p; p < $self->nsolvables; p++, s++) + { + if (!s) + continue; + if (!s->name) + continue; + xs[i] = xsolvable_new($self, s - $self->solvables); + ++i; + } + xs[i] = NULL; + + return xs; + } +#endif #if defined(SWIGPYTHON) %pythoncode %{ def __iter__(self): - r = range(0,self.size()) - while r: - yield self.get(r.pop(0)) + s = self.solvables() + while s: + yield s.pop(0) %} #endif Modified: trunk/sat-solver/bindings/repo.i URL: http://svn.opensuse.org/viewcvs/zypp/trunk/sat-solver/bindings/repo.i?rev=11678&r1=11677&r2=11678&view=diff ============================================================================== --- trunk/sat-solver/bindings/repo.i (original) +++ trunk/sat-solver/bindings/repo.i Thu Nov 13 13:16:21 2008 @@ -25,8 +25,8 @@ { return $self->name; } - -int size() + /* see also count() below ! */ + int size() { return $self->nsolvables; } #if defined(SWIGRUBY) %rename("empty?") empty(); @@ -93,26 +93,41 @@ void each() { repo_xsolvables_iterate( $self, generic_xsolvables_iterate_callback, NULL ); } #endif + int count() + { return repo_xsolvables_count( $self ); } + +/* Nah, thats not for Ruby. Use Repo#each in Ruby */ +#if !defined(SWIGRUBY) + XSolvable **solvables() { + int count = repo_xsolvables_count( $self ); + Id p; + Solvable *s; + int i = 0; + XSolvable **xs = (XSolvable **) malloc((count + 1) * sizeof(XSolvable **)); + + FOR_REPO_SOLVABLES($self, p, s) + { + if (!s) + continue; + if (!s->name) + continue; + xs[i] = xsolvable_new($self->pool, s - $self->pool->solvables); + ++i; + } + xs[i] = NULL; + + return xs; + } +#endif #if defined(SWIGPYTHON) %pythoncode %{ def __iter__(self): - r = range(0,self.size()) - while r: - yield self.get(r.pop(0)) + s = self.solvables() + while s: + yield s.pop(0) %} #endif -#if defined(SWIGRUBY) - /* %rename is rejected by swig for [] */ - %alias get "[]"; -#endif - /* - * get xsolvable by index - * FIXME: doesn't work this way, there might be holes ! - */ - XSolvable *get( int i ) - { return xsolvable_get( $self->pool, i, $self ); } - /* * find (best) solvable by name */ Modified: trunk/sat-solver/bindings/satsolver.i URL: http://svn.opensuse.org/viewcvs/zypp/trunk/sat-solver/bindings/satsolver.i?rev=11678&r1=11677&r2=11678&view=diff ============================================================================== --- trunk/sat-solver/bindings/satsolver.i (original) +++ trunk/sat-solver/bindings/satsolver.i Thu Nov 13 13:16:21 2008 @@ -280,6 +280,25 @@ %typemap(in) FILE* { $1 = PerlIO_findFILE(IoIFP(sv_2io($input))); } + +/* + * XSolvable array to Perl + */ +%typemap(out) XSolvable ** { + int n, i; + + for (n = 0; $1[n];) + n++; + + if (n > items) + EXTEND(sp, n - items); + + for (i = 0; i < n; i++) { + ST(argvi) = SWIG_NewPointerObj(SWIG_as_voidptr($1[i]), SWIGTYPE_p__Solvable, SWIG_OWNER | SWIG_SHADOW); + argvi++; + } + free($1); +} #endif typedef int Id; -- To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org For additional commands, e-mail: zypp-commit+help@opensuse.org
participants (1)
-
kkaempf@svn.opensuse.org