📄 fileworks.cs
字号:
{
resultLine.Add(str);
}
}
return true;
}
public bool readAllLines(ArrayList arrayResultLines)
{
FileInfo ofd;
FileStream reader;
ofd = new FileInfo(nameofMainRecs);
if (ofd.Exists && ((ofd.Attributes & FileAttributes.ReadOnly) == 0))
{
byte[] allLines = new byte[sizeofLine * MaxOffset];
reader = ofd.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
//reader.Seek((long)thisOffset * sizeofLine, SeekOrigin.Begin);
reader.Read(allLines, 0, allLines.Length);
byte[] thisline = new byte[sizeofLine];
int j = 0;//index for EmptyRecs
EmptyRecs.Sort();
for (int i = 0; i < MaxOffset; i++)// i is the index of valid line
{
//跳过EmptyRecs
if (j < EmptyRecs.Count && i == (int)EmptyRecs[j])
{
//i++;//这里真阴险,都continue了,上面居然还会i++
j++;
continue;
}
Array.Copy(allLines, i * sizeofLine, thisline, 0, sizeofLine);
string tStr = Encoding.Default.GetString(thisline);
string[] strArray;//= new string [];
strArray = tStr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
ArrayList resultLine = new ArrayList();
foreach (string str in strArray)
{
resultLine.Add(str);
}
arrayResultLines.Add(resultLine);
}
reader.Close();
}
return true;
}
//重载一个无参数的版本, C#......怀念C++
public bool readAllFromFile()
{
return (readAllFromFile("index.bin"));
}
//complete
public bool readAllFromFile(string allFileNames)
{
nameofIndex = allFileNames;
this.EmptyRecs.Clear();
this.filenames_Of_trees.Clear();
this.insertBuffer.Clear();
this.removeBuffer.Clear();
this.Trees.Clear();
//FileInfo ifd = new FileInfo(allFileNames);
FileStream SReader = File.OpenRead(allFileNames);//ifd.OpenRead();
byte[] byteArray = new byte[SReader.Length];
SReader.Read(byteArray, 0, byteArray.Length);
string longstr = Encoding.Default.GetString(byteArray);
string[] strArray = longstr.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);//分行,第二个参数用来去除所有空string
nameofMainRecs = strArray[0];//第一行是主记录文件名
nameofEmptyRecs = strArray[1];//第二行是EmptyRecs文件名
MaxOffset = Int32.Parse(strArray[2]);//第三行是MaxOffset (in int)
string[] sizeofRecord = (strArray[3]).Split(new string[] { " ", "\t" }, StringSplitOptions.RemoveEmptyEntries);//第四行是sizeofRec的ArrayList拉
int sizeofSingleRec = 0;
sizeofLine = 0;//清零这个搞掰我了
sizeofRec.Clear();
foreach (string str in sizeofRecord)
{
sizeofSingleRec = Int32.Parse(str);
sizeofRec.Add(sizeofSingleRec);
sizeofLine += sizeofSingleRec + 1;//1 for blank
}
SReader.Close();//居然忘记关闭这里
//以下各行都是树的文件名
//下面这个for循环建树 i的初值为3
for (int i = 4; i < strArray.Length; i++)
{
string [] str = (strArray[i]).Split(new char[] {' '}, 3, StringSplitOptions.RemoveEmptyEntries);
filenames_Of_trees.Add(str[2]);//不用多说
SReader = File.OpenRead(str[2]);
if (str[0].Equals("int") && str[1].Equals("int"))
{
Trees.Add(build_int_intTree(SReader));
}
else if (str[0].Equals("string") && str[1].Equals("int"))
{
Trees.Add(build_str_intTree(SReader));
}
else
{
return false;
}
}
//下面这个foreach循环建EmptyRecs
SReader = File.OpenRead(strArray[1]);
byteArray = new byte[SReader.Length];
SReader.Read(byteArray, 0, byteArray.Length);
SReader.Close();//这里关闭了流
string longstr1 = Encoding.Default.GetString(byteArray);//用了不同的string name
string[] strArray1 = longstr1.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);//分开各个数
foreach (string str in strArray1)
{
if (str.Length == 0)
{
break;
}
EmptyRecs.Add(Int32.Parse(str));
}
return true;
}
//complete
//have to close file_of_a_tree
public Object build_int_intTree(FileStream file_of_a_tree)
{
Treeworks.BPTree<int, int, intintCompare> thistree = new Treeworks.BPTree<int, int, intintCompare>();
byte[] byteArray = new byte[file_of_a_tree.Length];
file_of_a_tree.Read(byteArray, 0, byteArray.Length);
string longstr = Encoding.Default.GetString(byteArray);
string[] strArray = longstr.Split('\n');//分行
foreach (string str in strArray)
{
if (str.Equals(""))//唉,居然会有空串要跳过
break;//continue;
string[] s = str.Split();//行内分节点
for (int i = 1; i < s.Length - 1; i++)//小心这里 i 的初值为1
{
if (s[i].Equals(""))
break;//continue;
thistree.insert(Int32.Parse(s[0]), Int32.Parse(s[i]));
}
}
file_of_a_tree.Close();
return thistree;
}
public Object build_str_intTree(FileStream file_of_a_tree)
{
Treeworks.BPTree<string, int, strstrCompare> thistree = new Treeworks.BPTree<string, int, strstrCompare>();
byte[] byteArray = new byte[file_of_a_tree.Length];
file_of_a_tree.Read(byteArray, 0, byteArray.Length);
string longstr = Encoding.Default.GetString(byteArray);
string[] strArray = longstr.Split('\n');//分行
foreach (string str in strArray)
{
if (str.Equals(""))//唉,居然会有空串要跳过
break;//continue;
string[] s = str.Split();//行内分节点
for (int i = 1; i < s.Length - 1; i++)//小心这里 i 的初值为1
{
if (s[i].Equals(""))
break;//continue;
thistree.insert(s[0], Int32.Parse(s[i]));
}
}
file_of_a_tree.Close();
return thistree;
}
//complete
public int insert(ArrayList list, ArrayList trees)
{
int i = 0;
ArrayList insertlist = new ArrayList(list.Count + 1);//不new一下还真不行,还是c++按值传递好,c#不爽
foreach (Object elem in list)
{
insertlist.Add(elem);
}
//bool insertsucc = true;
//insertlist.TrimToSize();
//string[] elements = (String[])insertlist.ToArray(typeof(string));//好像没必要这么麻烦
int thisOffset = 0;//应该插入的Offset//是第几条记录
if (EmptyRecs.Count == 0)//MainRecs中没有空位
{
thisOffset = MaxOffset;
MaxOffset++;
}
else//MainRecs中有空位
{
EmptyRecs.Sort();
thisOffset = (int)EmptyRecs[0];
EmptyRecs.RemoveAt(0);
}
//insertlist.
i = 0;
foreach (Object tree in trees)
{
//insertsucc &=
if (tree is Treeworks.BPTree<int, int, intintCompare>)
{
((Treeworks.BPTree<int, int, intintCompare>)tree).insert(Convert.ToInt32(insertlist[i]), thisOffset);//先做了int再说
}
else if (tree is Treeworks.BPTree<string, int, strstrCompare>)
{
((Treeworks.BPTree<string, int, strstrCompare>)tree).insert(insertlist[i].ToString(), thisOffset);//这就做了string了
}
i++;
}
insertlist.Add(thisOffset);//插入buffer前把offset插入该记录的最后
//if (removeBuffer.Contains(insertlist))//如果只是buffer操作
//{
// removeBuffer.Remove(insertlist);
//}
i = 0;
int j = 0;
int flag = 0;
//因为不知道怎么重写ArrayList.Equals();不得已的办法
foreach (ArrayList testlist in removeBuffer)
{
i = 0;
foreach (Object elem in testlist)
{
if (!elem.Equals(insertlist[i]))//不相等就跳出
{
//flag = 1;
break;
}
i++;
}
if (i == testlist.Count)//相等就跳出
{
flag = 1;
break;
}
j++;
}
if (flag == 1)
{
removeBuffer.RemoveAt(j);
}
insertBuffer.Add(insertlist);//插入buffer
return thisOffset;
}
//complete
public bool remove(ArrayList list, ref Object thisOffset, ArrayList trees)
{
int i = 0;
ArrayList removelist = new ArrayList(list.Count + 1);
foreach (Object elem in list)
{
removelist.Add(elem);
}
Object thisremoveOffset;// = thisOffset;
i = 0;
foreach (Object tree in trees)
{
//insertsucc &=
thisremoveOffset = thisOffset;
//一开始忘了要在循环里面反复赋值,因为一旦没有,null就会被赋给这个
if (tree is Treeworks.BPTree<int, int, intintCompare>)
{
((Treeworks.BPTree<int, int, intintCompare>)tree).remove(Convert.ToInt32(removelist[i]), ref thisremoveOffset);//先做了int再说
}
else if (tree is Treeworks.BPTree<string, int, strstrCompare>)
{
((Treeworks.BPTree<string, int, strstrCompare>)tree).remove(removelist[i].ToString(), ref thisremoveOffset);
}
else
{
//do nothing
}
i++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -