[Pyrex] python dictionaries with unsigned long long keys and values

Sam Tannous stannous at gmail.com
Wed Mar 14 15:41:13 UTC 2007


Hello,

I'm a bit new to pyrex and am not too clear about how dictionaries
are handled.  I'm using a number of python dictionaries where the
indices (keys) and the values are 64 bit numbers (from 0 to (1<<64) - 1).
(i.e. unsigned long long...U64).
While the arrays contain (and are indexed by) U64 numbers, they do not
contain that many values (no more then 64x256 entries).

I'd like to move the most CPU intensive portions of my code to pyrex
modules but I'm not quite sure how to do this.  I know that one can create
unsigned long long variables, but how should the dictionaries be declared
and created?
And where are they declared and created?  In python or the pyrex functions?
>From what
I can gather, any access to a dictionary (whether created in python or pyrex
code) slows everything down to pure python speeds.

This is a small sample of what I'm trying to do:
Most of the time is spent in functions like get_move
and these functions need to access dictionaries made up of
these 64 bit numbers.

Any help would be appreciated.

Thanks,
Sam

---------------------------
Python
=====
rank = {}
rank_mask = {}
count = 0
rank[0] = 0
rank_mask[0] = 0
for i in range(1,9):
    for j in range(8,0,-1):
        rank[1L<<count]      = i
        rank_mask[1L<<count] = (255L<<8*(i-1))
        count = count + 1

rank_attacks = {}   # long complicated definition...
rank_attacks[1<<45] = {}
rank_attacks[1<<45][0] = 0


piece = 1<<45
all_pieces = 1<<35 | 1<<33
other_pieces = 1<<62

print sample1.get_move(piece,all_pieces,other_pieces,
                       rank_mask,rank_attacks)


Pyrex
====
def get_move(unsigned long long piece, unsigned long long all_pieces,
             unsigned long long other_pieces, rank_mask, rank_attacks):

    cdef unsigned long long from_square
    cdef unsigned long long rank_pieces
    cdef unsigned long long to_square

    mymoves = []

    while (piece):
        from_square = piece & -piece
        # handle the rank attacks
        rank_pieces = rank_mask[from_square] & all_pieces
        # this produces all moves including our own piece attacks
        moves = (rank_attacks[from_square][rank_pieces] & other_pieces) | \
                (rank_attacks[from_square][rank_pieces] & ~all_pieces)


        while(moves):
            to_square = moves & -moves
            mymoves.append[(from_square,to_square)]
            moves = moves & (moves - 1)

        piece = piece & (piece - 1)

    return moves


---------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.copyleft.no/pipermail/pyrex/attachments/20070314/ec749a39/attachment.html 


More information about the Pyrex mailing list