1 / 24

Data Abstraction and Encapsulation

Data Abstraction and Encapsulation. Definition: Data Encapsulation or Information Hiding is the concealing of the implementation details of a data object from the outside world. Data Abstraction and Encapsulation.

deion
Download Presentation

Data Abstraction and Encapsulation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Abstraction and Encapsulation • Definition: Data Encapsulation or Information Hiding is the concealing of the implementation details of a data object from the outsideworld.

  2. Data Abstraction and Encapsulation • Definition: Data Abstraction is the separation between the specification of a data object and its implementation. • Definition: A data type is a collection of objects and a set of operations that act on those objects.

  3. Advantages of Data Abstraction and Data Encapsulation • Simplification of software development • Testing and Debugging • Reusability

  4. Data Abstraction and Encapsulation • Definition: An abstract data type (ADT) is a data type that is organized in such a way that the specification of the objects and the specification of the operations on the objects is separated from the representation of the objects and the implementation of the operations.

  5. Sparse Matrices

  6. ADT 2.3 Abstract data type SparseMatrix class SparseMatrix // objects: A set of triples, <row, column, value>, where row and column are integers and form a unique combinations; value is also an integer. public: SparseMatrix(int MaxRow, int MaxCol); SparseMatrixTranspose(); SparseMatrixAdd(SparseMatrix b); SparseMatrix Multiply(SparseMatrix b); };

  7. Sparse Matrix Representation • Use triple <row, column, value> • Store triples row by row • For all triples within a row, their column indices are in ascending order. • Must know the number of rows and columns and the number of nonzero elements

  8. Sparse Matrix Representation (Cont.) classSparseMatrix; // forward declaration classMatrixTerm { friendclassSparseMatrix private: int row, col, value; }; classSparseMatrix: private: int Rows, Cols, Terms; MatrixTermsmArray[MaxTerms];

  9. Transposing A Matrix • Intuitive way: for (each row i) take element (i, j, value) and store it in (j, i, value) of the transpose • More efficient way: for (all elements in column j) place element (i, j, value) in position (j, i, value)

  10. Program 2.10 Transposing a Matrix SparseMatrix SparseMatrix::Transpose() // return the transpose of a (*this) { SparseMatrix b; b.Rows = Cols; // rows in b = columns in a b.Cols = Rows; // columns in b = rows in a b.Terms = Terms; // terms in b = terms in a if (Terms > 0) // nonzero matrix { int CurrentB = 0; for (int c = 0; c < Cols; c++) // transpose by columns for (int i = 0; i < Terms; i++) // find elements in column c if (smArray[i].col == c) { b.smArray[CurrentB].row = c; b.smArray[CurrentB].col = smArray[i].row; b.smArray[CurrentB].value = smArray[i].value; CurrentB++; } } // end of if (Terms > 0) } // end of transpose O(terms*columns)

  11. Fast Matrix Transpose • The O(terms*columns) time => O(rows*columns2) when terms is the order of rows*columns • A better transpose function in Program 2.11. It first computes how many terms in each columns of matrix a before transposing to matrix b. Then it determines where is the starting point of each row for matrix b. Finally it moves each term from a to b.

  12. Program 2.11 Fast Matrix Transposing SparseMatrix SparseMatrix::Transpose() // The transpose of a(*this) is placed in b and is found in Q(terms + columns) time. { int *RowSize = new int[Cols]; int *RowStart = new int[Cols]; SparseMatrix b; b.Rows = Cols; b.Cols = Rows; b.Terms = Terms; if (Terms > 0) // nonzero matrix { // compute RowSize[i] = number of terms in row i of b for (int i = 0; i< Cols; i++) RowSize[i] = 0; // Initialize for ( i= 0; i < Terms; i++) RowSize[smArray[i].col]++; // RowStart[i] = starting position of row i in b RowStart[0] = 0; for (i = 1; i < Cols; i++) RowStart[i] = RowStart[i-1] + RowSize[i-1]; O(columns) O(terms) O(columns-1)

  13. Program 2.11 Fast Matrix Transposing (Cont.) for (i = 0; i < Terms; i++) // move from a to b { int j = RowStart[smArray[i].col]; b.smArray[j].row = smArray[i].col; b.smArray[j].col = smArray[i].row; b.smArray[j].value = smArray[i].value; RowStart[smArray[i].col]++; } // end of for } // end of if delete [] RowSize; delete [] RowStart; return b; } // end of FastTranspose O(terms) O(row * column)

  14. Representation of Arrays • Multidimensional arrays are usually implemented by one dimensional array via row major order. • Example: One dimensional array α α+1 α+2 α+3 α+4 α+5 α+6 α+7 α+8 α+9 α+10 α+11 A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]

  15. Two Dimensional Array Row Major Order Col 0 Col 1 Col 2 Col u2 - 1 Row 0 X X X X X X X X Row 1 Row u1 - 1 X X X X u2 elements u2 elements Row u1 - 1 Row i Row 0 Row 1 i * u2 element

  16. Generalizing Array Representation The address indexing of Array A[i1][i2],…,[in] is α+ i1 u2 u3 … un + i2 u3 u4 … un + i3 u4 u5 … un : : + in-1 un + in =α+

  17. intString::Find(Stringpat) • {// 如果在 *this 字串裡面找不到pat,那麼傳回 -1;否則回傳pat在 *this 裡的起始位置。 • for (intstart = 0;start <= Length( )-pat.Length();star ++) • { //從 str [start]開始檢查有沒有相同的字元 • intj; • for (j = 0;j < pat.Length( )&&str [start+j] = = pat.str[j]; j++) • if (j = = pat.Length( )) returnstart; //找到相同的字串 • //在 start 這個位置沒找到匹配 • } • return-1 ; // pat為空字串或者不存在於在s中 • } • 樣式比對 • 假設有兩個字串string和pat,其中pat是要在string中找尋的樣式。

  18. 範例:假設pat = “aab”且string = “ababbaabaa”。下圖儲存nfind如何比對pat 和string中的字元。 • 圖:模擬nfind j lastp (a) 樣式 start endmatch lasts (b)不同

  19. start endmatch lasts (c) 不同 start endmatch lasts (d) 不同 start endmatch lasts (e) 不同 start endmatch lasts (f) 不同

  20. start endmatch lasts (g) 相同 • 樣式的失敗函數(failure function)定義: 若P=p0p1…pn-1為一個樣式,則其失敗函數f定義如下: 對於上述的範例樣式pat = “abcabcacab”,我們可得:

  21. 根據失敗函數的定義,我們可以知道:如果找到部分的匹配使得根據失敗函數的定義,我們可以知道:如果找到部分的匹配使得 ,則比對工作可以從比較  和 繼續。若j=0,則從比較   和  繼續。這樣的樣式比對規則較換成函數pmatch。

  22. 程式:Knuth,Morris,Pratt的樣式比對演算法 • int String::FastFind(String pat) • 2 {// 決定 pat 是否為 s的子字串。 • 3 int posP = 0, posS = 0; • 4 int lengthP = pat.Length(), lengthS = Length(); • 5 while((posP < lengthP) && (posS < lengthS)) • 6 if (pat.str[posP] = = str[posS]) {// 匹配到相同的字元 • 7 posP++; posS++; • 8 } • 9 else • 10 if (posP = = 0) • 11 posS++; • 12 else posP = pat . f [posP-1] + 1; • 13 if (posP < lengthP) return -1; //字串沒有完全比完 • 14 else return posS-lengthP; • 15 }

  23. 有一個快速的方法可計算失敗函數。它以下列重新定義的失敗函數為依據:有一個快速的方法可計算失敗函數。它以下列重新定義的失敗函數為依據: • 這個定義產生了程式2.16中的函數,他用來計算樣式的失敗函數。

  24. 程式:計算失敗函數 • 1 voidString::FailureFunction() • 2 {//為字串樣本 *this 計算失敗函數。 • 3 intlengthP = Length( ); • 4 f[0] = -1; • 5 for (intj = 1;j < lengthP; j++) //計算 f [j] • 6 { • 7 inti = f [j-1]; • 8 while ((*(str+j) != *(str+i+1)) && (i >= 0)) i = f [i]; • 9 if (*(str+j) = = *(str+i+1)) • 10 f [j] = i+1; • 11 elsef [j] = -1; • 12 } • 13 }

More Related