Browse code

Use std::copy_n/std::fill_n instead of std::copy/std::fill where possible.

Naram Qashat authored on 2014/09/08 16:52:49
Showing 5 changed files
... ...
@@ -264,7 +264,7 @@ void Blip_Synth_::treble_eq(const blip_eq_t &eq)
264 264
 		fimpulse[blip_res + half_size + i] = fimpulse[blip_res + half_size - 1 - i];
265 265
 
266 266
 	// starts at 0
267
-	std::fill(&fimpulse[0], &fimpulse[blip_res], 0.0f);
267
+	std::fill_n(&fimpulse[0], blip_res, 0.0f);
268 268
 
269 269
 	// find rescale factor
270 270
 	double total = std::accumulate(&fimpulse[blip_res], &fimpulse[blip_res + half_size], 0.0);
... ...
@@ -83,7 +83,7 @@ void Gb_Apu::volume(double v)
83 83
 
84 84
 void Gb_Apu::reset_regs()
85 85
 {
86
-	std::fill(&this->regs[0], &this->regs[0x20], 0);
86
+	std::fill_n(&this->regs[0], 0x20, 0);
87 87
 
88 88
 	this->square1.reset();
89 89
 	this->square2.reset();
... ...
@@ -1505,7 +1505,7 @@ void CPUInit()
1505 1505
 		cpuBitsSet[i] = count;
1506 1506
 	}
1507 1507
 
1508
-	std::fill(&ioReadable[0], &ioReadable[0x304], true);
1508
+	std::fill_n(&ioReadable[0], 0x304, true);
1509 1509
 	std::fill(&ioReadable[0x10], &ioReadable[0x48], false);
1510 1510
 	std::fill(&ioReadable[0x4c], &ioReadable[0x50], false);
1511 1511
 	std::fill(&ioReadable[0x54], &ioReadable[0x60], false);
... ...
@@ -102,11 +102,11 @@ template<size_t N> struct RingBuffer
102 102
 
103 103
 	RingBuffer() : bufferPos(N / 2), getPos(N / 2)
104 104
 	{
105
-		std::fill(&this->buffer[0], &this->buffer[N * 2], 0);
105
+		std::fill_n(&this->buffer[0], N * 2, 0);
106 106
 	}
107 107
 	void Clear()
108 108
 	{
109
-		std::fill(&this->buffer[0], &this->buffer[N * 2], 0);
109
+		std::fill_n(&this->buffer[0], N * 2, 0);
110 110
 		this->bufferPos = this->getPos = N / 2;
111 111
 	}
112 112
 	void PushSample(int16_t sample)
... ...
@@ -125,11 +125,11 @@ template<size_t N> struct RingBuffer
125 125
 		if (this->bufferPos + size > N * 3 / 2)
126 126
 		{
127 127
 			size_t free = N * 3 / 2 - this->bufferPos;
128
-			std::copy(&samples[0], &samples[free], &this->buffer[this->bufferPos]);
128
+			std::copy_n(&samples[0], free, &this->buffer[this->bufferPos]);
129 129
 			std::copy(&samples[free], &samples[size], &this->buffer[N / 2]);
130 130
 		}
131 131
 		else
132
-			std::copy(&samples[0], &samples[size], &this->buffer[this->bufferPos]);
132
+			std::copy_n(&samples[0], size, &this->buffer[this->bufferPos]);
133 133
 		size_t rightFree = this->bufferPos < N ? N - this->bufferPos : 0;
134 134
 		if (rightFree < size)
135 135
 		{
... ...
@@ -137,18 +137,18 @@ template<size_t N> struct RingBuffer
137 137
 			{
138 138
 				size_t leftStart = this->bufferPos - N;
139 139
 				size_t leftSize = std::min(N / 2 - leftStart, size);
140
-				std::copy(&samples[0], &samples[leftSize], &this->buffer[leftStart]);
140
+				std::copy_n(&samples[0], leftSize, &this->buffer[leftStart]);
141 141
 				if (leftSize < size)
142 142
 					std::copy(&samples[leftSize], &samples[size], &this->buffer[N * 3 / 2]);
143 143
 			}
144 144
 			else
145 145
 			{
146
-				std::copy(&samples[0], &samples[rightFree], &this->buffer[this->bufferPos + N]);
146
+				std::copy_n(&samples[0], rightFree, &this->buffer[this->bufferPos + N]);
147 147
 				std::copy(&samples[rightFree], &samples[size], &this->buffer[0]);
148 148
 			}
149 149
 		}
