Common: Add HeapArray class

This commit is contained in:
Connor McLaughlin 2019-10-27 13:34:13 +10:00
parent 41574d92e8
commit 273d807e31
6 changed files with 131 additions and 1 deletions

View file

@ -147,6 +147,30 @@ u32 CDImage::Read(ReadMode read_mode, u32 sector_count, void* buffer)
return sectors_read;
}
bool CDImage::ReadRawSector(void* buffer)
{
if (m_position_in_index == m_current_index->length)
{
if (!Seek(m_position_on_disc))
return false;
}
Assert(m_current_index->file);
// get raw sector
if (std::fread(buffer, RAW_SECTOR_SIZE, 1, m_current_index->file) != 1)
{
Log_ErrorPrintf("Read of LBA %u failed", m_position_on_disc);
Seek(m_position_on_disc);
return false;
}
m_position_on_disc++;
m_position_in_index++;
m_position_in_track++;
return true;
}
const CDImage::Index* CDImage::GetIndexForDiscPosition(LBA pos)
{
for (const Index& index : m_indices)

View file

@ -123,6 +123,9 @@ public:
// Read from the current LBA. Returns the number of sectors read.
u32 Read(ReadMode read_mode, u32 sector_count, void* buffer);
// Read a single raw sector from the current LBA.
bool ReadRawSector(void* buffer);
protected:
struct Track
{

View file

@ -41,6 +41,7 @@
<ClInclude Include="fifo_queue.h" />
<ClInclude Include="gl_program.h" />
<ClInclude Include="gl_texture.h" />
<ClInclude Include="heap_array.h" />
<ClInclude Include="jit_code_buffer.h" />
<ClInclude Include="state_wrapper.h" />
<ClInclude Include="types.h" />

View file

@ -11,6 +11,7 @@
<ClInclude Include="cd_image.h" />
<ClInclude Include="audio_stream.h" />
<ClInclude Include="cd_xa.h" />
<ClInclude Include="heap_array.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="jit_code_buffer.cpp" />

94
src/common/heap_array.h Normal file
View file

@ -0,0 +1,94 @@
#pragma once
#include <algorithm>
#include <type_traits>
template<typename T, std::size_t SIZE>
class HeapArray
{
public:
using value_type = T;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using reference = T&;
using const_reference = const T&;
using pointer = T*;
using const_pointer = const T*;
using this_type = typename HeapArray<T, SIZE>;
HeapArray() { m_data = new T[size]; }
HeapArray(const this_type& copy)
{
m_data = new T[size];
std::copy(copy.cbegin(), copy.cend(), begin());
}
HeapArray(const this_type&& move)
{
m_data = move.m_data;
move.m_data = nullptr;
}
~HeapArray() { delete[] m_data; }
size_type size() const { return SIZE; }
size_type capacity() const { return SIZE; }
bool empty() const { return false; }
pointer begin() { return m_data; }
pointer end() { return m_data + SIZE; }
const_pointer data() const { return m_data; }
pointer data() { return m_data; }
const_pointer cbegin() const { return m_data; }
const_pointer cend() const { return m_data + SIZE; }
const_reference operator[](size_type index) const { return m_data[index]; }
reference operator[](size_type index) { return m_data[index]; }
const_reference front() const { return m_data[0]; }
const_reference back() const { return m_data[SIZE - 1]; }
reference front() { return m_data[0]; }
reference back() { return m_data[SIZE - 1]; }
void fill(const_reference value) { std::fill(begin(), end(), value); }
void swap(this_type& move) { std::swap(m_data, move.m_data); }
this_type& operator=(const this_type& rhs)
{
std::copy(begin(), end(), rhs.cbegin());
return *this;
}
this_type& operator=(const this_type&& move)
{
delete[] m_data;
m_data = move.m_data;
move.m_data = nullptr;
return *this;
}
#define RELATIONAL_OPERATOR(op) \
bool operator##op(const this_type& rhs) const \
{ \
for (size_type i = 0; i < SIZE; i++) \
{ \
if (!(m_data[i] op rhs.m_data[i])) \
return false; \
} \
}
RELATIONAL_OPERATOR(==);
RELATIONAL_OPERATOR(!=);
RELATIONAL_OPERATOR(<);
RELATIONAL_OPERATOR(<=);
RELATIONAL_OPERATOR(>);
RELATIONAL_OPERATOR(>=);
#undef RELATIONAL_OPERATOR
private:
T* m_data;
};

View file

@ -1,6 +1,7 @@
#pragma once
#include "YBaseLib/ByteStream.h"
#include "fifo_queue.h"
#include "heap_array.h"
#include "types.h"
#include <cstring>
#include <deque>
@ -110,6 +111,12 @@ public:
DoArray(data->data(), data->size());
}
template<typename T, size_t N>
void Do(HeapArray<T, N>* data)
{
DoArray(data->data(), data->size());
}
template<typename T>
void Do(std::vector<T>* data)
{