mirror of
https://github.com/fadden/6502bench.git
synced 2025-01-20 14:31:17 +00:00
Move symbol sort comparison function into Symbol class
This commit is contained in:
parent
f32135e2c7
commit
7339d2a681
@ -781,13 +781,14 @@ namespace SourceGenWPF.ProjWin {
|
||||
|
||||
switch (col.Header) {
|
||||
case "Type":
|
||||
comparer = new SymTabSortComparer(SymTabSortField.CombinedType, isAscending);
|
||||
comparer = new SymTabSortComparer(Symbol.SymbolSortField.CombinedType,
|
||||
isAscending);
|
||||
break;
|
||||
case "Value":
|
||||
comparer = new SymTabSortComparer(SymTabSortField.Value, isAscending);
|
||||
comparer = new SymTabSortComparer(Symbol.SymbolSortField.Value, isAscending);
|
||||
break;
|
||||
case "Name":
|
||||
comparer = new SymTabSortComparer(SymTabSortField.Name, isAscending);
|
||||
comparer = new SymTabSortComparer(Symbol.SymbolSortField.Name, isAscending);
|
||||
break;
|
||||
default:
|
||||
comparer = null;
|
||||
@ -802,12 +803,11 @@ namespace SourceGenWPF.ProjWin {
|
||||
}
|
||||
|
||||
// Symbol table sort comparison helper.
|
||||
private enum SymTabSortField { CombinedType, Value, Name };
|
||||
private class SymTabSortComparer : IComparer {
|
||||
private SymTabSortField mSortField;
|
||||
private Symbol.SymbolSortField mSortField;
|
||||
private bool mIsAscending;
|
||||
|
||||
public SymTabSortComparer(SymTabSortField prim, bool isAscending) {
|
||||
public SymTabSortComparer(Symbol.SymbolSortField prim, bool isAscending) {
|
||||
mSortField = prim;
|
||||
mIsAscending = isAscending;
|
||||
}
|
||||
@ -817,54 +817,7 @@ namespace SourceGenWPF.ProjWin {
|
||||
Symbol a = ((SymbolsListItem)oa).Sym;
|
||||
Symbol b = ((SymbolsListItem)ob).Sym;
|
||||
|
||||
// Label is always unique, so we use it as a secondary sort.
|
||||
if (mSortField == SymTabSortField.CombinedType) {
|
||||
if (mIsAscending) {
|
||||
int cmp = string.Compare(a.SourceTypeString, b.SourceTypeString);
|
||||
if (cmp == 0) {
|
||||
cmp = string.Compare(a.Label, b.Label);
|
||||
}
|
||||
return cmp;
|
||||
} else {
|
||||
int cmp = string.Compare(a.SourceTypeString, b.SourceTypeString);
|
||||
if (cmp == 0) {
|
||||
// secondary sort is always ascending, so negate
|
||||
cmp = -string.Compare(a.Label, b.Label);
|
||||
}
|
||||
return -cmp;
|
||||
}
|
||||
} else if (mSortField == SymTabSortField.Value) {
|
||||
if (mIsAscending) {
|
||||
int cmp;
|
||||
if (a.Value < b.Value) {
|
||||
cmp = -1;
|
||||
} else if (a.Value > b.Value) {
|
||||
cmp = 1;
|
||||
} else {
|
||||
cmp = string.Compare(a.Label, b.Label);
|
||||
}
|
||||
return cmp;
|
||||
} else {
|
||||
int cmp;
|
||||
if (a.Value < b.Value) {
|
||||
cmp = -1;
|
||||
} else if (a.Value > b.Value) {
|
||||
cmp = 1;
|
||||
} else {
|
||||
cmp = -string.Compare(a.Label, b.Label);
|
||||
}
|
||||
return -cmp;
|
||||
}
|
||||
} else if (mSortField == SymTabSortField.Name) {
|
||||
if (mIsAscending) {
|
||||
return string.Compare(a.Label, b.Label);
|
||||
} else {
|
||||
return -string.Compare(a.Label, b.Label);
|
||||
}
|
||||
} else {
|
||||
Debug.Assert(false);
|
||||
return 0;
|
||||
}
|
||||
return Symbol.Compare(mSortField, mIsAscending, a, b);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -152,5 +152,66 @@ namespace SourceGenWPF {
|
||||
return Asm65.Label.ToNormal(Label).GetHashCode() ^
|
||||
Value ^ (int)SymbolType ^ (int)SymbolSource;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Comparison function, used when sorting the symbol table.
|
||||
//
|
||||
|
||||
public enum SymbolSortField { CombinedType, Value, Name };
|
||||
|
||||
public static int Compare(SymbolSortField sortField, bool isAscending,
|
||||
Symbol a, Symbol b) {
|
||||
// In the symbol table, only the label field is guaranteed to be unique. We
|
||||
// use it as a secondary sort key when comparing the other fields.
|
||||
switch (sortField) {
|
||||
case SymbolSortField.CombinedType:
|
||||
if (isAscending) {
|
||||
int cmp = string.Compare(a.SourceTypeString, b.SourceTypeString);
|
||||
if (cmp == 0) {
|
||||
cmp = string.Compare(a.Label, b.Label);
|
||||
}
|
||||
return cmp;
|
||||
} else {
|
||||
int cmp = string.Compare(a.SourceTypeString, b.SourceTypeString);
|
||||
if (cmp == 0) {
|
||||
// secondary sort is always ascending, so negate
|
||||
cmp = -string.Compare(a.Label, b.Label);
|
||||
}
|
||||
return -cmp;
|
||||
}
|
||||
case SymbolSortField.Value:
|
||||
if (isAscending) {
|
||||
int cmp;
|
||||
if (a.Value < b.Value) {
|
||||
cmp = -1;
|
||||
} else if (a.Value > b.Value) {
|
||||
cmp = 1;
|
||||
} else {
|
||||
cmp = string.Compare(a.Label, b.Label);
|
||||
}
|
||||
return cmp;
|
||||
} else {
|
||||
int cmp;
|
||||
if (a.Value < b.Value) {
|
||||
cmp = -1;
|
||||
} else if (a.Value > b.Value) {
|
||||
cmp = 1;
|
||||
} else {
|
||||
cmp = -string.Compare(a.Label, b.Label);
|
||||
}
|
||||
return -cmp;
|
||||
}
|
||||
case SymbolSortField.Name:
|
||||
if (isAscending) {
|
||||
return string.Compare(a.Label, b.Label);
|
||||
} else {
|
||||
return -string.Compare(a.Label, b.Label);
|
||||
}
|
||||
default:
|
||||
Debug.Assert(false);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user