150 150
 		else
151
-			std::copy(&samples[0], &samples[size], &this->buffer[this->bufferPos + N]);
151
+			std::copy_n(&samples[0], size, &this->buffer[this->bufferPos + N]);
152 152
 		this->bufferPos += size;
153 153
 		if (this->bufferPos >= N * 3 / 2)
154 154
 			this->bufferPos -= N;
... ...
@@ -66,12 +66,12 @@ bool XSFPlayer::FillBuffer(std::vector<uint8_t> &buf, unsigned &samplesWritten)
66 66
 		if (this->uses32BitSamplesClampedTo16Bit)
67 67
 		{
68 68
 			auto trueBufLong = reinterpret_cast<int32_t *>(&trueBuffer[0]);
69
-			std::copy(&trueBufLong[0], &trueBufLong[bufsize << 1], &bufLong[0]);
69
+			std::copy_n(&trueBufLong[0], bufsize << 1, &bufLong[0]);
70 70
 		}
71 71
 		else
72 72
 		{
73 73
 			auto trueBufShort = reinterpret_cast<int16_t *>(&trueBuffer[0]);
74
-			std::copy(&trueBufShort[0], &trueBufShort[bufsize << 1], &bufLong[0]);
74
+			std::copy_n(&trueBufShort[0], bufsize << 1, &bufLong[0]);
75 75
 		}
76 76
 		if (detectSilence || skipSilenceOnStartSec)
77 77
 		{
... ...
@@ -117,7 +117,7 @@ bool XSFPlayer::FillBuffer(std::vector<uint8_t> &buf, unsigned &samplesWritten)
117 117
 				{
118 118
 					auto tmpBuf = std::vector<int32_t>((bufsize - skipOffset) << 1);
119 119
 					std::copy(&bufLong[(offset + skipOffset) << 1], &bufLong[bufsize << 1], &tmpBuf[0]);
120
-					std::copy(&tmpBuf[0], &tmpBuf[(bufsize - skipOffset) << 1], &bufLong[offset << 1]);
120
+					std::copy_n(&tmpBuf[0], (bufsize - skipOffset) << 1, &bufLong[offset << 1]);
121 121
 					pos += skipOffset;
122 122
 				}
123 123
 				else
... ...
@@ -131,12 +131,12 @@ bool XSFPlayer::FillBuffer(std::vector<uint8_t> &buf, unsigned &samplesWritten)
131 131
 			if (this->uses32BitSamplesClampedTo16Bit)
132 132
 			{
133 133
 				auto trueBufLong = reinterpret_cast<int32_t *>(&trueBuffer[0]);
134
-				std::copy(&bufLong[0], &bufLong[bufsize << 1], &trueBufLong[0]);
134
+				std::copy_n(&bufLong[0], bufsize << 1, &trueBufLong[0]);
135 135
 			}
136 136
 			else
137 137
 			{
138 138
 				auto trueBufShort = reinterpret_cast<int16_t *>(&trueBuffer[0]);
139
-				std::copy(&bufLong[0], &bufLong[bufsize << 1], &trueBufShort[0]);
139
+				std::copy_n(&bufLong[0], bufsize << 1, &trueBufShort[0]);
140 140
 			}
141 141
 		}
142 142
 	}
... ...
@@ -188,7 +188,7 @@ bool XSFPlayer::FillBuffer(std::vector<uint8_t> &buf, unsigned &samplesWritten)
188 188
 	else
189 189
 	{
190 190
 		auto trueBufShort = reinterpret_cast<int16_t *>(&trueBuffer[0]);
191
-		std::copy(&bufLong[0], &bufLong[bufsize << 1], &trueBufShort[0]);
191
+		std::copy_n(&bufLong[0], bufsize << 1, &trueBufShort[0]);
192 192
 		std::copy(&trueBuffer[0], &trueBuffer[bufsize << 2], &buf[0]);
193 193
 	}
194 194