Skip to content

Commit 48ac12d

Browse files
committed
refactor: 修改编码和解码函数以返回状态
1 parent 13b0434 commit 48ac12d

3 files changed

Lines changed: 11 additions & 13 deletions

File tree

algorithms/huffman/cpp/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,12 @@ static bool decompress_file(const std::string& input_path, const std::string& ou
343343
return ok;
344344
}
345345

346-
void huffman_encode_file(const std::string& input_path, const std::string& output_path) {
347-
(void)compress_file(input_path, output_path);
346+
bool huffman_encode_file(const std::string& input_path, const std::string& output_path) {
347+
return compress_file(input_path, output_path);
348348
}
349349

350-
void huffman_decode_file(const std::string& input_path, const std::string& output_path) {
351-
(void)decompress_file(input_path, output_path);
350+
bool huffman_decode_file(const std::string& input_path, const std::string& output_path) {
351+
return decompress_file(input_path, output_path);
352352
}
353353

354354
int main(int argc, char** argv) {

algorithms/huffman/go/huffman.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,13 @@ func BuildFrequenciesFromFile(path string) ([]uint32, error) {
174174
freq := make([]uint32, SymbolLimit)
175175
f, err := os.Open(path)
176176
if err != nil {
177-
freq[EOFSymbol] = 1
178-
return freq, nil
177+
return nil, fmt.Errorf("cannot open input file: %s: %w", path, err)
179178
}
180179
defer f.Close()
181180

182181
stat, err := f.Stat()
183182
if err != nil {
184-
freq[EOFSymbol] = 1
185-
return freq, nil
183+
return nil, fmt.Errorf("cannot stat input file: %s: %w", path, err)
186184
}
187185
if stat.Size() > MaxInputSize {
188186
return nil, fmt.Errorf("input file too large (max %d bytes)", MaxInputSize)
@@ -349,7 +347,7 @@ func Decode(r io.Reader, w io.Writer) error {
349347
}
350348
node = root
351349
}
352-
if bitReader.EOF() && node.IsLeaf() == false && node.Left == nil && node.Right == nil {
350+
if bitReader.EOF() && node == root {
353351
break
354352
}
355353
}

algorithms/rle/cpp/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ static bool read_u32_le(std::istream& in, uint32_t& out_value) {
5252
static bool rle_encode_file_checked(const std::string& input_path, const std::string& output_path);
5353
static bool rle_decode_file_checked(const std::string& input_path, const std::string& output_path);
5454

55-
void rle_encode_file(const std::string& input_path, const std::string& output_path) {
56-
(void)rle_encode_file_checked(input_path, output_path);
55+
bool rle_encode_file(const std::string& input_path, const std::string& output_path) {
56+
return rle_encode_file_checked(input_path, output_path);
5757
}
5858

5959
// Perform Run-Length encoding on entire file.
@@ -170,8 +170,8 @@ static bool rle_decode_file_checked(const std::string& input_path, const std::st
170170
return true;
171171
}
172172

173-
void rle_decode_file(const std::string& input_path, const std::string& output_path) {
174-
(void)rle_decode_file_checked(input_path, output_path);
173+
bool rle_decode_file(const std::string& input_path, const std::string& output_path) {
174+
return rle_decode_file_checked(input_path, output_path);
175175
}
176176

177177
int main(int argc, char** argv) {

0 commit comments

Comments
 (0)