Skip to content

Commit

Permalink
Trac #28532: fix a bunch of warnings
Browse files Browse the repository at this point in the history
In this ticket, we fix most of the compilation warnings that can be seen
while running `./sage -b` after touching
`sage/data_structures/bitset.pxi`.

URL: https://trac.sagemath.org/28532
Reported by: dcoudert
Ticket author(s): David Coudert
Reviewer(s): Dima Pasechnik
  • Loading branch information
Release Manager committed Oct 10, 2019
2 parents 1ff91fe + 2472ab1 commit 90948f4
Show file tree
Hide file tree
Showing 21 changed files with 77 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/sage/coding/binary_code.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4028,7 +4028,7 @@ cdef class BinaryCodeClassifier:
j += 1

log_2_radix = 0
while ((<codeword>1) << log_2_radix) < self.radix:
while ((<codeword>1) << log_2_radix) < <codeword>self.radix:
log_2_radix += 1
# now we assume (<codeword>1 << log_2_radix) == self.radix
if k < log_2_radix:
Expand Down
8 changes: 4 additions & 4 deletions src/sage/combinat/words/word_char.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ cdef class WordDatatype_char(WordDatatype):
cdef int res = 5381
cdef size_t i
if self._hash is None:
for i in range(min(1024,self._length)):
for i in range(min(<size_t>1024, self._length)):
res = ((res << 5) + res) + self._data[i]
self._hash = res
return self._hash
Expand Down Expand Up @@ -385,7 +385,7 @@ cdef class WordDatatype_char(WordDatatype):
i = key # cast key into a size_t
if i < 0:
i += self._length;
if i < 0 or i >= self._length:
if i < 0 or <size_t>i >= self._length:
raise IndexError("word index out of range")
return self._data[i]
Expand Down Expand Up @@ -568,7 +568,7 @@ cdef class WordDatatype_char(WordDatatype):
# now consider non trivial powers
if w._length > SIZE_T_MAX / (i+1):
raise OverflowError("the length of the result is too large")
cdef size_t new_length = w._length * i + rest
cdef Py_ssize_t new_length = w._length * i + rest
cdef unsigned char * data = <unsigned char *>check_allocarray(new_length, sizeof(unsigned char))
cdef Py_ssize_t j = w._length
Expand Down Expand Up @@ -619,7 +619,7 @@ cdef class WordDatatype_char(WordDatatype):
sage: w.has_prefix([0,1,0])
True
"""
cdef size_t i
cdef Py_ssize_t i
cdef WordDatatype_char w
if isinstance(other, WordDatatype_char):
Expand Down
4 changes: 2 additions & 2 deletions src/sage/crypto/boolean_function.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -835,13 +835,13 @@ cdef class BooleanFunction(SageObject):
sage: B.correlation_immunity()
2
"""
cdef int c
cdef size_t c
if self._correlation_immunity is None:
c = self._nvariables
W = self.walsh_hadamard_transform()
for 0 < i < len(W):
sig_check()
if (W[i] != 0):
if W[i]:
c = min( c , hamming_weight_int(i) )
self._correlation_immunity = ZZ(c-1)
return self._correlation_immunity
Expand Down
4 changes: 2 additions & 2 deletions src/sage/data_structures/bitset.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ cdef class FrozenBitset:
raise ValueError("Bitsets must not be empty")
if capacity is None:
bitset_realloc(self._bitset, len(iter))
elif self._bitset.size != len(iter):
elif self._bitset.size != <mp_bitcnt_t>len(iter):
raise ValueError("bitset capacity does not match passed string")
bitset_from_str(self._bitset, iter)
else: # an iterable
Expand Down Expand Up @@ -497,7 +497,7 @@ cdef class FrozenBitset:
98
"""
cdef FrozenBitset temp
if self._bitset.size >= capacity:
if self._bitset.size >= <mp_bitcnt_t>capacity:
return self
else:
temp = self._new(self._bitset.size)
Expand Down
4 changes: 2 additions & 2 deletions src/sage/graphs/asteroidal_triples.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def is_asteroidal_triple_free(G, certificate=False):
if not isinstance(G, Graph):
raise ValueError("The first parameter must be a Graph.")

cdef int n = G.order()
cdef uint32_t n = <uint32_t>G.order()
cdef int i

# ==> Trivial cases
Expand Down Expand Up @@ -180,7 +180,7 @@ def is_asteroidal_triple_free(G, certificate=False):
return False if ret else True


cdef list is_asteroidal_triple_free_C(int n,
cdef list is_asteroidal_triple_free_C(uint32_t n,
short_digraph sd,
uint32_t** connected_structure,
uint32_t* waiting_list,
Expand Down
4 changes: 2 additions & 2 deletions src/sage/graphs/base/c_graph.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ from sage.data_structures.bitset cimport bitset_t
from .graph_backends cimport GenericGraphBackend

cdef class CGraph:
cdef int num_verts
cdef int num_arcs
cdef size_t num_verts
cdef size_t num_arcs
cdef int *in_degrees
cdef int *out_degrees

Expand Down
16 changes: 8 additions & 8 deletions src/sage/graphs/base/c_graph.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ cdef class CGraph:
False
"""
return (n >= 0 and
n < self.active_vertices.size and
<mp_bitcnt_t>n < self.active_vertices.size and
bitset_in(self.active_vertices, n))

