<commit_before>#!/usr/bin/python

cimport cython
import numpy
cimport numpy


def inside_polygon(vertices, point, border_value=True):
    """
    Return True/False is a pixel is inside a polygon.

    @param vertices:
    @param point: 2-tuple of integers or list
    @param border_value: boolean
    """
    counter = 0
    for i,polypoint1 in enumerate(vertices):
        if (polypoint1[0] == point[0]) and (polypoint1[1] == point[1]):
            return border_value
        polypoint2 = vertices[(i+1)%len(vertices)]
        if (point[1] > min(polypoint1[1], polypoint2[1])):
            if (point[1] <= max(polypoint1[1], polypoint2[1])):
                if (point[0] <= max(polypoint1[0], polypoint2[0])):
                    if (polypoint1[1] != polypoint2[1]):
                        xinters = (point[1]-polypoint1[1])*(polypoint2[0]-polypoint1[0])/(polypoint2[1]-polypoint1[1])+polypoint1[0]
                        if (polypoint1[0] == polypoint2[0]) or (point[0] <= xinters):
                            counter+=1
    if counter % 2 == 0:
        return False
    else:
        return True

def inside_polygon_np(vertices, point, border_value=True):
    """
    Return True/False is a pixel is inside a polygon.

    @param vertices: numpy ndarray Nx2
    @param point: 2-tuple of integers or list
    @param border_value: boolean

    Numpy implementation + Cython optimization
    """
    cdef int counter, i, nvert
    cdef float px, py, polypoint1x, polypoint1y, polypoint2x, polypoint2y, xinters
    cdef float[:,:] c_vertices =  numpy.ascontiguousarray(vertices, dtype=numpy.float32)
    counter = 0
    px, py = point[0], point[1]
    nvert = vertices.shape[0]
    polypoint1x, polypoint1y = c_vertices[nvert-1, 0],c_vertices[nvert-1, 1]
    for i in range(nvert):
        if (polypoint1x == px) and (polypoint1y == py):
            return border_value
        polypoint2x, polypoint2y = c_vertices[i, 0],c_vertices[i, 1]
        if (py > min(polypoint1y, polypoint2y)):
            if (py <= max(polypoint1y, polypoint2y)):
                if (px <= max(polypoint1x, polypoint2x)):
                    if (polypoint1y != polypoint2y):
                        xinters = (py - polypoint1y) * (polypoint2x - polypoint1x) / (polypoint2y - polypoint1y) + polypoint1x
                        if (polypoint1x == polypoint2x) or (px <= xinters):
                            counter += 1
        polypoint1x, polypoint1y = polypoint2x, polypoint2y
    if counter % 2 == 0:
        return False
    else:
        return True

cdef class Polygon(object):
    cdef float[:,:] vertices
    cdef int nvert
    def __init__(self, vertices):
        """
        @param vertices: Nx2 array of floats
        """
        self.vertices = numpy.ascontiguousarray(vertices, dtype=numpy.float32)
        self.nvert = vertices.shape[0]

    @cython.cdivision(True)
    @cython.wraparound(False)
    @cython.boundscheck(False)
    cpdef bint isInside(self, float px, float py, bint border_value=True):
        """
        Pure C_Cython class implementation
        """
        cdef int counter, i
        cdef float polypoint1x, polypoint1y, polypoint2x, polypoint2y, xinters
        counter = 0

        polypoint1x, polypoint1y = self.vertices[self.nvert-1, 0],self.vertices[self.nvert-1, 1]
        for i in range(self.nvert):
            if (polypoint1x == px) and (polypoint1y == py):
                return border_value
            polypoint2x, polypoint2y = self.vertices[i, 0],self.vertices[i, 1]
            if (py > min(polypoint1y, polypoint2y)):
                if (py <= max(polypoint1y, polypoint2y)):
                    if (px <= max(polypoint1x, polypoint2x)):
                        if (polypoint1y != polypoint2y):
                            xinters = (py - polypoint1y) * (polypoint2x - polypoint1x) / (polypoint2y - polypoint1y) + polypoint1x
                            if (polypoint1x == polypoint2x) or (px <= xinters):<commit_msg>packing & depacking tuples is not efficient<commit_after>#!/usr/bin/python

cimport cython
import numpy
cimport numpy


