aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/IntBuffer.cpp
blob: 1bb5e26a5caeeebd09e3bbd7fe96b2ceaf7e638f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "stdafx.h"

#include "IntBuffer.h"

//Allocates a new int buffer.
//The new buffer's position will be zero, its limit will be its capacity, and its mark will be undefined.
//It will have a backing array, and its array offset will be zero.
//
//Parameters:
//capacity - The new buffer's capacity, in ints
IntBuffer::IntBuffer(unsigned int capacity) : Buffer( capacity )
{
	buffer = new int[capacity];
	memset( buffer,0,sizeof(int)*capacity);
}

IntBuffer::IntBuffer( unsigned int capacity, int *backingArray ) : Buffer( capacity )
{
	hasBackingArray = true;
	buffer = backingArray;
}

IntBuffer::~IntBuffer()
{
	if( !hasBackingArray )
		delete[] buffer;
}

int *IntBuffer::getBuffer()
{
	return buffer;
}

//Flips this buffer. The limit is set to the current position and then the position is set to zero.
//If the mark is defined then it is discarded.
//
//Returns:
//This buffer
IntBuffer *IntBuffer::flip()
{
	m_limit = m_position;
	m_position = 0;
	return this;
}

//Absolute get method. Reads the int at the given index.
//Parameters:
//index - The index from which the int will be read
//Returns:
//The int at the given index
int IntBuffer::get(unsigned int index)
{
	assert( index < m_limit );

	return buffer[index];
}

//Relative bulk put method  (optional operation).
//This method transfers ints into this buffer from the given source array.
//If there are more ints to be copied from the array than remain in this buffer, that is, if length > remaining(),
//then no ints are transferred and a BufferOverflowException is thrown.
//
//Otherwise, this method copies length ints from the given array into this buffer, starting at the given offset in the array
//and at the current position of this buffer. The position of this buffer is then incremented by length.
//
//In other words, an invocation of this method of the form dst.put(src, off, len) has exactly the same effect as the loop
//
//     for (int i = off; i < off + len; i++)
//         dst.put(a[i]); 
//except that it first checks that there is sufficient space in this buffer and it is potentially much more efficient.
//Parameters:
//src - The array from which ints are to be read
//offset - The offset within the array of the first int to be read; must be non-negative and no larger than array.length
//length - The number of ints to be read from the given array; must be non-negative and no larger than array.length - offset
//Returns:
//This buffer
IntBuffer *IntBuffer::put(intArray *inputArray, unsigned int offset, unsigned int length)
{
	assert( offset + length < inputArray->length );

	std::copy( inputArray->data +offset, inputArray->data +offset+length, buffer+m_position );

	m_position += length;

	return this;
}

IntBuffer *IntBuffer::put(intArray inputArray)
{
	if( inputArray.length > remaining() )
		assert( false ); //TODO 4J Stu - Some kind of exception?

	std::copy( inputArray.data, inputArray.data + inputArray.length, buffer+m_position );

	m_position += inputArray.length;

	return this;
}

//Writes the given int into this buffer at the current position, and then increments the position.
//
//Parameters:
//i - The int to be written
//Returns:
//This buffer
IntBuffer *IntBuffer::put(int i)
{
	assert( m_position < m_limit );

	buffer[m_position++] = i;

	return this;
}