cpdef check_vertex(self, int n):
Expand Down Expand Up @@ -207,7 +207,7 @@ cdef class CGraph:
"""
if k == -1:
k = bitset_first_in_complement(self.active_vertices)
elif self.active_vertices.size <= k:
elif self.active_vertices.size <= <mp_bitcnt_t>k:
k = -1
if k != -1:
if not bitset_in(self.active_vertices, k):
Expand Down Expand Up @@ -323,7 +323,7 @@ cdef class CGraph:
"requested vertex is past twice the allocated range: "
"use realloc")
if (k >= <int>self.active_vertices.size or
(k == -1 and self.active_vertices.size == self.num_verts)):
(k == -1 and self.active_vertices.size == <mp_bitcnt_t>self.num_verts)):
self.realloc(2 * self.active_vertices.size)
return self.add_vertex_unsafe(k)

Expand Down Expand Up @@ -607,7 +607,7 @@ cdef class CGraph:
[1, 2]
"""
cdef int i
return [i for i in range(self.active_vertices.size)
return [i for i in range(<int>self.active_vertices.size)
if bitset_in(self.active_vertices, i)]

cpdef realloc(self, int total):
Expand Down Expand Up @@ -1113,7 +1113,7 @@ cdef class CGraphBackend(GenericGraphBackend):
u_long = pyobject_to_long(u)
except Exception:
return -1
if u_long < 0 or u_long >= G.active_vertices.size or u_long in vertex_labels:
if u_long < 0 or u_long >= <long>G.active_vertices.size or u_long in vertex_labels:
return -1
return u_long

Expand Down Expand Up @@ -1744,7 +1744,7 @@ cdef class CGraphBackend(GenericGraphBackend):
sage: list(P._backend.iterator_verts([1, 2, 10]))
[1, 2]
"""
cdef long i
cdef size_t i
if verts is None:
for x in self.vertex_ints:
yield x
Expand Down Expand Up @@ -3055,7 +3055,7 @@ cdef class CGraphBackend(GenericGraphBackend):
if v_int == -1:
return True
v = self.vertex_label(v_int)
cdef int n = 0
cdef size_t n = 0
for _ in self.depth_first_search(v, ignore_direction=True):
n += 1
return n == cg.num_verts
Expand Down Expand Up @@ -3089,7 +3089,7 @@ cdef class CGraphBackend(GenericGraphBackend):

v = self.vertex_label(v_int)

cdef int n = 0
cdef size_t n = 0
for _ in self.depth_first_search(v):
n += 1
if cg.num_verts != n:
Expand Down
2 changes: 1 addition & 1 deletion src/sage/graphs/base/dense_graph.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ from .c_graph cimport CGraph
cdef class DenseGraph(CGraph):
cdef int radix_div_shift
cdef int radix_mod_mask
cdef int num_longs
cdef size_t num_longs
cdef unsigned long *edges
14 changes: 8 additions & 6 deletions src/sage/graphs/base/dense_graph.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ cdef class DenseGraph(CGraph):

cdef bitset_t bits
cdef int min_verts, min_longs, old_longs = self.num_longs
if total_verts < self.active_vertices.size:
if <size_t>total_verts < self.active_vertices.size:
min_verts = total_verts
min_longs = -1
bitset_init(bits, self.active_vertices.size)
Expand Down Expand Up @@ -372,9 +372,9 @@ cdef class DenseGraph(CGraph):
True
"""
if u < 0 or u >= self.active_vertices.size or not bitset_in(self.active_vertices, u):
if u < 0 or u >= <int>self.active_vertices.size or not bitset_in(self.active_vertices, u):
return False
if v < 0 or v >= self.active_vertices.size or not bitset_in(self.active_vertices, v):
if v < 0 or v >= <int>self.active_vertices.size or not bitset_in(self.active_vertices, v):
return False
return self.has_arc_unsafe(u, v) == 1

Expand Down Expand Up @@ -451,7 +451,7 @@ cdef class DenseGraph(CGraph):
cdef unsigned long * active_vertices_bitset
active_vertices_bitset = <unsigned long *> self.active_vertices.bits

cdef int i, j
cdef size_t i, j
for i in range(self.active_vertices.size):
if bitset_in(self.active_vertices, i):
self.add_arc_unsafe(i, i)
Expand Down Expand Up @@ -494,7 +494,8 @@ cdef class DenseGraph(CGraph):
"""
cdef int place = (u * self.num_longs)
cdef int num_nbrs = 0
cdef int i, v = 0
cdef size_t i
cdef int v = 0
cdef unsigned long word, data
for i in range(self.num_longs):
data = self.edges[place + i]
Expand Down Expand Up @@ -571,7 +572,8 @@ cdef class DenseGraph(CGraph):
"""
cdef int place = v / radix
cdef unsigned long word = (<unsigned long>1) << (v & radix_mod_mask)
cdef int i, num_nbrs = 0
cdef size_t i
cdef int num_nbrs = 0
for i in range(self.active_vertices.size):
if self.edges[place + i * self.num_longs] & word:
if num_nbrs == size:
Expand Down
24 changes: 13 additions & 11 deletions src/sage/graphs/base/sparse_graph.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ cdef class SparseGraph(CGraph):
"""
cdef SparseGraphBTNode **temp
cdef SparseGraphLLNode *label_temp
cdef int i
cdef size_t i

# Freeing the list of arcs attached to each vertex
for i from 0 <= i < self.active_vertices.size * self.hash_length:
Expand Down Expand Up @@ -370,21 +370,22 @@ cdef class SparseGraph(CGraph):
30
"""
if total == 0:
if not total:
raise RuntimeError('Sparse graphs must allocate space for vertices!')
cdef bitset_t bits
if total < self.active_vertices.size:
cdef size_t s_total = <size_t>total
if s_total < self.active_vertices.size:
bitset_init(bits, self.active_vertices.size)
bitset_set_first_n(bits, total)
bitset_set_first_n(bits, s_total)
if not bitset_issubset(self.active_vertices, bits):
bitset_free(bits)
return -1
bitset_free(bits)

self.vertices = <SparseGraphBTNode **>check_reallocarray(
self.vertices, total * self.hash_length, sizeof(SparseGraphBTNode *))
self.in_degrees = <int *>check_reallocarray(self.in_degrees, total, sizeof(int))
self.out_degrees = <int *>check_reallocarray(self.out_degrees, total, sizeof(int))
self.vertices, s_total * self.hash_length, sizeof(SparseGraphBTNode *))
self.in_degrees = <int *>check_reallocarray(self.in_degrees, s_total, sizeof(int))
self.out_degrees = <int *>check_reallocarray(self.out_degrees, s_total, sizeof(int))

