51 lines
1.5 KiB
C#
51 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace file_finder__test;
|
|
|
|
public class FileClassifier
|
|
{
|
|
public async Task<(List<string> musicFiles, List<string> videoFiles)> ClassifyFilesAsync(
|
|
List<string> allFiles,
|
|
List<string> musicExtensions,
|
|
List<string> videoExtensions)
|
|
{
|
|
int coreCount = Environment.ProcessorCount;
|
|
int totalFiles = allFiles.Count;
|
|
int chunkSize = (int)Math.Ceiling((double)totalFiles / coreCount);
|
|
|
|
var musicBag = new ConcurrentBag<string>();
|
|
var videoBag = new ConcurrentBag<string>();
|
|
|
|
var tasks = new List<Task>();
|
|
|
|
for (int i = 0; i < coreCount; i++)
|
|
{
|
|
int start = i * chunkSize;
|
|
int end = Math.Min(start + chunkSize, totalFiles);
|
|
|
|
tasks.Add(Task.Run(() =>
|
|
{
|
|
for (int j = start; j < end; j++)
|
|
{
|
|
string file = allFiles[j];
|
|
string ext = Path.GetExtension(file)?.ToLowerInvariant();
|
|
|
|
if (ext == null)
|
|
continue;
|
|
|
|
if (musicExtensions.Contains(ext))
|
|
musicBag.Add(file);
|
|
else if (videoExtensions.Contains(ext))
|
|
videoBag.Add(file);
|
|
}
|
|
}));
|
|
}
|
|
|
|
await Task.WhenAll(tasks);
|
|
return (new List<string>(musicBag), new List<string>(videoBag));
|
|
}
|
|
} |