def count_alleles(self, max_allele=None, subpop=None):
        """Count the number of calls of each allele per variant.

        Parameters
        ----------
        max_allele : int, optional
            The highest allele index to count. Alleles greater than this
            index will be ignored.
        subpop : array_like, int, optional
            Indices of haplotypes to include.

        Returns
        -------
        ac : AlleleCountsArray, int, shape (n_variants, n_alleles)

        Examples
        --------

        >>> import allel
        >>> h = allel.HaplotypeArray([[0, 0, 0, 1],
        ...                           [0, 1, 1, 1],
        ...                           [0, 2, -1, -1]], dtype='i1')
        >>> ac = h.count_alleles()
        >>> ac
        <AlleleCountsArray shape=(3, 3) dtype=int32>
        3 1 0
        1 3 0
        1 0 1

        """

        # check inputs
        subpop = _normalize_subpop_arg(subpop, self.shape[1])

        # determine alleles to count
        if max_allele is None:
            max_allele = self.max()

        # use optimisations
        values = memoryview_safe(self.values)
        if subpop is None:
            ac = haplotype_array_count_alleles(values, max_allele)

        else:
            ac = haplotype_array_count_alleles_subpop(values, max_allele, subpop)

        return AlleleCountsArray(ac, copy=False)