harbor run -t openthoughts/tasktrove-exp-rpt-crosscodeeval-java-crosscodeeval-java-0478harbor download openthoughts/tasktrove-exp-rpt-crosscodeeval-java-crosscodeeval-java-0478solution file
package com.deshaw.python;
import com.deshaw.util.ByteList;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.RandomAccess;
/**
* Serialization of basic Java objects into a format compatible with Python's
* pickle protocol. This should allow objects to be marshalled from Java into
* Python.
*
* <p>This class is not thread-safe.
*/
public class PythonPickle
{
// Keep in sync with pickle.Pickler._BATCHSIZE. This is how many elements
// batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
// break if this gets out of sync with pickle.py, but it's unclear that
// would help anything either.
private static final int BATCHSIZE = 1000;
private static final byte MARK_V = Operations.MARK.code;
// ----------------------------------------------------------------------
/**
* We buffer up everything in here for dumping.
*/
private final ByteArrayOutputStream myStream = new ByteArrayOutputStream();
/**
* Used to provide a handle on objects which we have already stored (so that
* we don't duplicate them in the result).
*/
private final IdentityHashMap<Object,Integer> myMemo = new IdentityHashMap<>();
// Scratch space
private final ByteBuffer myTwoByteBuffer = ByteBuffer.allocate(2);
private final ByteBuffer myFourByteBuffer = ByteBuffer.allocate(4);
private final ByteBuffer myEightByteBuffer = ByteBuffer.allocate(8);
private final ByteList myByteList = new ByteList();
// ----------------------------------------------------------------------
/**
* Dump an object out to a given stream.
*/
public void toStream(Object o, OutputStream stream)
throws IOException
{
// Might be better to use the stream directly, rather than staging
// locally.
toPickle(o);
myStream.writeTo(stream);
}
/**
* Dump to a byte-array.
*/
public byte[] toByteArray(Object o)
{
toPickle(o);
return myStream.toByteArray();
}
/**
* Pickle an arbitrary object which isn't handled by default.
*
* <p>Subclasses can override this to extend the class's behaviour.
*
* @throws UnsupportedOperationException if the object could not be pickled.
*/
protected void saveObject(Object o)
throws UnsupportedOperationException
{
throw new UnsupportedOperationException(
"Cannot pickle " + o.getClass().getCanonicalName()
);
}
// ----------------------------------------------------------------------------
// Methods which subclasses might need to extend funnctionality
/**
* Get back the reference to a previously saved object.
*/
protected final void get(Object o)
{
writeOpcodeForValue(Operations.BINGET,
Operations.LONG_BINGET,
myMemo.get(o));
}
/**
* Save a reference to an object.
*/
protected final void put(Object o)
{
// 1-indexed; see comment about positve vs. non-negative in Python's
// C pickle code
final int n = myMemo.size() + 1;
myMemo.put(o, n);
writeOpcodeForValue(Operations.BINPUT,
Operations.LONG_BINPUT,
n);
}
/**
* Write out an opcode, depending on the size of the 'n' value we are
* encoding (i.e. if it fits in a byte).
*/
protected final void writeOpcodeForValue(Operations op1, Operations op5, int n)
{
if (n < 256) {
write(op1);
write((byte) n);
}
else {
write(op5);
// The pickle protocol saves this in little-endian format.
writeLittleEndianInt(n);
}
}
/**
* Dump out a string as ASCII.
*/
protected final void writeAscii(String s)
{
write(s.getBytes(StandardCharsets.US_ASCII));
}
/**
* Write out a byte.
*/
protected final void write(Operations op)
{
myStream.write(op.code);
}
/**
* Write out a byte.
*/
protected final void write(byte i)
{
myStream.write(i);
}
/**
* Write out a char.
*/
protected final void write(char c)
{
myStream.write(c);
}
/**
* Write out an int, in little-endian format.
*/
protected final void writeLittleEndianInt(final int n)
{
write(myFourByteBuffer.order(ByteOrder.LITTLE_ENDIAN).putInt(0, n));
}
/**
* Write out the contents of a ByteBuffer.
*/
protected final void write(ByteBuffer b)
{
write(b.array());
}
/**
* Write out the contents of a byte array.
*/
protected final void write(byte[] array)
{
myStream.write(array, 0, array.length);
}
/**
* Dump out a 32bit float.
*/
protected final void saveFloat(float o)
{
myByteList.clear();
myByteList.append(Float.toString(o).getBytes());
write(Operations.FLOAT);
write
Complete the code above, taking into account the cross-file context provided. Your solution must work correctly with the related files in the project.
Place your complete solution in /app/solution.py