Skip to content

Instantly share code, notes, and snippets.

@oonyanya
Created December 23, 2025 06:43
Show Gist options
  • Select an option

  • Save oonyanya/1b39883a9f847be8131362384c18c7df to your computer and use it in GitHub Desktop.

Select an option

Save oonyanya/1b39883a9f847be8131362384c18c7df to your computer and use it in GitHub Desktop.
マルチコア対応、改行コードごとに行を分割するコード
//ForEachLinesの内容ははLineToIndex.csを参照すること
IList<LineToIndexTableData> CreateLineList(long index, long length,bool setdirtyflag = false, int lineLimitLength = -1)
{
const int CoreNum = 4;
const long thresholdParallelCreateList = CoreNum + 1;
//const long thresholdParallelCreateList = 1024 * 1024;
//const long thresholdParallelCreateList = int.MaxValue - 1;
if (length < thresholdParallelCreateList)
{
long startIndex = index;
long endIndex = index + length - 1;
List<LineToIndexTableData> output = new List<LineToIndexTableData>();
foreach (var range in this.ForEachLines(startIndex, endIndex, lineLimitLength))
{
long lineHeadIndex = range.Item1;
long lineLength = range.Item2;
char c = this.Document[lineHeadIndex + lineLength - 1];
bool hasNewLine = range.Item3 != string.Empty;
LineToIndexTableData result = new LineToIndexTableData(lineHeadIndex, lineLength, hasNewLine, setdirtyflag, null);
output.Add(result);
}
return output;
}
else
{
var spilitLength = length / CoreNum;
var spilitStartIndex = index;
List<(long startIndex, long length)> spilitter = new List<(long startIndex, long length)>();
for (int i = 0; i < CoreNum; i++)
{
var actualSpilitLength = spilitLength;
while(true)
{
var currentIndex = spilitStartIndex + actualSpilitLength - 1;
if (currentIndex > index + length - 1)
break;
if(this.Document[currentIndex] == Document.CR_CHAR)
{
if(currentIndex + 1 < this.Document.Length && this.Document[currentIndex + 1] == Document.LF_CHAR)
{
actualSpilitLength++;
break;
}
else
{
break;
}
}
if (this.Document[currentIndex] == Document.LF_CHAR)
break;
actualSpilitLength++;
}
if (spilitStartIndex + actualSpilitLength > index + length - 1)
actualSpilitLength = index + length -1 - spilitStartIndex + 1;
spilitter.Add((spilitStartIndex, actualSpilitLength));
spilitStartIndex += actualSpilitLength;
}
var result = spilitter.AsParallel().Select((item) => {
var startIndex = item.startIndex;
var endIndex = startIndex + item.length - 1;
List<LineToIndexTableData> output = new List<LineToIndexTableData>();
foreach (var range in this.ForEachLines(startIndex, endIndex, lineLimitLength))
{
long lineHeadIndex = range.Item1;
long lineLength = range.Item2;
char c = this.Document[lineHeadIndex + lineLength - 1];
bool hasNewLine = range.Item3 != string.Empty;
LineToIndexTableData result = new LineToIndexTableData(lineHeadIndex, lineLength, hasNewLine, setdirtyflag, null);
output.Add(result);
}
return (startIndex, output);
})
.OrderBy(item => item.startIndex)
.SelectMany(item => item.output)
.ToList();
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment