id
stringlengths 36
36
| text
stringlengths 1
1.25M
|
---|---|
59484725-15b6-4807-a537-50baa69e9c89 | RecieveMsg(SctpChannel clientSocket) {
this.clientSocket = clientSocket;
} |
52a35e66-b3af-4093-8d52-b5b5385b3437 | public static void receiveMessage(SctpChannel clientSock)
throws InterruptedException {
String[] output;
String[] outputVector;
ByteBuffer byteBuffer;
byteBuffer = ByteBuffer.allocate(512);
String messageType = "";
try {
if (running = true) {
MessageInfo messageInfo = clientSock.receive(byteBuffer, null,
null);
String message = byteToString(byteBuffer);
System.out.println("Received Message from Server: "
+ clientSock.getRemoteAddresses() + " " + message);
messageType = message.substring(0, 2);
message = message.substring(2, message.length());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} |
4638e628-0e04-40d5-896c-83fcd13264d2 | public static String byteToString(ByteBuffer byteBuffer) {
byteBuffer.position(0);
byteBuffer.limit(512);
byte[] bufArr = new byte[byteBuffer.remaining()];
byteBuffer.get(bufArr);
return new String(bufArr);
} |
ed203918-0ee7-4a50-8c31-2bb36acf60cf | @Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
receiveMessage(clientSocket);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} |
38dd2455-8a63-4383-bb77-6c097e569766 | public static void readConnectedTo(String filePath) throws IOException {
BufferedReader br = null;
String sCurrentLine;
String[] output;
ArrayList<TripletData> result = new ArrayList<>();
br = new BufferedReader(new FileReader(filePath));
while (!(sCurrentLine = br.readLine()).equals("END")) {
}
int counter = 1;
while ((sCurrentLine = br.readLine()) != null) {
System.out.println("Hello");
output = sCurrentLine.split("\\s+");
if (counter == myNodeNumber) {
for (int i = 0; i < output.length; i++) {
if (output[i].equals("1")) {
System.out.println("Hey");
for (int j = 0; j < nodeList.size(); j++) {
if (nodeList.get(j).hostName == i + 1)
connectedTo.add(nodeList.get(j));
}
}
}
}
counter++;
}
br.close();
} |
b91caef7-95f7-45bf-a70c-9336f2008e5e | public static void readNodeList(String filePath) throws IOException {
BufferedReader br = null;
String sCurrentLine;
String[] output;
ArrayList<TripletData> result = new ArrayList<>();
br = new BufferedReader(new FileReader(filePath));
System.out.println("here");
while (!(sCurrentLine = br.readLine()).equals("END")) {
TripletData newEntry = new TripletData();
if (sCurrentLine.equals("")) {
} else {
output = sCurrentLine.split("\\s+");
if (Integer.parseInt(output[0]) == myNodeNumber) {
myHostAddress = output[1];
myPortNumber = Integer.parseInt(output[2]);
System.out.println("Hostname:" + myHostAddress + ";"
+ "myPortNumber:" + myPortNumber);
// lud.put(myNodeNumber,-1);
} else {
newEntry.hostName = Integer.parseInt(output[0]);
newEntry.ipAddress = output[1];
newEntry.portNo = Integer.parseInt(output[2]);
nodeList.add(newEntry);
// lud.put(newEntry.hostName,-1);
}
}
}
br.close();
} |
377dde37-1505-47b1-b1c7-a7afcc14763d | public static void main(String args[]) throws IOException, Exception {
System.out.println("Enter Node Number");
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(
System.in));
myNodeNumber = Integer.parseInt(bufferRead.readLine());
System.out.println("myNodeNumber:" + myNodeNumber);
readNodeList("topo2.txt");
readConnectedTo("topo2.txt");
for (int i = 0; i < MainClass.connectedTo.size(); i++) {
System.out.println("Connected To : "
+ MainClass.connectedTo.get(i).hostName + ","
+ MainClass.connectedTo.get(i).ipAddress + ","
+ MainClass.connectedTo.get(i).portNo);
}
for (int i = 0; i < MainClass.nodeList.size(); i++) {
System.out.println("Node List : "
+ MainClass.nodeList.get(i).hostName + ","
+ MainClass.nodeList.get(i).ipAddress + ","
+ MainClass.nodeList.get(i).portNo);
}
if (MainClass.myNodeNumber == 1)
CriticalSection.setTokenHere(true);
for (int i = 0; i <= MainClass.nodeList.size(); i++) {
System.out.println("Initialized");
LinkedList<ReqID> d = new LinkedList<>();
CriticalSection.reqArray.add(i, d);
}
Thread server = new SCTPServer();
server.setName("Server Thread");
server.start();
Thread.sleep(10000);
Thread client = new SCTPClient();
client.start();
// Thread cs= new CriticalSection(criticalCount);
Thread.sleep(10000);
// for(int i=0;i<criticalCount;i++){
// int randomNum = 500 + (int)(Math.random()*10000);
// Thread.sleep(randomNum);
// CriticalSection.enterCS();
// }
// cs.start();
// for(int i=0;i<criticalCount;i++){
// int randomNum = 500 + (int)(Math.random()*10000);
// Thread.sleep(randomNum);
// for(int i=0;i<criticalCount;i++){
// int randomNum = 1000 + (int)(Math.random()*3000);
// while(!CriticalSection.getInCS()){
//
// }
// Thread.sleep(randomNum);
// CriticalSection.exitCS();
//
// }
// }
Thread.sleep(600000);
// if(myNodeNumber==1){
// Thread.sleep(30000);
// CriticalSection.exitCS();
// }
// else
// CriticalSection.exitCS();
System.out.println("Waiting in main");
// cs.join();
// System.out.println("Waiting in main after cs.join()");
server.join();
client.join();
System.out.println("Out OF MAIN");
} |
97cc1c8d-f68c-4170-b3e5-0b76ff18bf6e | public void checkSortedList()
{
String targetDirectory = System.getProperty("user.dir");
String[] sortedArray = null;
File directory = new File(targetDirectory);
File[] files = directory.listFiles();
int fileSize = files.length;
for (int i = 0; i < fileSize; i++) {
BufferedReader buffer1 = null;
try {
buffer1 = new BufferedReader(new FileReader(files[i]));
String line1 = buffer1.readLine() + ",";
sortedArray = line1.split(",");
int comp = 0;
boolean sorted = false;
for (int p = 0; p < sortedArray.length - 1; p++) {
comp = Integer.parseInt(sortedArray[p]);
if (comp > (Integer.parseInt(sortedArray[p + 1]))) {
sorted = false;
System.out.println("Not in sorted Order");
break;
} else {
sorted = true;
// System.out.println("File already in order");
}
}
if (sorted) {
System.out.println("File already in order");
}
} catch (Exception e) {
e.printStackTrace();
}
// System.out.println(listHere.get(0));
}
} |
72895692-40c5-41ba-b4a5-79375485eaca | public static void main(String[] args) {
CheckFilesSort check = new CheckFilesSort();
check.checkSortedList();
} |
d677c229-7939-40d0-9287-e584384fdf3f | public QuadData() {
} |
33985d71-ed8e-4dd9-b334-3752b58c2251 | public QuadData(int ele, int ts, int i, int j) {
elec = ele;
timeStamp = ts;
indexI = i;
indexJ = j;
} |
0d17b36c-85a0-4737-b268-7da2bf44297b | TripletData() {
} |
1f3a3dea-2978-41dc-ae54-bccd06e85339 | TripletData(int hostName, String ipAddress, int portNo) {
hostName = this.hostName;
ipAddress = this.ipAddress;
portNo = this.portNo;
} |
c648d99d-53a6-4408-8ab8-76ac2e0add3b | public static void sendMessage(SctpChannel clientSock, String Message)
throws IOException {
// System.out.println("Hey Im inside Send Messgae");
// prepare byte buffer to send massage
ByteBuffer sendBuffer = ByteBuffer.allocate(512);
sendBuffer.clear();
// Reset a pointer to point to the start of buffer
sendBuffer.put(Message.getBytes());
sendBuffer.flip();
System.out.println("Sending to... " + clientSock.getRemoteAddresses()
+ " " + Message);
try {
// Send a message in the channel
MessageInfo messageInfo = MessageInfo.createOutgoing(null, 0);
clientSock.send(sendBuffer, messageInfo);
sendBuffer.clear();
} catch (IOException ex) {
ex.printStackTrace();
}
} |
8730057b-5611-4dcc-838f-d9dc11684737 | public static void sendMessage2(SctpChannel clientSock, String Message)
throws IOException {
// System.out.println("Hey Im inside Send Messgae");
// prepare byte buffer to send massage
ByteBuffer sendBuffer = ByteBuffer.allocate(512);
sendBuffer.clear();
// Reset a pointer to point to the start of buffer
sendBuffer.put(Message.getBytes());
sendBuffer.flip();
System.out.println("Sending2 to... " + clientSock.getRemoteAddresses()
+ " " + Message);
try {
// Send a message in the channel
MessageInfo messageInfo = MessageInfo.createOutgoing(null, 0);
clientSock.send(sendBuffer, messageInfo);
sendBuffer.clear();
} catch (IOException ex) {
ex.printStackTrace();
}
} |
5ffd22eb-cd40-428b-a9ea-8966790bd0ca | public static void receiveMessage(SctpChannel clientSock) {
ByteBuffer byteBuffer;
byteBuffer = ByteBuffer.allocate(512);
try {
MessageInfo messageInfo = clientSock
.receive(byteBuffer, null, null);
String message = byteToString(byteBuffer);
// System.out.println("Received Message from Server:");
// System.out.println(message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} |
7f8dcee8-dc07-4654-957e-6a321cbc7847 | public static String byteToString(ByteBuffer byteBuffer) {
byteBuffer.position(0);
byteBuffer.limit(512);
byte[] bufArr = new byte[byteBuffer.remaining()];
byteBuffer.get(bufArr);
return new String(bufArr);
} |
11bdaae2-f27a-4220-b521-284e940ffe86 | public void run() {
try {
for (int i = 0; i < MainClass.connectedTo.size(); i++) {
System.out.println("In Client");
InetSocketAddress serverAddr = new InetSocketAddress(
MainClass.connectedTo.get(i).ipAddress,
MainClass.connectedTo.get(i).portNo);
MainClass.ClientSocket.put(
MainClass.connectedTo.get(i).hostName,
SctpChannel.open());
MainClass.ClientSocket.get(
MainClass.connectedTo.get(i).hostName).connect(
serverAddr, 0, 0);
String message = "Hello From Client with node number:"
+ MainClass.connectedTo.get(i).hostName;
sendMessage(MainClass.ClientSocket.get(MainClass.connectedTo
.get(i).hostName), message);
}
while (true) {
}
} catch (Exception e) {
e.printStackTrace();
}
// while(runningClient){
// VectorClockClass.globalCounter++;
// for(int i=0;i<VectorClockClass.hostNames.size();i++)
// {
// //System.out.println("Host Names Size in Sending="+VectorClockClass.hostNames.size());
// String message=null;
// //System.out.println("Thread going to sleep");
// //System.out.println("Thread is up");
// if(!VectorClockClass.myHostAddress.equals(VectorClockClass.hostNames.get(i))
// &&
// VectorClockClass.myPortNumber!=VectorClockClass.portNumbers.get(i)){
// if(VectorClockClass.sentMessageCounter<sendingExitMessage){
// message="Hello From Client with node number:"+VectorClockClass.myNodeNumber+" Message Number:"+VectorClockClass.globalCounter+",";
//
// }
// else{
// message="END,";
// }
//
// VectorClockClass.setInternalClock(1);
// VectorClockClass.vector.put(VectorClockClass.myNodeNumber,VectorClockClass.internalClock);
// // for(int j=0;j<=VectorClockClass.hostNames.size();j++)
// // {
// // message+=VectorClockClass.vector.get(j+1)+":";
// // }
// Iterator it = VectorClockClass.vector.entrySet().iterator();
// while (it.hasNext()) {
// Map.Entry pairs = (Map.Entry)it.next();
// message+=pairs.getValue()+":";
// //it.remove(); // avoids a ConcurrentModificationException
// }
//
// try {
// //System.out.println("Sending..."+message);
// sendMessage(ClientSocket.get(VectorClockClass.nodeNumbers.get(i)),
// message);
// if(VectorClockClass.sentMessageCounter>=sendingExitMessage){
// System.out.println("Vector After Sending exit message:"+VectorClockClass.vector);
// runningClient=false;
// //System.out.println(runningClient);
// }
// Thread.sleep(500*(VectorClockClass.myNodeNumber));
// } catch (CharacterCodingException | InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// }
// VectorClockClass.sentMessageCounter++;
// }
} |
41abf86a-5da5-404e-8e70-a6894094d611 | public ReqID() {
} |
e5e9f12f-5f00-4d09-a9d7-22ea25d378d1 | public ReqID(int reqO, int reqT) {
reqOrigin = reqO;
reqTime = reqT;
} |
22ae3279-7252-4a62-b639-11c237cd86a4 | @Before
public void setUp() {
r = new Random();
} |
b7191a99-c25b-48c4-b64e-e53932db3a6e | @Test
public void randomizedAddTest() {
for(int i = 1; i <= TESTS_AMOUNT * TESTS_ENABLED; i++) {
BigInteger a = new BigInteger(TESTS_MULTIPLIER * i, r);
BigInteger b = new BigInteger(TESTS_MULTIPLIER * i, r);
UnsignedBigInt c = new UnsignedBigInt(a.toString());
UnsignedBigInt d = new UnsignedBigInt(b.toString());
assertTrue(a.add(b).toString().equals(c.add(d).toString()));
}
} |
f3b159d2-064a-48f2-8060-fc0c368e6494 | @Test
public void randomizedSubtractTest() {
for(int i = 1; i <= TESTS_AMOUNT * TESTS_ENABLED; i++) {
BigInteger a = new BigInteger(TESTS_MULTIPLIER * i, r);
BigInteger b = new BigInteger(TESTS_MULTIPLIER * i, r);
if(a.compareTo(b) < 0) {
BigInteger swap = a;
a = b;
b = swap;
}
UnsignedBigInt c = new UnsignedBigInt(a.toString());
UnsignedBigInt d = new UnsignedBigInt(b.toString());
assertTrue(a.subtract(b).toString().equals(c.subtract(d).toString()));
}
} |
dc874734-4b04-483b-81de-4a2c9dcffb0d | @Test
public void randomizedMultiplyTest() {
for(int i = 1; i <= TESTS_AMOUNT * TESTS_ENABLED; i++) {
BigInteger a = new BigInteger(TESTS_MULTIPLIER * i, r);
BigInteger b = new BigInteger(TESTS_MULTIPLIER * i, r);
UnsignedBigInt c = new UnsignedBigInt(a.toString());
UnsignedBigInt d = new UnsignedBigInt(b.toString());
assertTrue(a.multiply(b).toString().equals(c.multiply(d).toString()));
}
} |
f6738758-264e-48ba-8061-a9c73cac2659 | @Test
public void randomizedDivideTest() {
for(int i = 1; i <= TESTS_AMOUNT * TESTS_ENABLED; i++) {
BigInteger a = new BigInteger(TESTS_MULTIPLIER * i, r);
BigInteger b = new BigInteger(TESTS_MULTIPLIER * i, r);
UnsignedBigInt c = new UnsignedBigInt(a.toString());
UnsignedBigInt d = new UnsignedBigInt(b.toString());
BigInteger biResult[] = a.divideAndRemainder(b);
UnsignedBigInt ubiResult[] = c.divideAndRemainder(d);
assertTrue("Quotient mismatch: was " + ubiResult[0].toString() + ", expected " + biResult[0].toString(),
biResult[0].toString().equals(ubiResult[0].toString()));
assertTrue("Remainder mismatch: was " + ubiResult[1].toString() + ", expected " + biResult[1].toString(),
biResult[1].toString().equals(ubiResult[1].toString()));
}
} |
12663868-8bbf-4d74-845a-aa40705743f6 | @Before
public void setUp() {
a = new UnsignedBigInt(12341234L);
b = new UnsignedBigInt("12341234123412341234123412341234123412341234");
} |
a216d99f-6faf-43bc-b960-2de7e277b503 | @Test
public void testLongConstructor_positiveInt() {
assertEquals(a.longValue(), 12341234L);
} |
699821c2-cbcb-4423-a3f9-b9238f26f768 | @Test
public void testStringConstructor() {
assertEquals(b.toString(), "12341234123412341234123412341234123412341234");
} |
d4b3d394-1cfd-4f3a-b585-18967d806793 | @Test
public void testAdd() {
assertEquals(a.add(b), new UnsignedBigInt("12341234123412341234123412341234123424682468"));
assertEquals(b.add(a), new UnsignedBigInt("12341234123412341234123412341234123424682468"));
} |
5a22dd19-172d-4439-8455-de47021f03f4 | @Test
public void testBiggerThan() {
assertTrue(b.biggerThan(a));
assertTrue(!a.biggerThan(b));
assertTrue(!b.biggerThan(b));
} |
1b3f4dc0-8d1a-49b7-9aa5-8ba333279c61 | @Test
public void testEquals() {
assertTrue(a.equals(a));
assertTrue(b.equals(b));
assertTrue(!a.equals(b));
assertTrue(!b.equals(a));
assertTrue(a.equals(new UnsignedBigInt(a.toString())));
} |
1ea50f3d-26e1-4659-a78c-8c4ee079e627 | @Test
public void testDivide() {
assertEquals(a.divide(a), new UnsignedBigInt("1"));
assertEquals(b.divide(a), new UnsignedBigInt("1000000010000000100000001000000010000"));
} |
dc30e80c-9c29-4aba-97fa-9aed968cebc3 | @Test
public void testLongValue() {
assertEquals(a.longValue(), 12341234L);
} |
3241490b-e58e-4cf4-be5a-cc4a74e36a2f | @Test
public void testMod() {
assertEquals(a.mod(a), new UnsignedBigInt("0"));
assertEquals(b.mod(a), new UnsignedBigInt("1234"));
} |
583fac55-4459-47b8-8502-a8267c38af13 | @Test
public void testModPow() {
assertEquals(b.modPow(new UnsignedBigInt("4"), a), new UnsignedBigInt("3720510"));
assertEquals(b.modPow(a, a), new UnsignedBigInt("1522756"));
} |
1f4a9a18-37ac-43ca-860a-e637d1f52eb3 | @Test
public void testMultiply_basecase() {
assertEquals(a.multiply(a), new UnsignedBigInt("152306056642756"));
} |
7bfe7ffe-eed2-466b-8b0e-47be70d84bf4 | @Test
public void testMultiply_karatsuba() {
UnsignedBigInt c = new UnsignedBigInt("1552518092300708935148979488462502555256886017116696611139052038026050952686376886330878408828646477950487730697131073206171580044114814391444287275041181139204454976020849905550265285631598444825262999193716468750892846853816057855");
assertEquals(c.multiply(c), new UnsignedBigInt("2410312426921032588580116606028314112912093247945688951359675039065257391591803200669085024107346049663448766280888004787862416978794958324969612987890774651455213339381625224770782077917681499676845543137387820057597345857904599106356350937498090094699856664417295567115701321048224206133690087915683964997615196447464774285761276110587297055818433175507740905485263500588072081099152785395963423140560695146462266635509081361999844936968295485071311052707201025"));
} |
574ab3eb-d6bd-42de-a71d-f14f9c785478 | @Test
public void testPow() {
assertEquals(a.pow(new UnsignedBigInt("0")), new UnsignedBigInt("1"));
assertEquals(a.pow(new UnsignedBigInt("1")), a);
assertEquals(a.pow(new UnsignedBigInt("2")), new UnsignedBigInt("152306056642756"));
assertEquals(a.pow(new UnsignedBigInt("3")), new UnsignedBigInt("1879644684645506200904"));
assertEquals(a.pow(new UnsignedBigInt("4")), new UnsignedBigInt("23197134890066399073807275536"));
} |
2fbb485b-e0bb-43ea-b28e-1b7893d87b58 | @Test
public void testSmallerThan() {
assertTrue(a.smallerThan(b));
assertTrue(!b.smallerThan(a));
assertTrue(!a.smallerThan(a));
} |
04d66572-0423-443a-9ac5-e163201139ee | @Test
public void testSubtract() {
assertEquals(b.subtract(a), new UnsignedBigInt("12341234123412341234123412341234123400000000"));
assertEquals(a.subtract(a), new UnsignedBigInt("0"));
} |
2e02b97a-a5c0-4ea7-ad43-0d022830c473 | @Test
public void testToString() {
assertEquals(a.toString(), "12341234");
assertEquals(b.toString(), "12341234123412341234123412341234123412341234");
} |
101d476c-ab7b-4f99-86df-1b9eba7a05a4 | public UnsignedBigInt(long l) {
this.bits = new BitArray(l);
} |
cd821780-5e7d-4a49-952b-f6dd5ce95e85 | public UnsignedBigInt(String s) {
this();
UnsignedBigInt newBigInt = new UnsignedBigInt();
for(int i = 0; i < s.length(); i++) {
newBigInt = newBigInt.multiply(UnsignedBigInt.TEN);
newBigInt = newBigInt.add(new UnsignedBigInt(Long.parseLong(s.charAt(i) + "")));
}
this.bits = newBigInt.bits;
} |
6a6a61f3-4bc1-4ee6-8d32-7ac0ffc6e718 | public UnsignedBigInt() {
this.bits = new BitArray();
} |
a480a3a3-72af-449f-b9a2-21489b638fe4 | public UnsignedBigInt(UnsignedBigInt b) {
this.bits = new BitArray(b.bits);
} |
8be64a7b-6efd-4942-8f84-892366e17968 | public UnsignedBigInt(BitArray bits) {
this.bits = bits;
} |
4dce619f-e9af-4b3c-b9c6-8445d13887f3 | public UnsignedBigInt add(UnsignedBigInt b) {
UnsignedBigInt result = new UnsignedBigInt();
int carry = 0;
int max = Math.max(this.bits.length(), b.bits.length());
for(int i = 0; i <= max; i++) {
int s = this.bits.getBit(i) + b.bits.getBit(i) + carry;
carry = 0;
if(s >= 2) {
carry = 1;
s -= 2;
}
result.bits.setBit(i, s);
}
return result;
} |
2763da41-b023-4ff1-84c6-659a1cb445a9 | public boolean biggerThan(UnsignedBigInt b) {
for(int i = Math.max(this.bits.length(), b.bits.length()); i >= 0; i--) {
if(this.bits.getBit(i) > b.bits.getBit(i)) {
return true;
}
if(this.bits.getBit(i) < b.bits.getBit(i)) {
return false;
}
}
return false;
} |
6c2d3375-4b53-49d9-8ae2-efeb7b25df97 | @Override
public boolean equals(Object b) {
if(b == null || !(b instanceof UnsignedBigInt)) {
return false;
}
for(int i = Math.max(this.bits.length(), ((UnsignedBigInt)b).bits.length()); i >= 0; i--) {
if(this.bits.getBit(i) > ((UnsignedBigInt)b).bits.getBit(i)) {
return false;
}
if(this.bits.getBit(i) < ((UnsignedBigInt)b).bits.getBit(i)) {
return false;
}
}
return true;
} |
c0ca08e5-9063-44d0-a959-f2938aabc973 | public UnsignedBigInt divide(UnsignedBigInt b) {
UnsignedBigInt result[] = this.divideAndRemainder(b);
return result[0];
} |
087e72d6-7aa8-453a-8e10-a9aea0e16abd | public UnsignedBigInt[] divideAndRemainder(UnsignedBigInt b) {
// quotient is in result[0]
// remainder is in result[1]
UnsignedBigInt result[] = new UnsignedBigInt[2];
result[0] = new UnsignedBigInt();
result[1] = new UnsignedBigInt();
for(int i = this.bits.length() - 1; i >= 0; i--) {
result[1].bits.shiftLeft();
result[1].bits.setBit(0, this.bits.getBit(i));
if(result[1].biggerThan(b) || result[1].equals(b)) {
result[1] = result[1].subtract(b);
result[0].bits.setBit(i, 1);
}
}
return result;
} |
eec7ae08-39d0-49a8-9b81-99b4fd9e9669 | public long longValue() {
return this.bits.toLong();
} |
12c865d2-6c6c-4f0b-82ac-ffab153bc424 | public UnsignedBigInt mod(UnsignedBigInt m) {
UnsignedBigInt result[] = this.divideAndRemainder(m);
return result[1];
} |
84eaacb2-2a1a-457d-b14b-1c1b6bad4f30 | public UnsignedBigInt modPow(UnsignedBigInt e, UnsignedBigInt m) {
UnsignedBigInt b = new UnsignedBigInt(this);
UnsignedBigInt exp = new UnsignedBigInt(e);
UnsignedBigInt result = new UnsignedBigInt(UnsignedBigInt.ONE);
while(exp.biggerThan(UnsignedBigInt.ZERO)) {
UnsignedBigInt mod = exp.mod(UnsignedBigInt.TWO);
if(mod.equals(UnsignedBigInt.ONE)) {
result = result.multiply(b).mod(m);
}
exp.bits.shiftRight();
b = b.multiply(b).mod(m);
}
return result;
} |
871b012a-0b97-4470-aec1-9047f2dba62d | public UnsignedBigInt karatsuba(UnsignedBigInt b) {
int m = (int)Math.ceil(Math.min(this.bits.length(), b.bits.length()) / 2.0);
if(m < 512) {
return this.multiply(b);
}
// TODO: calculate B^m with left shifts instead of multiplications
UnsignedBigInt base = UnsignedBigInt.TWO.pow(new UnsignedBigInt(m));
UnsignedBigInt base2 = base.pow(UnsignedBigInt.TWO);
UnsignedBigInt[] x = this.divideAndRemainder(base);
UnsignedBigInt[] y = b.divideAndRemainder(base);
UnsignedBigInt z0 = x[1].karatsuba(y[1]);
UnsignedBigInt z2 = x[0].karatsuba(y[0]);
UnsignedBigInt z1 = ((x[0].add(x[1])).karatsuba(y[0].add(y[1]))).subtract(z2).subtract(z0);
z2 = z2.multiply(base2);
z1 = z1.multiply(base);
return z0.add(z1.add(z2));
} |
7d612bcc-2284-4578-be1f-a5e15ea4f980 | public UnsignedBigInt multiply(UnsignedBigInt b) {
UnsignedBigInt result = new UnsignedBigInt();
UnsignedBigInt temp;
for(int i = 0; i < b.bits.length(); i++) {
if(b.bits.getBit(i) > 0) {
temp = new UnsignedBigInt(this);
for(int j = 0; j < i; j++) {
if(i - j > 64) {
temp.bits.shiftLeft64();
j += 63;
} else {
temp.bits.shiftLeft();
}
}
result = result.add(temp);
}
}
return result;
} |
411aae9e-566a-497c-aaf0-d71b82cc1698 | public UnsignedBigInt pow(UnsignedBigInt e) {
UnsignedBigInt b = new UnsignedBigInt(this);
UnsignedBigInt result = new UnsignedBigInt(UnsignedBigInt.ONE);
while(e.biggerThan(UnsignedBigInt.ZERO)) {
if(e.bits.getBit(0) > 0) {
result = result.multiply(b);
e.bits.setBit(0, 0);
}
b = b.multiply(b);
e.bits.shiftRight();
}
return result;
} |
29b85792-c77a-41f4-996f-cfcd3ccbab41 | public boolean smallerThan(UnsignedBigInt b) {
for(int i = Math.max(this.bits.length(), b.bits.length()); i >= 0; i--) {
if(this.bits.getBit(i) > b.bits.getBit(i)) {
return false;
}
if(this.bits.getBit(i) < b.bits.getBit(i)) {
return true;
}
}
return false;
} |
15504c6f-fb5c-40c8-a8f9-2e4b2fe4a528 | public UnsignedBigInt subtract(UnsignedBigInt b) {
UnsignedBigInt result = new UnsignedBigInt();
int borrow = 0;
int max = Math.max(this.bits.length(), b.bits.length());
for(int i = 0; i <= max; i++) {
int s = this.bits.getBit(i) - b.bits.getBit(i) + borrow;
borrow = 0;
if(s < 0) {
borrow = -1;
s += 2;
}
result.bits.setBit(i, s);
}
return result;
} |
540c2a23-9da0-4fd2-82ea-8ae59870cd80 | @Override
public String toString() {
StringBuilder sb = new StringBuilder();
UnsignedBigInt result[] = new UnsignedBigInt[2];
result[0] = new UnsignedBigInt(this);
do {
result = result[0].divideAndRemainder(UnsignedBigInt.TEN);
int n = 0;
for(int i = 0; i < 4; i++) {
if(result[1].bits.getBit(i) > 0) {
n += Math.pow(2, i);
}
}
sb.append(n);
} while(result[0].biggerThan(UnsignedBigInt.ZERO));
return sb.reverse().toString();
} |
e87658a6-7c29-4bd2-a58c-a51d2e07847f | public BitArray() {
a = new long[1];
} |
dcbb2339-fa71-4e85-bda4-4a017689b3ff | public BitArray(long l) {
a = new long[1];
a[0] = l;
} |
18822436-f59c-4d61-a09d-0d7f7febb9f0 | public BitArray(BitArray ba) {
a = new long[ba.a.length];
System.arraycopy(ba.a, 0, a, 0, ba.a.length);
} |
aa83fb71-9db0-47f1-aada-7f7d332e9555 | private void expand(int w) {
int size = Math.max(a.length * 2, w + 1);
long[] newArray = new long[size];
System.arraycopy(a, 0, newArray, 0, a.length);
this.a = newArray;
} |
5337755c-1f4a-4d2c-9b0e-0383a0f88943 | public int length() {
for(int i = a.length - 1; i >= 0; i--) {
if(a[i] != 0) {
for(int j = 63; j >= 0; j--) {
if((a[i] & (1L << (j % 64))) != 0) {
return (i * 64) + j + 1;
}
}
}
}
return 0;
} |
d4041ac6-6ad8-4b50-99ac-ce2e8cd8d1df | public void clear() {
for(int i = 0; i < a.length; i++) {
a[i] = 0;
}
} |
5f80b7a4-71c7-4ccf-8926-9f96e0a04418 | public void setBit(int i, int b) {
int w = (int)Math.floor(i / 64.0f);
if(w >= a.length) {
expand(w);
}
if(b == 1) {
a[w] |= (1L << (i % 64));
} else {
a[w] &= ~(1L << (i % 64));
}
} |
0a69102c-68f4-467c-a180-0cd00f5947a4 | public int getBit(int i) {
int w = (int)Math.floor(i / 64.0f);
if(w >= a.length) {
return 0;
}
return (a[w] & (1L << (i % 64))) == 0 ? 0 : 1;
} |
0954c843-0325-4c4a-8e29-242aafab1338 | public void shiftLeft() {
int lowBit = 0;
int highBit = 0;
for(int i = 0; i < a.length; i++) {
lowBit = highBit;
highBit = getBit((i * 64) + 63);
a[i] <<= 1L;
setBit(i * 64, lowBit);
if(highBit > 0 && i == a.length - 1) {
expand(0);
}
}
} |
c709ee1d-2dc8-4185-ad43-9e7b5dfe45d2 | public void shiftLeft64() {
if(Math.floor(length() / 64.0f) >= a.length - 1) {
expand(0);
}
long high = 0;
long low = 0;
for(int i = 0; i < a.length; i++) {
low = high;
high = a[i];
a[i] = low;
}
} |
ac24e8fc-fa96-4b1e-a223-c5b4d55c996c | public void shiftRight() {
int lowBit = 0;
int highBit = 0;
for(int i = a.length - 1; i >= 0; i--) {
highBit = lowBit;
lowBit = getBit(i * 64);
a[i] >>>= 1L;
setBit((i * 64) + 63, highBit);
}
} |
fa169689-b4df-4593-8a7b-54e2700138c2 | @Override
public String toString() {
StringBuilder sb = new StringBuilder();
for(int i = a.length - 1; i >= 0; i--) {
sb.append(" ");
sb.append(String.format("%64s", Long.toBinaryString(a[i])).replace(' ', '0'));
}
return sb.toString().trim();
} |
9dcb4a78-61e7-4f3b-9c10-335c48507df7 | public long toLong() {
return a[0];
} |
407fb1bc-19cc-403b-9b68-f7217b2d85f5 | public static void main(String[] args) {
SecureRandom random = new SecureRandom();
UnsignedBigInt p = new UnsignedBigInt(BigInteger.probablePrime(256, random).toString());
UnsignedBigInt g = new UnsignedBigInt("2");
UnsignedBigInt a = new UnsignedBigInt(BigInteger.probablePrime(128, random).toString());
UnsignedBigInt b = new UnsignedBigInt(BigInteger.probablePrime(128, random).toString());
System.out.println("Alice's secret is: " + a + ", Bob's secret is: " + b);
UnsignedBigInt A = g.modPow(a, p);
UnsignedBigInt B = g.modPow(b, p);
System.out.println("A: " + A);
System.out.println("B: " + B);
System.out.println("---");
UnsignedBigInt alice = B.modPow(a, p);
UnsignedBigInt bob = A.modPow(b, p);
System.out.println("Alice got: " + alice);
System.out.println("Bob got: " + bob);
} |
b72d6047-23d2-484a-9c02-d7974dc230f1 | public static void main (String[] args) {
GroupReader groupReader = createGroupReader(INPUT_FILE);
FileWriter fileWriter = createFileWriter(OUTPUT_FILE);
Dictionary.getInstance().setNGramLength(4); //TODO: magic
if (!loadDictionary()) {
buildDictionary(INPUT_FILE);
}
//From 0.0005 to 0.001
Dictionary.getInstance().deleteHighProbabilities(0.0005); //TODO: magic
//Is redundant
//Dictionary.getInstance().deleteLowProbabilities(0.002);
System.out.println("Process");
while (groupReader.hasNext()) {
Group group = groupReader.next();
if (group == null || group.size() < 1000) { //TODO: magic number
continue;
}
group.updateFrequencies();
//TODO: magic number
System.out.print("\r№: " + group.getGroupId());
fileWriter.println("№: " + group.getGroupId());
fileWriter.println("Group size: " + group.size());
fileWriter.printMostFrequentlyNGrams(group, 0.0005);
fileWriter.printMostDistinctMessages(group, 0.0005);
//break;//Temporary
}
System.out.println("\nFree resources");
groupReader.close();
fileWriter.close();
System.out.println("done");
} |
771c90e8-9f92-436f-9c88-691513943c87 | private static boolean loadDictionary() {
try {
Dictionary.getInstance().load();
} catch (FileNotFoundException exception) {
System.out.println("Dictionary file not found. Continue.");
return false;
}
return true;
} |
dd65713a-b0fb-4d8a-b20f-83b80bbb0e09 | private static void buildDictionary(String inputFilename) {
try {
Dictionary.getInstance().build(new CsvFileReader(), inputFilename);
} catch (FileNotFoundException exception) {
System.out.println("Dictionary building error. No such file. Exit.");
System.exit(1);
}
} |
c607e8c2-5c47-4365-8770-a7b56f99f5ad | private static GroupReader createGroupReader(String inputFilename) {
GroupReader reader = new GroupReader(new CsvFileReader());
try {
reader.open(inputFilename);
} catch (FileNotFoundException exception) {
System.out.println("No file \"" + inputFilename + "\" for input. Exit.");
System.exit(1);
}
return reader;
} |
2dfc457c-e7cd-4530-8f4d-ae1339c12fe2 | private static FileWriter createFileWriter(String outputFilename) {
FileWriter writer = null;
try {
writer = new FileWriter(outputFilename);
} catch (FileNotFoundException exception) {
System.out.println("Output file opening error. Exit.");
System.exit(1);
}
return writer;
} |
5dd70f51-d4d8-4e43-a2c8-b91438675bd7 | @Override
public Post next() {
if (hasNext()) {
Entry entry = readNextEntry();
ArrayList<Integer> nGramIds = Dictionary.getInstance().stringToIndexes(entry.message);
for (Integer a : nGramIds) {
if (a == 447) {
int b = 0;
int c = b+a;
}
}
return new Post(entry.message, entry.groupId, nGramIds);
}
return null;
} |
fbf844b5-f5b2-4ddf-a31d-83392f0b9651 | private Entry readNextEntry() {
scanner.useDelimiter("\\s");
int groupId = scanner.nextInt();
int messageId = scanner.nextInt();
long timeStamp = scanner.nextLong();
scanner.useDelimiter("\n|\t");
String message = scanner.next();
return new Entry(groupId, messageId, timeStamp, message);
} |
b6c7e307-0494-4e6b-bec1-1852e89217f2 | public Entry(int groupId, int messageId, long timeStamp, String message) {
this.groupId = groupId;
this.messageId = messageId;
this.timeStamp = timeStamp;
this.message = message;
} |
8cd05bfd-414c-4558-adec-bbe5a916fff3 | public Pair(A first, B second) {
this.first = first;
this.second = second;
} |
10fe2a0d-fa8c-43df-967c-ba79ffc2a051 | public NGram(String base) {
this.base = base;
} |
a0da1c53-283b-4afc-906c-ada394f93006 | public String getBase() {
return base;
} |
e533e543-b91c-4c82-8d98-10dd840a61b2 | @Override
public boolean equals(Object obj) {
return obj instanceof NGram && ((NGram) obj).base.equals(this.base);
} |
96825955-6ced-4039-bb52-b62a5e88da91 | @Override
public int hashCode() {
return base.hashCode();
} |
e8bd1469-c07d-4a23-bbb2-2ce6efdee053 | @Override
public String toString() {
return base;
} |
3225171e-892a-486b-b6be-11537f1b2725 | public Post(String message, int groupId, Collection<Integer> collection) {
this.message = message;
this.groupId = groupId;
this.nGramIds = new ArrayList<Integer>(collection);
} |
0ccb21d3-5e7d-4cb0-a634-805247575a70 | public String getMessage() {
return message;
} |
aeb548da-8d9b-451f-8da9-d7f4c707763b | public int getGroupId() {
return groupId;
} |
f638187e-34f1-496e-b6d4-3979fe5d097e | public ArrayList<Integer> getNGramIds() {
return nGramIds;
} |
196f5a7c-9acf-4953-a5ec-a115ef7bc354 | public SortedPairs() {
this.list = new ArrayList<Entry<F, S>>();
} |
9c0e4d84-2a7e-42a1-a671-abb355e96708 | public void add(F first, S second) {
list.add(new Entry<F, S>(first, second));
} |
2ad51313-b91a-40c8-962c-81d9a8b5f26d | public Pair<F, S> getPair(int index) {
Entry entry = list.get(index);
return new Pair<F, S>((F) entry.first, (S) entry.second);
} |
82fdd789-5752-4963-aadd-c30c3ecdc7ab | public void remove(int index) {
list.remove(index);
} |
0e645d2c-dcf7-4f99-bf68-14135bfae612 | public void sort() {
Collections.sort(list);
} |
37f156a0-df5c-493c-a9c1-def9e79bac5a | public int size() {
return list.size();
} |
66c7af7e-1a34-464b-840b-6b6077d445bf | @Override
public Iterator<Pair<F, S>> iterator() {
return new Iterator<Pair<F, S>>() {
private Iterator<Entry<F, S>> iterator = list.iterator();
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public Pair<F, S> next() {
Entry entry = iterator.next();
return new Pair<F, S>((F) entry.first, (S) entry.second);
}
@Override
public void remove() {
iterator.remove();
}
};
} |
65f01a4f-0a21-4364-bdd9-468ef9e35c3a | @Override
public boolean hasNext() {
return iterator.hasNext();
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.