Upload
rudolph-mcdowell
View
221
Download
1
Embed Size (px)
Citation preview
1
Hu Junfeng2015/10/16
Lesson 8 Binary File,Bit Lesson 8 Binary File,Bit Operations Operations
关于 Trie (字典树)
又称单词查找树, Trie 树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。
可以实现串频统计、求最长公共子串、字典索引(前缀匹配) 空间占用较大 —— 双数组 Tire
2
周仕林同学实现的 TRie
优先队列的排序方式用的则是它们若编码之后能够得到的压缩量做关键字排序。然后达到 32768 个元素之后每新插入一个元素就删掉原优先队列里面一个关键词最小的元素。这样子就能够得到前 32768 个最适合压缩的单词。
解压缩文件是 jsa.txt. 作业里面还有一个 same.out 是用来检测两个文件是否相同的程序,也就是判断是否是无损压缩。
3
4
优先队列
完全二叉树的顺序存储 按子树的大根或小根维护算法 初始堆的创建算法
5
6
The Data Hierarchy Bit - smallest data item
Value of 0 or 1 Byte – 8 bits
Used to store a character Decimal digits, letters, and special symbols
Field - group of characters conveying meaning Example: your name
Record – group of related fields Represented a struct or a class Example: In a payroll system, a record for a particular
employee that contained his/her identification number, name, address, etc.
File – group of related records Example: payroll file
Database – group of related files
7
Binary Files Formatted Text files
contain variable length records must be accessed sequentially, processing all records from the
start of file to access a particular record Binary Files (random access file)
a file containing binary numbers that are the computer’s internal representation of each file component
contain fixed length records can be accessed directly, directly accessing the record that is
required sizeof
operator that finds the number of bytes used for storage of a data type
8
In a Random Access File …
Data Data unformatted (stored as "raw bytes") in random
access files All data of the same type (ints, for example) use
the same memory All records of the same type have a fixed length Data not human readable
9
Random Access Access individual records without searching through other records Instant access to records in a file Data can be inserted without destroying other data Data previously stored can be updated or deleted without
overwriting. Implemented using fixed length records
Sequential files do not have fixed length records
0 200 300 400 500
byte offsets}
} } } } } }100
100bytes
100bytes
100bytes
100bytes
100bytes
100bytes
10
Random Access a File -- fread () fread --Transfer bytes from a file to a location in memory Function fread requires four arguments
ret = fread(buffer, size, num, myptr); the number of objects read
buffer: Address of first memory cell to fill size: Size of one value num: Maximum number of elements to copy from the file into
memory myptr: File pointer to a binary file opened in mode “rb” using
function fopen
11
Binary File operation — fwrite()
fwrite - Transfer bytes from a location in memory to a file fwrite( &number, sizeof( int ), 1, myPtr );
&number - Location to transfer bytes from sizeof( int ) - Number of bytes to transfer 1 - For arrays, number of elements to transfer
In this case, "one element" of an array is being transferred
myPtr – file pointer
12
Binary File operation — fwrite() (cont.)
Writing structs
fwrite( &myObject, sizeof (struct myStruct), 1, myPtr );
sizeof - Returns size in bytes of object in parentheses
To write several array elements Pointer to array as first argument Number of elements to write as third argument
13
Creating a Binary File of Integers
14
Access Data in a Random Access File fseek
Sets file position pointer to a specific position fseek( myPtr, offset, symbolic_constant);
myPtr - pointer to file offset - file position pointer (0 is first location) symbolic_constant - specifies where in file we are reading from SEEK_SET - seek starts at beginning of file SEEK_CUR - seek starts at current location in file SEEK_END - seek starts at end of file
ftell Return the current position in a stream ftell( myptr)
myPtr - pointer to file
15
Read file
16
17
Bit Operation
Topics:
Bit operators
Using bitwise operations
Bit fields
18
Overview of Bit Operations
useful for systems programming where a single bit or group of bits has meaning
bitwise operators can be used for manipulating bits in integral operands (char, short, int, long)
bit fields can be defined in a struct declaration and accessed as struct members
19
Bitwise Operators
& (bitwise AND) useful for masking bits and turning off bits don’t’ confuse with && (logical AND)
| (bitwise OR) useful for turning on bits don’t confuse with || (logical OR)
^ (bitwise XOR) useful for checking if bits have different values
~ (one’s complement) useful for reversing the sense of bits (unary)
20
Bitwise Shift Operators
<< (left shift) useful for moving a bit or bit group to the left 0 filled in low order bits
>> (right shift) useful for moving a bit or bit group to the right unsigned integer is 0 filled in high order bits signed integer treatment is machine
dependent
21
Bitwise AND
i j i & j
0 0 0
0 1 0
1 0 0
1 1 1
22
Bitwise OR
i j i | j
0 0 0
0 1 1
1 0 1
1 1 1
23
Bitwise XOR
i j i ^ j
0 0 0
0 1 1
1 0 1
1 1 0
24
Bitwise One’s Complement
i ~i
0 1
1 0
25
Bitwise AND Illustration
1 0 1 0 1 0 1 0
0 0 1 1 1 0 0 0
0 0 1 0 1 0 0 0kk
ii
jj
k = i k = i && j j isolate bit group
26
Bitwise OR Illustration
1 0 1 0 1 0 1 0
0 0 0 0 0 0 0 1
1 0 1 0 1 0 1 1kk
ii
jj
k = i k = i || j j set this bit
27
Bitwise Left Shift Illustration
1 1 1 1 1 1 1 1
1 1 1 1 1 1 0 0kk
ii
k = i k = i << << 22
0 filled
28
Bitwise Right Shift Illustration
1 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1kk
ii
k = ik = i >> >> 22
0 filled (if unsigned)
29
Using bitwise operations —— masking and setting
30
Complement notation
31
Exclusive OR
32
Odd or Even
33
Bits rotation
34
Structure Bit Fields
general format: type fieldname : n
type is int (signed or unsigned)
n is the number of bits in the field (must be an integer constant)
can’t take the address ( & ) of a bit field
can’t have an array of bit fields
35
Bit Field Declaration
struct flags { unsigned int f1 : 1 ; unsigned int f2 : 1 ; unsigned int f3 : 1 ; unsigned int type : 4 ; unsigned int index : 9 ;};
main() { struct flags Flags;
Flags.f3 = 1;}
set the bit
5 bit fields
36
Packed data
Count the number of one’sin the field of homework
作业 , 两道 18 号前提交(位运算、广搜) http://bailian.openjudge.cn/practice/1753 http://bailian.openjudge.cn/practice/2251/
37