cdef int new_vertices = total - self.active_vertices.size

Expand All @@ -404,7 +405,7 @@ cdef class SparseGraph(CGraph):
new_vertices * sizeof(int))

# self.active_vertices
bitset_realloc(self.active_vertices, total)
bitset_realloc(self.active_vertices, s_total)

###################################
# Unlabeled arc functions
Expand Down Expand Up @@ -508,9 +509,9 @@ cdef class SparseGraph(CGraph):
True
"""
if u < 0 or u >= self.active_vertices.size or not bitset_in(self.active_vertices, u):
if u < 0 or u >= <int>self.active_vertices.size or not bitset_in(self.active_vertices, u):
return False
if v < 0 or v >= self.active_vertices.size or not bitset_in(self.active_vertices, v):
if v < 0 or v >= <int>self.active_vertices.size or not bitset_in(self.active_vertices, v):
return False
return self.has_arc_unsafe(u,v)

Expand Down Expand Up @@ -853,7 +854,8 @@ cdef class SparseGraph(CGraph):
expensive than out_neighbors_unsafe.
"""
cdef int i, num_nbrs = 0
cdef size_t i
cdef int num_nbrs = 0
if self.in_degrees[v] == 0:
return 0
for i from 0 <= i < self.active_vertices.size:
Expand Down
2 changes: 1 addition & 1 deletion src/sage/graphs/base/static_sparse_backend.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ cdef class StaticSparseCGraph(CGraph):
bitset_free(self.active_vertices)
free_short_digraph(self.g)
sig_free(self.number_of_loops)
if self.g_rev != NULL:
if self._directed:
free_short_digraph(self.g_rev)