def inside_polygon(vertices, point, border_value=True):
    """
    Return True/False is a pixel is inside a polygon.

    @param vertices:
    @param point: 2-tuple of integers or list
    @param border_value: boolean
    """
    counter = 0
    for i,polypoint1 in enumerate(vertices):
        if (polypoint1[0] == point[0]) and (polypoint1[1] == point[1]):
            return border_value
        polypoint2 = vertices[(i+1)%len(vertices)]
        if (point[1] > min(polypoint1[1], polypoint2[1])):
            if (point[1] <= max(polypoint1[1], polypoint2[1])):
                if (point[0] <= max(polypoint1[0], polypoint2[0])):
                    if (polypoint1[1] != polypoint2[1]):
                        xinters = (point[1]-polypoint1[1])*(polypoint2[0]-polypoint1[0])/(polypoint2[1]-polypoint1[1])+polypoint1[0]
                        if (polypoint1[0] == polypoint2[0]) or (point[0] <= xinters):
                            counter+=1
    if counter % 2 == 0:
        return False
    else:
        return True

def inside_polygon_np(vertices, point, border_value=True):
    """
    Return True/False is a pixel is inside a polygon.

    @param vertices: numpy ndarray Nx2
    @param point: 2-tuple of integers or list
    @param border_value: boolean

    Numpy implementation + Cython optimization
    """
    cdef int counter, i, nvert
    cdef float px, py, polypoint1x, polypoint1y, polypoint2x, polypoint2y, xinters
    cdef float[:,:] c_vertices =  numpy.ascontiguousarray(vertices, dtype=numpy.float32)
    counter = 0
    px, py = point[0], point[1]
    nvert = vertices.shape[0]
    polypoint1x, polypoint1y = c_vertices[nvert-1, 0],c_vertices[nvert-1, 1]
    for i in range(nvert):
        if (polypoint1x == px) and (polypoint1y == py):
            return border_value
        polypoint2x, polypoint2y = c_vertices[i, 0],c_vertices[i, 1]
        if (py > min(polypoint1y, polypoint2y)):
            if (py <= max(polypoint1y, polypoint2y)):
                if (px <= max(polypoint1x, polypoint2x)):
                    if (polypoint1y != polypoint2y):
                        xinters = (py - polypoint1y) * (polypoint2x - polypoint1x) / (polypoint2y - polypoint1y) + polypoint1x
                        if (polypoint1x == polypoint2x) or (px <= xinters):
                            counter += 1
        polypoint1x, polypoint1y = polypoint2x, polypoint2y
    if counter % 2 == 0:
        return False
    else:
        return True

cdef class Polygon(object):
    cdef float[:,:] vertices
    cdef int nvert
    def __init__(self, vertices):
        """
        @param vertices: Nx2 array of floats
        """
        self.vertices = numpy.ascontiguousarray(vertices, dtype=numpy.float32)
        self.nvert = vertices.shape[0]

    @cython.cdivision(True)
    @cython.wraparound(False)
    @cython.boundscheck(False)
    cpdef bint isInside(self, float px, float py, bint border_value=True):
        """
        Pure C_Cython class implementation
        """
        cdef int counter, i
        cdef float polypoint1x, polypoint1y, polypoint2x, polypoint2y, xinters
        counter = 0

        polypoint1x = self.vertices[self.nvert-1, 0]
        polypoint1y = self.vertices[self.nvert-1, 1]
        for i in range(self.nvert):
            if (polypoint1x == px) and (polypoint1y == py):
                return border_value
            polypoint2x = self.vertices[i, 0]
            polypoint2y = self.vertices[i, 1]
            if (py > min(polypoint1y, polypoint2y)):
                if (py <= max(polypoint1y, polypoint2y)):
                    if (px <= max(polypoint1x, polypoint2x)):
                        if (polypoint1y != polypoint2y):
                            xinters = (py - polypoint1y) * (polypoint2x - polypoint1x) / (polypoint2y - polypoint1y) + polypoint1x
                            if (polypoint1x == polypoint2x) or (px <= xinters):
{'fraction_non_alphanumeric': 0.11494974874371859, 'fraction_numerical': 0.03391959798994975, 'mean_word_length': 2.2828865979381443, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2944298', 'n_tokens_mistral': 2825, 'n_tokens_neox': 2659, 'n_words': 624}