-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement binary string table support
- Loading branch information
Showing
6 changed files
with
123 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
namespace ValveKeyValue | ||
{ | ||
public sealed class StringTable | ||
{ | ||
public StringTable(Memory<string> values) | ||
{ | ||
this.lookup = values; | ||
} | ||
|
||
readonly Memory<string> lookup; | ||
Dictionary<string, int> reverse; | ||
|
||
public string this[int index] | ||
{ | ||
get | ||
{ | ||
if (index < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(index), "Index must be non-negative."); | ||
} | ||
|
||
if (index >= lookup.Length) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(index), index, "Index must be less than the number of strings in the table."); | ||
} | ||
|
||
return lookup.Span[index]; | ||
} | ||
} | ||
|
||
public int IndexOf(string value) | ||
{ | ||
if (reverse is null) | ||
{ | ||
throw new InvalidOperationException("String table has not been prepared for serialization."); | ||
} | ||
|
||
return reverse[value]; | ||
} | ||
|
||
public void PrepareForSerialization() | ||
{ | ||
if (reverse is not null) | ||
{ | ||
return; | ||
} | ||
|
||
reverse = new Dictionary<string, int>(capacity: lookup.Length, StringComparer.Ordinal); | ||
var span = lookup.Span; | ||
|
||
for (var i = 0; i < span.Length; i++) | ||
{ | ||
var value = span[i]; | ||
reverse[value] = i; | ||
} | ||
} | ||
} | ||
} |