1/*
2Copyright 2011 Google Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16Author: lode.vandevenne@gmail.com (Lode Vandevenne)
17Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
18*/
19
20/*
21The cache that speeds up ZopfliFindLongestMatch of lz77.c.
22*/
23
24#ifndef ZOPFLI_CACHE_H_
25#define ZOPFLI_CACHE_H_
26
27#include "util.h"
28
29#ifdef ZOPFLI_LONGEST_MATCH_CACHE
30
31/*
32Cache used by ZopfliFindLongestMatch to remember previously found length/dist
33values.
34This is needed because the squeeze runs will ask these values multiple times for
35the same position.
36Uses large amounts of memory, since it has to remember the distance belonging
37to every possible shorter-than-the-best length (the so called "sublen" array).
38*/
39typedef struct ZopfliLongestMatchCache {
40  unsigned short* length;
41  unsigned short* dist;
42  unsigned char* sublen;
43} ZopfliLongestMatchCache;
44
45/* Initializes the ZopfliLongestMatchCache. */
46void ZopfliInitCache(size_t blocksize, ZopfliLongestMatchCache* lmc);
47
48/* Frees up the memory of the ZopfliLongestMatchCache. */
49void ZopfliCleanCache(ZopfliLongestMatchCache* lmc);
50
51/* Stores sublen array in the cache. */
52void ZopfliSublenToCache(const unsigned short* sublen,
53                         size_t pos, size_t length,
54                         ZopfliLongestMatchCache* lmc);
55
56/* Extracts sublen array from the cache. */
57void ZopfliCacheToSublen(const ZopfliLongestMatchCache* lmc,
58                         size_t pos, size_t length,
59                         unsigned short* sublen);
60/* Returns the length up to which could be stored in the cache. */
61unsigned ZopfliMaxCachedSublen(const ZopfliLongestMatchCache* lmc,
62                               size_t pos, size_t length);
63
64#endif  /* ZOPFLI_LONGEST_MATCH_CACHE */
65
66#endif  /* ZOPFLI_CACHE_H_ */
67