@@ -191,7 +191,7 @@ function initSearch(rawSearchIndex) {
191
191
*/
192
192
let searchIndex ;
193
193
let currentResults ;
194
- const ALIASES = Object . create ( null ) ;
194
+ const ALIASES = new Map ( ) ;
195
195
196
196
function isWhitespace ( c ) {
197
197
return " \t\n\r" . indexOf ( c ) !== - 1 ;
@@ -903,10 +903,18 @@ function initSearch(rawSearchIndex) {
903
903
* @return {ResultsTable }
904
904
*/
905
905
function execQuery ( parsedQuery , searchWords , filterCrates , currentCrate ) {
906
- const results_others = { } , results_in_args = { } , results_returned = { } ;
906
+ const results_others = new Map ( ) , results_in_args = new Map ( ) ,
907
+ results_returned = new Map ( ) ;
907
908
909
+ /**
910
+ * Add extra data to result objects, and filter items that have been
911
+ * marked for removal.
912
+ *
913
+ * @param {[ResultObject] } results
914
+ * @returns {[ResultObject] }
915
+ */
908
916
function transformResults ( results ) {
909
- const duplicates = { } ;
917
+ const duplicates = new Set ( ) ;
910
918
const out = [ ] ;
911
919
912
920
for ( const result of results ) {
@@ -919,10 +927,10 @@ function initSearch(rawSearchIndex) {
919
927
// To be sure than it some items aren't considered as duplicate.
920
928
obj . fullPath += "|" + obj . ty ;
921
929
922
- if ( duplicates [ obj . fullPath ] ) {
930
+ if ( duplicates . has ( obj . fullPath ) ) {
923
931
continue ;
924
932
}
925
- duplicates [ obj . fullPath ] = true ;
933
+ duplicates . add ( obj . fullPath ) ;
926
934
927
935
obj . href = res [ 1 ] ;
928
936
out . push ( obj ) ;
@@ -934,24 +942,30 @@ function initSearch(rawSearchIndex) {
934
942
return out ;
935
943
}
936
944
945
+ /**
946
+ * This function takes a result map, and sorts it by various criteria, including edit
947
+ * distance, substring match, and the crate it comes from.
948
+ *
949
+ * @param {Results } results
950
+ * @param {boolean } isType
951
+ * @param {string } preferredCrate
952
+ * @returns {[ResultObject] }
953
+ */
937
954
function sortResults ( results , isType , preferredCrate ) {
938
- const userQuery = parsedQuery . userQuery ;
939
- const ar = [ ] ;
940
- for ( const entry in results ) {
941
- if ( hasOwnPropertyRustdoc ( results , entry ) ) {
942
- const result = results [ entry ] ;
943
- result . word = searchWords [ result . id ] ;
944
- result . item = searchIndex [ result . id ] || { } ;
945
- ar . push ( result ) ;
946
- }
947
- }
948
- results = ar ;
949
955
// if there are no results then return to default and fail
950
- if ( results . length === 0 ) {
956
+ if ( results . size === 0 ) {
951
957
return [ ] ;
952
958
}
953
959
954
- results . sort ( ( aaa , bbb ) => {
960
+ const userQuery = parsedQuery . userQuery ;
961
+ const result_list = [ ] ;
962
+ for ( const result of results . values ( ) ) {
963
+ result . word = searchWords [ result . id ] ;
964
+ result . item = searchIndex [ result . id ] || { } ;
965
+ result_list . push ( result ) ;
966
+ }
967
+
968
+ result_list . sort ( ( aaa , bbb ) => {
955
969
let a , b ;
956
970
957
971
// sort by exact match with regard to the last word (mismatch goes later)
@@ -1060,7 +1074,7 @@ function initSearch(rawSearchIndex) {
1060
1074
nameSplit = hasPath ? null : parsedQuery . elems [ 0 ] . path ;
1061
1075
}
1062
1076
1063
- for ( const result of results ) {
1077
+ for ( const result of result_list ) {
1064
1078
// this validation does not make sense when searching by types
1065
1079
if ( result . dontValidate ) {
1066
1080
continue ;
@@ -1073,7 +1087,7 @@ function initSearch(rawSearchIndex) {
1073
1087
result . id = - 1 ;
1074
1088
}
1075
1089
}
1076
- return transformResults ( results ) ;
1090
+ return transformResults ( result_list ) ;
1077
1091
}
1078
1092
1079
1093
/**
@@ -1096,7 +1110,7 @@ function initSearch(rawSearchIndex) {
1096
1110
// The names match, but we need to be sure that all generics kinda
1097
1111
// match as well.
1098
1112
if ( elem . generics . length > 0 && row . generics . length >= elem . generics . length ) {
1099
- const elems = Object . create ( null ) ;
1113
+ const elems = new Map ( ) ;
1100
1114
for ( const entry of row . generics ) {
1101
1115
if ( entry . name === "" ) {
1102
1116
// Pure generic, needs to check into it.
@@ -1106,39 +1120,30 @@ function initSearch(rawSearchIndex) {
1106
1120
}
1107
1121
continue ;
1108
1122
}
1109
- if ( elems [ entry . name ] === undefined ) {
1110
- elems [ entry . name ] = [ ] ;
1123
+ let currentEntryElems ;
1124
+ if ( elems . has ( entry . name ) ) {
1125
+ currentEntryElems = elems . get ( entry . name ) ;
1126
+ } else {
1127
+ currentEntryElems = [ ] ;
1128
+ elems . set ( entry . name , currentEntryElems ) ;
1111
1129
}
1112
- elems [ entry . name ] . push ( entry . ty ) ;
1130
+ currentEntryElems . push ( entry . ty ) ;
1113
1131
}
1114
1132
// We need to find the type that matches the most to remove it in order
1115
1133
// to move forward.
1116
1134
const handleGeneric = generic => {
1117
- let match = null ;
1118
- if ( elems [ generic . name ] ) {
1119
- match = generic . name ;
1120
- } else {
1121
- for ( const elem_name in elems ) {
1122
- if ( ! hasOwnPropertyRustdoc ( elems , elem_name ) ) {
1123
- continue ;
1124
- }
1125
- if ( elem_name === generic ) {
1126
- match = elem_name ;
1127
- break ;
1128
- }
1129
- }
1130
- }
1131
- if ( match === null ) {
1135
+ if ( ! elems . has ( generic . name ) ) {
1132
1136
return false ;
1133
1137
}
1134
- const matchIdx = elems [ match ] . findIndex ( tmp_elem =>
1138
+ const matchElems = elems . get ( generic . name ) ;
1139
+ const matchIdx = matchElems . findIndex ( tmp_elem =>
1135
1140
typePassesFilter ( generic . typeFilter , tmp_elem ) ) ;
1136
1141
if ( matchIdx === - 1 ) {
1137
1142
return false ;
1138
1143
}
1139
- elems [ match ] . splice ( matchIdx , 1 ) ;
1140
- if ( elems [ match ] . length === 0 ) {
1141
- delete elems [ match ] ;
1144
+ matchElems . splice ( matchIdx , 1 ) ;
1145
+ if ( matchElems . length === 0 ) {
1146
+ elems . delete ( generic . name ) ;
1142
1147
}
1143
1148
return true ;
1144
1149
} ;
@@ -1424,22 +1429,22 @@ function initSearch(rawSearchIndex) {
1424
1429
const aliases = [ ] ;
1425
1430
const crateAliases = [ ] ;
1426
1431
if ( filterCrates !== null ) {
1427
- if ( ALIASES [ filterCrates ] && ALIASES [ filterCrates ] [ lowerQuery ] ) {
1428
- const query_aliases = ALIASES [ filterCrates ] [ lowerQuery ] ;
1432
+ if ( ALIASES . has ( filterCrates ) && ALIASES . get ( filterCrates ) . has ( lowerQuery ) ) {
1433
+ const query_aliases = ALIASES . get ( filterCrates ) . get ( lowerQuery ) ;
1429
1434
for ( const alias of query_aliases ) {
1430
1435
aliases . push ( createAliasFromItem ( searchIndex [ alias ] ) ) ;
1431
1436
}
1432
1437
}
1433
1438
} else {
1434
- Object . keys ( ALIASES ) . forEach ( crate => {
1435
- if ( ALIASES [ crate ] [ lowerQuery ] ) {
1439
+ for ( const [ crate , crateAliasesIndex ] of ALIASES ) {
1440
+ if ( crateAliasesIndex . has ( lowerQuery ) ) {
1436
1441
const pushTo = crate === currentCrate ? crateAliases : aliases ;
1437
- const query_aliases = ALIASES [ crate ] [ lowerQuery ] ;
1442
+ const query_aliases = crateAliasesIndex . get ( lowerQuery ) ;
1438
1443
for ( const alias of query_aliases ) {
1439
1444
pushTo . push ( createAliasFromItem ( searchIndex [ alias ] ) ) ;
1440
1445
}
1441
1446
}
1442
- } ) ;
1447
+ }
1443
1448
}
1444
1449
1445
1450
const sortFunc = ( aaa , bbb ) => {
@@ -1496,19 +1501,19 @@ function initSearch(rawSearchIndex) {
1496
1501
function addIntoResults ( results , fullId , id , index , dist , path_dist , maxEditDistance ) {
1497
1502
const inBounds = dist <= maxEditDistance || index !== - 1 ;
1498
1503
if ( dist === 0 || ( ! parsedQuery . literalSearch && inBounds ) ) {
1499
- if ( results [ fullId ] !== undefined ) {
1500
- const result = results [ fullId ] ;
1504
+ if ( results . has ( fullId ) ) {
1505
+ const result = results . get ( fullId ) ;
1501
1506
if ( result . dontValidate || result . dist <= dist ) {
1502
1507
return ;
1503
1508
}
1504
1509
}
1505
- results [ fullId ] = {
1510
+ results . set ( fullId , {
1506
1511
id : id ,
1507
1512
index : index ,
1508
1513
dontValidate : parsedQuery . literalSearch ,
1509
1514
dist : dist ,
1510
1515
path_dist : path_dist ,
1511
- } ;
1516
+ } ) ;
1512
1517
}
1513
1518
}
1514
1519
@@ -2345,17 +2350,22 @@ function initSearch(rawSearchIndex) {
2345
2350
}
2346
2351
2347
2352
if ( aliases ) {
2348
- ALIASES [ crate ] = Object . create ( null ) ;
2353
+ const currentCrateAliases = new Map ( ) ;
2354
+ ALIASES . set ( crate , currentCrateAliases ) ;
2349
2355
for ( const alias_name in aliases ) {
2350
2356
if ( ! hasOwnPropertyRustdoc ( aliases , alias_name ) ) {
2351
2357
continue ;
2352
2358
}
2353
2359
2354
- if ( ! hasOwnPropertyRustdoc ( ALIASES [ crate ] , alias_name ) ) {
2355
- ALIASES [ crate ] [ alias_name ] = [ ] ;
2360
+ let currentNameAliases ;
2361
+ if ( currentCrateAliases . has ( alias_name ) ) {
2362
+ currentNameAliases = currentCrateAliases . get ( alias_name ) ;
2363
+ } else {
2364
+ currentNameAliases = [ ] ;
2365
+ currentCrateAliases . set ( alias_name , currentNameAliases ) ;
2356
2366
}
2357
2367
for ( const local_alias of aliases [ alias_name ] ) {
2358
- ALIASES [ crate ] [ alias_name ] . push ( local_alias + currentIndex ) ;
2368
+ currentNameAliases . push ( local_alias + currentIndex ) ;
2359
2369
}
2360
2370
}
2361
2371
}
0 commit comments