Skip to content

Commit

Permalink
算法优化
Browse files Browse the repository at this point in the history
  • Loading branch information
MerrickZ committed Mar 18, 2022
1 parent 09a883a commit 1477d94
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 29 deletions.
Binary file removed dist/NewFolderExtension/ExplorerEnhancement.exp
Binary file not shown.
Binary file removed dist/NewFolderExtension/ExplorerEnhancement.pdb
Binary file not shown.
Binary file not shown.
Empty file.
69 changes: 41 additions & 28 deletions src/ExplorerEnhancement/NewFolderExt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ HRESULT __stdcall CNewFolderExt::DoCreateAndMoveItems(CMINVOKECOMMANDINFO* pici,
hr = SHCreateItemFromParsingName((*iter).GetString(), NULL, IID_PPV_ARGS(&pitem));
if (SUCCEEDED(hr)) {
pfo->MoveItem(pitem, pDstFolder, extract_filename((*iter)).GetString(), NULL);
/*
* 2022/3/18 对于每一个移动的文件,向shell发送通知
*/
SHChangeNotify(SHCNE_DELETE, SHCNF_IDLIST, pitem, NULL);
}
}

Expand Down Expand Up @@ -361,7 +365,7 @@ unsigned __stdcall selectItem(void* pArguments) {
CComPtr<IShellWindows> shellwindow;
HRESULT h = CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_ALL, IID_PPV_ARGS(&shellwindow));
if (FAILED(h)) { return 0; }

long count;
shellwindow->get_Count(&count);
for (long i = 0; i < count; i++) {
Expand Down Expand Up @@ -426,19 +430,44 @@ unsigned __stdcall selectItem(void* pArguments) {
/// <param name="str1"></param>
/// <param name="str2"></param>
/// <returns></returns>
int CNewFolderExt::first_common_substring_length(const ATL::CString& str1, const ATL::CString& str2)
int CNewFolderExt::first_common_substring_length(const ATL::CString& str1, const ATL::CString& str2, int length_to_compare)
{
int len = min(str1.GetLength(), str2.GetLength());
int breakpoint = 0;
for (int i = 0; i < len; i++) {
if (str1.GetAt(i) == str2.GetAt(i)) {
breakpoint = i + 1;
}
else {
/*
* 2022/3/18 重构,增加length_to_compare参数,用于提高处理速度。
*/
int l = min(str1.GetLength(), str2.GetLength());
l = min(l, length_to_compare);
int result = l;
for (int i = 0; i < l; i++) {
if (str1[i] != str2[i]) {
result = i;
break;
}
}
return breakpoint;
return result;
}

/// <summary>
/// 求一组字符串的最长公共前导子串
/// </summary>
/// <param name="strs"></param>
/// <returns></returns>
ATL::CString CNewFolderExt::longestCommonPrefix(const std::vector<ATL::CString>& strs)
{
/*
* 2022/3/18 重构,增加length_to_compare参数,用于提高处理速度。
*/
ATL::CString base = strs[0];
int common_prefix_length = base.GetLength();
for (int i = 1; i < strs.size(); i++) {
common_prefix_length = first_common_substring_length(base, strs[i], common_prefix_length);
}
if (common_prefix_length == 0) {
return ATL::CString("");
}
else {
return base.Left(common_prefix_length);
}
}

/// <summary>
Expand All @@ -460,25 +489,9 @@ ATL::CString CNewFolderExt::find_common_prefix(const std::vector<ATL::CString> f
filenames.push_back(_filename);
}

ATL::CString common_part = longestCommonPrefix(filenames);

ATL::CString common_part = filenames.at(0);
int differpoint = MAX_PATH;
for (auto iter = filenames.begin() + 1; iter != filenames.end(); iter++) {
size_t _differ_at = first_common_substring_length(common_part, (*iter));
if (_differ_at < differpoint) {
differpoint = (int)_differ_at;
common_part = common_part.Left(differpoint);
}
if (0 == differpoint) {
break;
}
}
if (0 == differpoint) {
return ATL::CString("");
}
else {
return common_part.Trim();
}
return common_part.Trim();
}

/// <summary>
Expand Down
11 changes: 10 additions & 1 deletion src/ExplorerEnhancement/NewFolderExt.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,16 @@ END_COM_MAP()

HRESULT __stdcall DoCreateAndMoveItems(CMINVOKECOMMANDINFO* pici, IShellItemArray* psiItemArray);

int first_common_substring_length(const ATL::CString& str1, const ATL::CString& str2);
/*
* 2022/3/18
* 增加 length_to_compare 参数,提高处理速度
*/
int first_common_substring_length(const ATL::CString& str1, const ATL::CString& str2, int length_to_compare);
/*
* 2022/3/18
* 该函数已提交leetcode,用于提高处理速度
*/
ATL::CString longestCommonPrefix(const std::vector<ATL::CString>& strs);

ATL::CString find_common_prefix(const std::vector<ATL::CString> filepaths, OUT ATL::CString* default_path);

Expand Down

0 comments on commit 1477d94

Please sign in to comment.