//Write your own contracts here. Currently compiles using solc v0.4.15+commit.bbb8e64f. pragma solidity ^0.4.26; contract Sorter { uint public totalUsers; // userID -> index mapping (uint => uint) public userMap; // string[][2] public userDetails; int[100][100][2] public adjMatrix; uint passedTxnsCnt; constructor() public { // loopVar = initVal*50; totalUsers = 0; passedTxnsCnt = 0; } // function runLoop() view public{ // uint a=0; // for (uint i = 0; i < loopVar; i++) // { // a++; // } // } function registerUser(uint userID, string userName) public{ // userDetails[totalUsers][0] = string(userID); // userDetails[totalUsers][0] = userName; userMap[userID] = totalUsers; for (uint i = 0; i < totalUsers; i++){ adjMatrix[totalUsers][i][0] = -1; adjMatrix[totalUsers][i][1] = -1; adjMatrix[i][totalUsers][0] = -1; adjMatrix[i][totalUsers][1] = -1; } totalUsers += 1; } function createAcc(uint userID1, uint userID2, int sampledValue) public{ // sampledValue will come from python script. Solidity doesn't have exponential dist. // The sampled value must be an even integer (solidity doesn't support float/double either) adjMatrix[userMap[userID1]][userMap[userID2]][0] = sampledValue/2; adjMatrix[userMap[userID1]][userMap[userID2]][1] = sampledValue/2; adjMatrix[userMap[userID2]][userMap[userID1]][0] = sampledValue/2; adjMatrix[userMap[userID2]][userMap[userID1]][1] = sampledValue/2; } function sendAmount(uint userID1, uint userID2) public{ // find the shortest path between userMap[userID1], userMap[userID2] // path = [a, b, c, d]; a->d uint[] memory visited = new uint[](totalUsers); uint[] queue; uint[] memory dist = new uint[](totalUsers); int[] memory parent = new int[](totalUsers); for (uint i=0;i<totalUsers;i++){ dist[i] = 1000; parent[i] = -1; visited[i] = 0; } uint src = userMap[userID1]; uint dest = userMap[userID2]; queue.push(src); visited[src] = 1; dist[src] = 0; uint flag = 0; while (queue.length!=0){ uint node = queue[0]; delete queue[0]; for (i = 0; i<totalUsers; i++){ if (!(adjMatrix[node][i][0] == -1 && adjMatrix[node][i][1] == -1) && adjMatrix[node][i][0] >= 1){ if (visited[i] == 0){ visited[i] = 1; dist[i] = dist[node] + 1; parent[i] = int(node); queue.push(i); if (i == dest){ flag = 1; break; } } } } } uint[] path; if (flag == 0){ // print("no path found"); } else{ // uint size = 0; passedTxnsCnt += 1; node = dest; while (node != src){ path.push(node); // size += 1; node = uint(parent[node]); } path.push(src); // size += 1; // return path; } // uint[] path = shortestPath(userID1, userID2); for (i = 0; i < path.length - 1; i++){ adjMatrix[path[i]][path[i+1]][0] -= 1; adjMatrix[path[i]][path[i+1]][1] += 1; adjMatrix[path[i+1]][path[i]][0] += 1; adjMatrix[path[i+1]][path[i]][1] -= 1; } } function closeAcc(uint userID1, uint userID2) public{ adjMatrix[userMap[userID1]][userMap[userID2]][0] = -1; adjMatrix[userMap[userID1]][userMap[userID2]][1] = -1; } function getTxStatus() public view returns(uint){ return passedTxnsCnt; } }
0.4.26