harbor run -t openthoughts/tasktrove-exp-rpt-crosscodeeval-java-crosscodeeval-java-0485harbor download openthoughts/tasktrove-exp-rpt-crosscodeeval-java-crosscodeeval-java-0485solution 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(myByteList.toArray());
write((byte) '\n');
}
/**
* Dump out a 64bit double.
*/
protected final void saveFloat(double o)
{
write(Operations.BINFLOAT);
// The pickle protocol saves Python floats in big-endian format.
write(myEightByteBuffer.order(ByteOrder.BIG_ENDIAN).putDouble(0, o));
}
/**
* Write a 32-or-less bit integer.
*/
protected final void saveInteger(int o)
{
// The pickle protocol saves Python integers in little-endian format.
final byte[] a =
myFourByteBuffer.order(ByteOrder.LITTLE_ENDIAN).putInt(0, o)
.array();
if (a[2] == 0 && a[3] == 0) {
if (a[1] == 0) {
// BININT1 is for integers [0, 256), not [-128, 128).
write(Operations.BININT1);
write(a[0]);
return;
}
// BININT2 is for integers [256, 65536), not [-32768, 32768).
write(Operations.BININT2);
write(a[0]);
write(a[1]);
return;
}
write(Operations.BININT);
write(a);
}
/**
* Write a 64-or-less bit integer.
*/
protected final void saveInteger(long o)
{
if (o <= Integer.MAX_VALUE && o >= Integer.MIN_VALUE) {
saveInteger((int) o);
}
else {
write(Operations.LONG1);
write((byte)8);
write(myEightByteBuffer.order(ByteOrder.LITTLE_ENDIAN).putLong(0, o));
}
}
/**
* Write out a string as real unicode.
*/
protected final void saveUnicode(String o)
{
final byte[] b;
b = o.getBytes(StandardCharsets.UTF_8);
write(Operations.BINUNICODE);
// Pickle protocol is always little-endian
writeLittleEndianInt(b.length);
write(b);
put(o);
}
// ----------------------------------------------------------------------
// Serializing objects intended to be unpickled as numpy arrays
//
// Instead of serializing array-like objects exactly the way numpy
// arrays are serialized, we simply serialize them to be unpickled
// as numpy arrays. The simplest way to do this is to use
// numpy.fromstring(). We write out opcodes to build the
// following stack:
//
// [..., numpy.fromstring, binary_data_string, dtype_string]
//
// and then call TUPLE2 and REDUCE to get:
//
// [..., numpy.fromstring(binary_data_string, dtype_string)]
//
// http://docs.scipy.org/doc/numpy/reference/generated/numpy.fromstring.html
/**
* Save the Python function module.name. We use this function
* with the REDUCE opcode to build Python objects when unpickling.
*/
protected final void saveGlobal(String module, String name)
{
write(Operations.GLOBAL);
writeAscii(module);
writeAscii("\n");
writeAscii(name);
writeAscii("\n");
}
/**
* Save a boolean array as a numpy array.
*/
protected final void saveNumpyBooleanArray(boolean[] o)
{
saveGlobal("numpy", "fromstring");
final int n = o.length;
writeBinStringHeader((long) n);
for (boolean d : o) {
write((byte) (d?1:0));
}
addNumpyArrayEnding(DType.Type.BOOLEAN, o);
}
/**
* Save a byte array as a numpy array.
*/
protected final void saveNumpyByteArray(byte[] o)
{
saveGlobal("numpy", "fromstring");
final int n = o.length;
writeBinStringHeader((long) n);
for (byte d : o) {
write(d);
}
addNumpyArrayEnding(DType.Type.INT8, o);
}
/**
* Save a char array as a numpy array.
*/
protected final void saveNumpyCharArray(char[] o)
{
saveGlobal("numpy", "fromstring");
final int n = o.length;
writeBinStringHeader((long) n);
for (char c : o) {
write(c);
}
addNumpyArrayEnding(DType.Type.CHAR, o);
}
/**
* Save a ByteList as a numpy array.
*/
protected final void saveNumpyByteArray(ByteList o)
{
saveGlobal("numpy", "fromstring");
final int n = o.size();
writeBinStringHeader((long) n);
for (int i=0; i < n; ++i) {
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