Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Tham khảo tài liệu 'c++ timesaving techniques for dummies phần 7', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Technique 48 Implementing Virtual Files Listing 48-1 continued void setOffset long offset _offset _offset void setLength long length _length length void setChunk const string chunk _chunk chunk long AccessCount void return _accesses bool Load ifstream in long offset long length Clear return Read in offset length const int kChunkSize 128 class FileChunkManager - 2 private int _numChunks FileChunk _chunks ifstream _in string _fileName protected FileChunk findChunk long theOffset for int i 0 i _numChunks i if _chunks i .InUse true long offset _chunks i .Offset long length _chunks i .Length if theOffset offset theOffset offset length return _chunks i return NULL FileChunk addChunk long theOffset Creating a Virtual File Class 287 for int i 0 i _numChunks i if _chunks i .InUse false if _chunks i .Load _in theOffset kChunkSize return _chunks i return NULL FileChunk getLeastRecentlyAccessed int idx 0 long access _chunks 0 .AccessCount for int i 0 i _numChunks i if _chunks i .InUse true if _chunks i .AccessCount access idx i access _chunks i .AccessCount return _chunks idx public FileChunkManager void _numChunks 0 _chunks NULL FileChunkManager const char fileName int nMaxChunks _numChunks nMaxChunks _chunks new FileChunk nMaxChunks _fileName fileName _in.open fileName FileChunkManager const FileChunkManager aCopy _numChunks aCopy._numChunks _chunks new FileChunk _numChunks for int i 0 i _numChunks i _chunks i aCopy._chunks i _fileName aCopy._fileName _in.open _fileName.c_str continued Technique 48 Implementing Virtual Files Listing 48-1 continued virtual FileChunkManager void delete _chunks char operator long offset Find which chunk this offset is in. FileChunk chunk findChunk offset if chunk NULL There are none. See whether we can add one. chunk addChunk offset If we have one just get the data from it. Otherwise we have to go dump one. if chunk chunk getLeastRecentlyAccessed chunk- Load _in offset kChunkSize Finally extract the piece we need. int pos offset - chunk- Offset