public void Update(int index, AnnotationRecord record)
 {
     _annotationRecords[index] = record.Clone();
 }
        MergeMatFileRecordsAndCacheRecords(IList <AnnotationRecord> matFileRecords, IList <bool> matFileRecordValidity,
                                           IList <AnnotationRecordCache> cacheRecords, IList <string> imageFileNameList)
        {
            bool matFileUpdateRequired = false;

            IList <AnnotationRecord> mergeResults = new List <AnnotationRecord>(imageFileNameList.Count);
            IDictionary <int, AnnotationRecordCache> cacheUpdateToDoList = new Dictionary <int, AnnotationRecordCache>();

            int maxRecordsLength = imageFileNameList.Count;

            if (matFileRecords.Count != maxRecordsLength)
            {
                matFileUpdateRequired = true;
            }

            // Trust chain: cache > mat file

            int firstPhaseEndIndex = Math.Min(Math.Min(maxRecordsLength, matFileRecords.Count), cacheRecords.Count);

            for (int index = 0; index < firstPhaseEndIndex; ++index)
            {
                AnnotationRecord mergedRecord = matFileRecords[index];

                var cacheResult = cacheRecords[index];

                bool isAnnotationRecordIdentityToCache = IsAnnotationRecordIdentityToCache(mergedRecord, cacheResult);
                bool isEmptyRecord = IsEmptyRecord(cacheResult);
                if (!isAnnotationRecordIdentityToCache && !isEmptyRecord)
                {
                    mergedRecord          = ConvertAnnotationRecordCacheToRecord(cacheResult);
                    matFileUpdateRequired = true;
                }
                else if (!isAnnotationRecordIdentityToCache && isEmptyRecord)
                {
                    mergedRecord = mergedRecord.Clone();
                    cacheUpdateToDoList.Add(index, ConvertAnnotationRecordToCache(mergedRecord));
                }
                else
                {
                    mergedRecord = mergedRecord.Clone();
                }

                string path = imageFileNameList[index];
                if (mergedRecord.Path != path)
                {
                    mergedRecord.Path     = path;
                    matFileUpdateRequired = true;
                }

                if (!matFileRecordValidity[index])
                {
                    matFileUpdateRequired = true;
                }

                mergeResults.Add(mergedRecord);
            }

            int  secondPhaseEndIndex      = Math.Min(Math.Max(matFileRecords.Count, cacheRecords.Count), maxRecordsLength);
            bool secondPhaseDoWithMatFile = matFileRecords.Count > cacheRecords.Count;

            if (secondPhaseDoWithMatFile)
            {
                for (int index = firstPhaseEndIndex; index < secondPhaseEndIndex; ++index)
                {
                    AnnotationRecord mergedRecord = matFileRecords[index].Clone();
                    cacheUpdateToDoList.Add(index, ConvertAnnotationRecordToCache(mergedRecord));

                    string path = imageFileNameList[index];
                    if (mergedRecord.Path != path)
                    {
                        mergedRecord.Path     = path;
                        matFileUpdateRequired = true;
                    }

                    if (!matFileRecordValidity[index])
                    {
                        matFileUpdateRequired = true;
                    }

                    mergeResults.Add(mergedRecord);
                }
            }
            else
            {
                for (int index = firstPhaseEndIndex; index < secondPhaseEndIndex; ++index)
                {
                    AnnotationRecord mergedRecord = ConvertAnnotationRecordCacheToRecord(cacheRecords[index]);
                    mergedRecord.Path     = imageFileNameList[index];
                    matFileUpdateRequired = true;

                    mergeResults.Add(mergedRecord);
                }
            }

            int thirdPhaseEndIndex = maxRecordsLength;

            for (int index = secondPhaseEndIndex; index < thirdPhaseEndIndex; ++index)
            {
                AnnotationRecord mergedRecord = CreateEmptyAnnotationRecord();
                mergedRecord.Path     = imageFileNameList[index];
                matFileUpdateRequired = true;
                cacheUpdateToDoList.Add(index, CreateEmptyAnnotationRecordCache());

                mergeResults.Add(mergedRecord);
            }

            return(mergeResults, cacheUpdateToDoList, matFileUpdateRequired);
        }