fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
4bd44d03
patching
1// Copyright 2017 The Bazel Authors. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15#include "ffi/rust_calling_c/c/matrix.h" 16 17#include <stdio.h> 18#include <string.h> 19 20Matrix * matrix_new (size_t rows ,size_t cols ,const uint64_t * data ) { 21if (data == NULL ) { 22return NULL ; 23 } 24Matrix * matrix = (Matrix * )malloc (sizeof (* matrix )); 25if (matrix == NULL ) { 26return NULL ; 27 } 28matrix -> rows = rows ; 29matrix -> cols = cols ; 30matrix -> data = (uint64_t * )malloc (rows * cols * sizeof (* (matrix -> data ))); 31memcpy (matrix -> data ,data ,rows * cols * sizeof (* data )); 32return matrix ; 33} 34 35int matrix_at (const Matrix * matrix ,size_t row ,size_t col ,uint64_t * n ) { 36if (matrix == NULL || matrix -> data == NULL || n == NULL ) { 37return 0 ; 38 } 39if (row >=matrix -> rows || col >=matrix -> cols ) { 40return 0 ; 41 } 42* n = matrix -> data [row * matrix -> cols + col ]; 43return 1 ; 44} 45 46int matrix_set (const Matrix * matrix ,size_t row ,size_t col ,uint64_t n ) { 47if (matrix == NULL || matrix -> data == NULL ) { 48return 0 ; 49 } 50if (row >=matrix -> rows || col >=matrix -> cols ) { 51return 0 ; 52 } 53matrix -> data [row * matrix -> cols + col ]= n ; 54return 1 ; 55} 56 57void matrix_transpose (Matrix * matrix ) { 58if (matrix == NULL || matrix -> data == NULL ) { 59return ; 60 } 61 62size_t len = matrix -> rows * matrix -> cols ; 63int * visited = (int * )malloc (len * sizeof (* visited )); 64if (visited == NULL ) { 65return ; 66 } 67memset (visited ,0 ,len * sizeof (* visited )); 68 69// Follow-the-cycles implementation of matrix transposition. Note that we 70// skip the last element since it always has a cycle of length 1 and thus 71// does not need to be moved. 72size_t q = matrix -> rows * matrix -> cols - 1 ; 73for (size_t i = 0 ;i < q ;++ i ) { 74if (visited [i ]== 1 ) { 75continue ; 76 } 77size_t current_idx = i ; 78size_t next_idx = i ; 79do { 80visited [current_idx ]= 1 ; 81next_idx = (current_idx * matrix -> cols ) %q ; 82if (next_idx == i ) { 83break ; 84 } 85 86uint64_t current_val = matrix -> data [current_idx ]; 87matrix -> data [current_idx ]= matrix -> data [next_idx ]; 88matrix -> data [next_idx ]= current_val ; 89current_idx = next_idx ; 90 }while (1 ); 91 } 92 93free (visited ); 94size_t cols = matrix -> rows ; 95matrix -> rows = matrix -> cols ; 96matrix -> cols = cols ; 97} 98 99int matrix_equal (const Matrix * a ,const Matrix * b ) { 100if (a == NULL || b == NULL || a -> data == NULL || b -> data == NULL ) { 101return 0 ; 102 } 103if (a -> rows != b -> rows || a -> cols != b -> cols ) { 104return 0 ; 105 } 106size_t len = a -> rows * a -> cols ; 107for (size_t i = 0 ;i < len ;++ i ) { 108if (a -> data [i ]!= b -> data [i ]) { 109return 0 ; 110 } 111 } 112return 1 ; 113} 114 115void matrix_free (Matrix * matrix ) { 116if (matrix == NULL ) { 117return ; 118 } 119if (matrix -> data != NULL ) { 120free (matrix -> data ); 121 } 122free (matrix ); 123}