Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
a10y committed Aug 13, 2024
1 parent d4cc929 commit 9062072
Show file tree
Hide file tree
Showing 6 changed files with 420 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"

[lints.rust]
warnings = "deny"
missing_docs = "deny"
# missing_docs = "deny"

[lints.clippy]
all = { level = "deny", priority = -1 }
Expand Down
12 changes: 8 additions & 4 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use std::cmp::Ordering;
use std::collections::BinaryHeap;

use crate::find_longest::FindLongestSymbol;
use crate::{Code, Symbol, SymbolTable};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -147,10 +148,13 @@ impl SymbolTable {
}

// Pop the 255 best symbols.
pqueue
.iter()
.take(255)
.for_each(|candidate| res.insert(candidate.symbol));
let mut n_symbols = 0;
while !pqueue.is_empty() && n_symbols < 255 {
let candidate = pqueue.pop().unwrap();
if res.insert(candidate.symbol) {
n_symbols += 1;
}
}

res
}
Expand Down
21 changes: 21 additions & 0 deletions src/find_longest/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2024 Spiral, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::Code;

mod naive;

pub trait FindLongestSymbol {
fn find_longest_symbol(&self, text: &[u8]) -> Code;
}
5 changes: 3 additions & 2 deletions src/longest.rs → src/find_longest/naive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::find_longest::FindLongestSymbol;
use crate::{Code, SymbolTable};

/// Find the longest substring.

impl SymbolTable {
impl FindLongestSymbol for SymbolTable {
// NOTE(aduffy): if you don't disable inlining, this function won't show up in profiles.
#[inline(never)]
pub(crate) fn find_longest_symbol(&self, text: &[u8]) -> Code {
fn find_longest_symbol(&self, text: &[u8]) -> Code {
debug_assert!(!text.is_empty(), "text must not be empty");

// Find the code that best maps to the provided text table here.
Expand Down
Loading

0 comments on commit 9062072

Please sign in to comment.