summaryrefslogtreecommitdiff
path: root/src/events_ds.h
blob: 6d2959ee67c7445badc97bb048c21e5c9bbfb646 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* -*- Mode: c++ -*- */
/***************************************************************************
 *            events_ds.h
 *
 *  Sun Jan 12 00:17:31 CET 2020
 *  Copyright 2020 André Nusser
 *  andre.nusser@googlemail.com
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#pragma once

#include <config.h>

#include <algorithm>
#include <array>
#include <vector>

#include "events.h"
#include "id.h"
#include "memory_heap.h"
#include "range.h"
#include "instrument.h"

//! EventsDS (events data structure) is the central engine events data structure.
//! It enables the following fast operations:
//! * Add a new event
//! * Remove an event
//! * Iterate over all events of a channel
//! * Get all events of a certain group
//! * ...
//!
//! All operations are constant time (assuming bounded group sizes). Also, it
//! assignes the event ids and group ids. Certain functions may be specific to certain event types.
//!
//! This data structure makes several assumptions about its usage:
//! * we assume that we add a group in one batch without deleting anything in-between
//! * we assume that an event_id isn't used after remove(event_id) was called
//! * we assume that before starting to emplace a new group, startAddingNewGroup is called

// TODO: make it possible to choose sizes of all the containers on initialization
// such that we avoid allocations all together as long as we stay in these bounds.

class EventsDS
{
public:
	//! Adds a new event of type T (via emplace syntax) to the data structure.
	template <typename T, typename... Args>
	T& emplace(int ch, Args&&... args);

	//! Removes the event with id being event_id.
	void remove(EventID event_id);

	//! Returns the number of all events of a certain channel.
	std::size_t numberOfEvents(int ch) const;

	//! Gives a range that can be used in a range based for loop to iterate over
	//! all events in channel ch of type T.
	template <typename T>
	ContainerRange<std::vector<T>> iterateOver(int ch);

	//! Returns all the group ids of sample events associated with instrument_id.
	const EventGroupIDs& getSampleEventGroupIDsOf(InstrumentID instrument_id) const;

	//! Get all the event ids of events that are in the group with event_group_id.
	const EventIDs& getEventIDsOf(EventGroupID event_group_id) const;

	//! This function should always be called before we emplace a new batch of
	//! events. This internally creates a new group id and then assigns this group
	//! id to all the events that are emplaced afterwards. The group id changes,
	//! when startAddingNewGroup is again called.
	void startAddingNewGroup(InstrumentID instrument_id = InstrumentID());

private:
	struct ChannelData
	{
		std::vector<SampleEvent> sample_events;
	};

	using ChannelEventIndex = std::size_t;

	struct EventInfo
	{
		Event::Type type;
		int ch;
		ChannelEventIndex channel_event_index;

		EventInfo(Event::Type type, int ch, ChannelEventIndex channel_event_index)
			: type(type), ch(ch), channel_event_index(channel_event_index) {}
	};

	struct GroupData
	{
		EventIDs event_ids;
		// Note: In the future, we could make groups just consist of events of one type.
		// Currently that is not necessary, but untested.
		// Event::Type type;

		// SampleEvent specific data
		std::size_t instrument_index;
	};

	// general data
	std::array<ChannelData, NUM_CHANNELS> channel_data_array;
	MemoryHeap<EventInfo> id_to_info;
	MemoryHeap<GroupData> id_to_group_data;

	// SampleEvent specific data
	// TODO: Make this into a vector and always have the size be the number of instruments.
	std::array<EventGroupIDs, 128> instruments_sample_event_group_ids;

	EventGroupID current_group_id;
	InstrumentID current_groups_instrument_id;

	void removeGroup(EventGroupID group_id, InstrumentID instrument_id = InstrumentID());
};

template <typename T, typename... Args>
T& EventsDS::emplace(int ch, Args&&... args)
{
	// add new event types here
	static_assert(std::is_same<T, SampleEvent>::value);

	if (std::is_same<T, SampleEvent>::value)
	{
		auto& sample_events = channel_data_array[ch].sample_events;
		auto channel_event_index = sample_events.size();
		sample_events.emplace_back(std::forward<Args>(args)...);

		auto event_id = id_to_info.emplace(Event::Type::SampleEvent, ch, channel_event_index);
		id_to_group_data.get(current_group_id).event_ids.push_back(event_id);

		auto& sample_event = sample_events.back();
		sample_event.id = event_id;
		sample_event.group_id = current_group_id;
		assert(sample_event.instrument_id == current_groups_instrument_id);

		return sample_event;
	}
}

template <typename T>
ContainerRange<std::vector<T>> EventsDS::iterateOver(int ch)
{
	// add new event types here
	static_assert(std::is_same<T, SampleEvent>::value);

	if (std::is_same<T, SampleEvent>::value)
	{
		auto& sample_events = channel_data_array[ch].sample_events;
		return ContainerRange<std::vector<T>>(sample_events.begin(), sample_events.end());
	}
}