/// <summary>
        /// I'm too lazy to comment this one. It basically finds the biggest previous clump of bytes that can replicate the current bytes
        /// </summary>
        private static ClumpInfo FindBestClumpOrig(byte[] rawData, int offset, int maxReadBack, int maxByteClump, int minByteClump)
        {
            ClumpInfo currentBestClump = new ClumpInfo(-1, -1, -1);

            int startRef = Math.Max(offset - maxReadBack, 0);

            for (int refOffset = startRef; refOffset < offset && refOffset < rawData.Length; refOffset += 2)
            {
                int matchCount = 0;
                while (matchCount < maxByteClump && offset + matchCount < rawData.Length && rawData[refOffset + matchCount] == rawData[offset + matchCount])
                {
                    matchCount++;
                }

                if (matchCount % 2 == 1)
                {
                    matchCount--;
                }

                if (matchCount >= minByteClump && matchCount > currentBestClump.Length)
                {
                    currentBestClump = new ClumpInfo(offset, offset - refOffset, matchCount);
                    if (matchCount == maxByteClump)
                    {
                        break;
                    }
                }
            }

            return(currentBestClump);
        }
        private static bool FindBestAlternativeClumps(byte[] rawData, int maxReadBack, int maxByteClump, int minByteClump,
                                                      List <ClumpInfo> origClumps, int clumpOffset)
        {
            //The idea is to shave off a byte at the beginning of the clump. If it improves the # of bytes used, then goodie goodie!

            //To do: look into shaving the end as well!
            int  oldLayoutBitCount = 1;
            int  oldByteCount      = 2;
            bool isInOldClump      = true;
            int  oldClumpOffset    = clumpOffset;

            int  newLayoutBitCount = 1;
            int  newByteCount      = 1;
            bool isInNewClump      = false;
            int  newClumpOffset    = 0;

            List <ClumpInfo> newClumps = new List <ClumpInfo>();

            int byteOffset = origClumps[clumpOffset].Offset + 1;

            while (byteOffset < rawData.Length)
            {
                if ((!isInNewClump || newClumps[newClumpOffset].FollowingByte == byteOffset) &&
                    (!isInOldClump || origClumps[oldClumpOffset].FollowingByte == byteOffset))
                {
                    break; //We are synced again, make sure to increment the oldClumpOffset so you don't overwrite a clump
                }

                //If we're not in a clump, try to generate a clump. If successful, add it in and jump in.
                if (isInNewClump)
                {
                    //Check if we need to exit
                    if (newClumps[newClumpOffset].FollowingByte == byteOffset)
                    {
                        newClumpOffset++;
                        //Try to generate new clump
                        ClumpInfo newClump = FindBestClump(rawData, byteOffset, maxReadBack, maxByteClump, minByteClump);
                        if (newClump.Offset != -1)
                        {
                            //jump into new clump
                            newClumps.Add(newClump);
                            newLayoutBitCount++;
                            newByteCount += 2;
                        }
                        else
                        {
                            isInNewClump = false;
                            newLayoutBitCount++;
                            newByteCount++;
                        }
                    }
                }
                else
                {
                    //Try to generate.
                    ClumpInfo newClump = FindBestClump(rawData, byteOffset, maxReadBack, maxByteClump, minByteClump);
                    if (newClump.Offset != -1)
                    {
                        //jump into new clump
                        newClumps.Add(newClump);
                        isInNewClump = true;
                        newLayoutBitCount++;
                        newByteCount += 2;
                    }
                    else
                    {
                        newLayoutBitCount++;
                        newByteCount++;
                    }
                }

                //Handle the old info
                if (isInOldClump)
                {
                    //Check if we need to exit
                    if (origClumps[oldClumpOffset].FollowingByte == byteOffset)
                    {
                        oldClumpOffset++;
                        if (oldClumpOffset < origClumps.Count && origClumps[oldClumpOffset].Offset == byteOffset)
                        {
                            //jump into new clump
                            oldLayoutBitCount++;
                            oldByteCount += 2;
                        }
                        else
                        {
                            isInOldClump = false;
                            oldLayoutBitCount++;
                            oldByteCount++;
                        }
                    }
                }
                else
                {
                    //Check if we enter
                    if (oldClumpOffset < origClumps.Count && origClumps[oldClumpOffset].Offset == byteOffset)
                    {
                        isInOldClump = true;
                        oldLayoutBitCount++;
                        oldByteCount += 2;
                    }
                    else
                    {
                        oldLayoutBitCount++;
                        oldByteCount++;
                    }
                }

                //if (isInNewClump && isInOldClump && origClumps[oldClumpOffset].LastByte == newClumps[newClumpOffset].LastByte)
                //    break; //We are synced again

                //if (!isInOldClump && !isInNewClump)
                //    break; //We are synced again

                byteOffset++;
            }

            if (newByteCount > oldByteCount || (newByteCount == oldByteCount && newLayoutBitCount >= oldLayoutBitCount))
            {
                return(false);
            }

            //make sure to go back an index if we're not in a clump
            if (!isInOldClump)
            {
                oldClumpOffset--;
            }

            //Apply
            int j;

            for (j = oldClumpOffset; j > clumpOffset; j--)
            {
                origClumps.RemoveAt(j);
            }
            origClumps.RemoveAt(j);

            while (newClumps.Count > 0)
            {
                origClumps.Insert(j, newClumps.Last());
                newClumps.RemoveAt(newClumps.Count - 1);
            }

            return(true);

            //Idea: have an algorithm that'll take a given clump, bump one byte off the start and step through bytes, incrementing
            // the old and new clumps until they match up again. Remember to try to create clumps before ending the algorithm. If
            // you can't drop it, consider recursively running the alogritm over it again, allowing for one more extra raw byte to be
            // injected somewhere. If that doesn't work, failure!
        }
Esempio n. 3
0
        private static ClumpInfo FindBestClump(byte[] rawData, int offset, int maxReadBack, int maxByteClump, int minByteClump)
        {
            ClumpInfo currentBestClump = new ClumpInfo(-1, -1, -1);

            int startRef = Math.Max(offset - maxReadBack, 0);

            for (int refOffset = startRef; refOffset < offset && refOffset < rawData.Length; refOffset++)
            {
                int matchCount = 0;
                while (matchCount < maxByteClump && offset + matchCount < rawData.Length && rawData[refOffset + matchCount] == rawData[offset + matchCount])
                    matchCount++;

                if (matchCount >= minByteClump && matchCount > currentBestClump.Length)
                {
                    currentBestClump = new ClumpInfo(offset, offset - refOffset, matchCount);
                    if (matchCount == maxByteClump)
                        break;
                }
            }

            return currentBestClump;
        }