cpdef bint has_vertex(self, int v) except -1:
Expand Down
21 changes: 11 additions & 10 deletions src/sage/graphs/base/static_sparse_graph.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,8 @@ cdef void strongly_connected_components_digraph_C(short_digraph g, int nscc, int
which should be empty at the beginning.
"""
cdef MemoryAllocator mem = MemoryAllocator()
cdef int v, w, i
cdef size_t v, w, i
cdef size_t s_nscc = <size_t>nscc
cdef int tmp = nscc + 1
cdef vector[vector[int]] scc_list = vector[vector[int]](nscc, vector[int]())
cdef vector[vector[int]] sons = vector[vector[int]](nscc + 1, vector[int]())
Expand All @@ -778,15 +779,15 @@ cdef void strongly_connected_components_digraph_C(short_digraph g, int nscc, int
cdef uint32_t degv
cdef uint32_t *p_tmp

for v in range(nscc):
for v in range(s_nscc):
scc_list[v] = vector[int]()
sons[v] = vector[int]()
sons[nscc] = vector[int]()

for i in range(g.n):
for i in range(<size_t>g.n):
scc_list[scc[i]].push_back(i)

for v in range(nscc):
for v in range(s_nscc):
for i in range(scc_list[v].size()):
p_tmp = g.neighbors[scc_list[v][i]]
while p_tmp<g.neighbors[scc_list[v][i]+1]:
Expand All @@ -806,13 +807,13 @@ cdef void strongly_connected_components_digraph_C(short_digraph g, int nscc, int

if not m:
output.edges = NULL
for v in range(1, nscc + 1):
for v in range(1, s_nscc + 1):
output.neighbors[v] = NULL

output.edges = <uint32_t *> check_allocarray(m, sizeof(uint32_t))
output.neighbors[0] = output.edges

for v in range(1, nscc + 1):
for v in range(1, s_nscc + 1):
degv = sons[v].size()
output.neighbors[v] = output.neighbors[v-1] + sons[v-1].size()
for i in range(sons[v].size()):
Expand Down Expand Up @@ -929,8 +930,8 @@ def triangles_count(G):
cdef uint32_t * p1
cdef uint32_t * p2

for u in range(g.n):
for i in range(out_degree(g, u)):
for u in range(<uint32_t>g.n):
for i in range(<uint32_t>out_degree(g, u)):
v = g.neighbors[u][i]
if v <= u:
continue
Expand Down Expand Up @@ -1146,8 +1147,8 @@ def spectral_radius(G, prec=1e-10):
G = G.copy(immutable=True)
g[0] = (<StaticSparseCGraph> (<StaticSparseBackend> G._backend)._cg).g[0]

cdef long n = g.n
cdef long m = g.m
cdef size_t n = <size_t>g.n
cdef size_t m = <size_t>g.m
cdef uint32_t ** neighbors = g.neighbors

# v1 and v2 are two arrays of length n, allocated as one array
Expand Down
4 changes: 2 additions & 2 deletions src/sage/graphs/centrality.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ cdef dict centrality_betweenness_C(G, numerical_type _, bint normalize=True):
# We enumerate vertices in reverse order of discovery.
for i in range(layer_current_end - 1, -1, -1):
u = queue[i]
for j in range(degrees[u]):
for j in range(<int>degrees[u]):
v = bfs_dag.neighbors[u][j]
if v != source: # better to not 'if' but set it to 0 afterwards?
if numerical_type is double:
Expand Down Expand Up @@ -455,7 +455,7 @@ cdef void _estimate_reachable_vertices_dir(short_digraph g, int* reachL, int* re

for i in range(n):
reachL[i] = reachL_scc[scc[i]]
reachU[i] = <int> min(reachU_scc[scc[i]], g.n)
reachU[i] = min(<int>reachU_scc[scc[i]], g.n)

cdef void _compute_reachable_vertices_undir(short_digraph g, int* reachable):
r"""
Expand Down

0 comments on commit 90948f4

Please sign in to comment.