?? nodemgr.cpp
字號:
// Copyright 1993 Nildram Software. All rights reserved.
// May not be copied, except for backup purposes, without express written
// permission from Nildram Software
#include "stdafx.h"
#include <stdlib.h>
#include "nodemgr.h"
NodeMgr::NodeMgr(int nBlockSize, int itsize)
{
if (itsize == 0)
{
AfxMessageBox("Error: Index Item Size Is Zero!");
ASSERT(FALSE);
exit(0);
}
m_nIndexSize = itsize + 4;
m_nKeyWordSize = itsize;
m_nBlockRecNum = (nBlockSize - 16) / m_nIndexSize; // 16 is the block header size
m_plPointersOfBlock = new long[m_nBlockRecNum];
m_pszKeyWordsOfBlock = new char *[m_nBlockRecNum];
for (int x = 0; x < m_nBlockRecNum; x++)
m_pszKeyWordsOfBlock[x] = new char[m_nIndexSize];
m_lLeftNode = -1;
m_lRightNode = -1;
m_cItemNum = 0;
m_nCurrItemPtr = -1;
m_cBuffLabel = stLeaf;
}
NodeMgr::~NodeMgr(void)
{
for (int x = 0; x < m_nBlockRecNum; x++)
delete m_pszKeyWordsOfBlock[x];
delete m_pszKeyWordsOfBlock;
delete m_plPointersOfBlock;
}
void NodeMgr::read_data(char *buffer)
{
char *strptr = buffer;
m_lLeftNode = *((long *)strptr);
strptr += 4;
m_lRightNode = *((long *)strptr);
strptr += 4;
m_cItemNum = *((short *)strptr);
strptr += 2;
m_cBuffLabel = *((short *)strptr);
strptr += 2;
m_lSeemNoUse = *((long *)strptr);
strptr += 4;
for (int x = 0; x < m_cItemNum; x++)
{
m_plPointersOfBlock[x] = *((long *)strptr);
strptr += 4;
memcpy(m_pszKeyWordsOfBlock[x], strptr, m_nKeyWordSize);
strptr += m_nKeyWordSize;
}
m_nCurrItemPtr = -1;
}
void NodeMgr::write_data(char *buffer)
{
char *strptr = buffer;
*((long *)strptr) = m_lLeftNode;
strptr += 4;
*((long *)strptr) = m_lRightNode;
strptr += 4;
*((short *)strptr) = m_cItemNum;
strptr += 2;
*((short *)strptr) = m_cBuffLabel;
strptr += 2;
*((long *)strptr) = ++m_lSeemNoUse;
strptr += 4;
static int nSmall8 = 0;
static int nBig8 = 0;
if ( m_cItemNum > 8 ) {
nSmall8 ++;
} else {
nBig8 ++;
}
for ( int x = 0; x < m_cItemNum; x++ )
{
*((long *)strptr) = m_plPointersOfBlock[x];
strptr += 4;
memcpy(strptr, m_pszKeyWordsOfBlock[x], m_nKeyWordSize);
strptr += m_nKeyWordSize;
}
}
int NodeMgr::erase(int itemno)
{
if ( m_cItemNum <= 1 )
{
m_cItemNum = 0;
return noEmpty;
}
if ( itemno == -1 )
itemno = m_nCurrItemPtr;
if ( itemno < 0 || itemno >= m_cItemNum )
return noNoSelect;
char *tmpptr = m_pszKeyWordsOfBlock[itemno];
for ( int x = itemno + 1; x < m_cItemNum; x++ )
{
m_pszKeyWordsOfBlock[x - 1] = m_pszKeyWordsOfBlock[x];
m_plPointersOfBlock[x - 1] = m_plPointersOfBlock[x];
}
m_pszKeyWordsOfBlock[m_cItemNum - 1] = tmpptr;
m_cItemNum--;
if ( m_nCurrItemPtr >= itemno )
m_nCurrItemPtr--;
return 0;
}
int NodeMgr::insert(long ptr, char *pszKeyWord)
{
int x = 0;
if ( m_cItemNum > 0 )
{
x = getpos(pszKeyWord, 0, m_cItemNum - 1);
if ( x != -1 )
{
if (memcmp(pszKeyWord, m_pszKeyWordsOfBlock[x], m_nKeyWordSize) == 0)
//if (memcmp(pszKeyWord, m_pszKeyWordsOfBlock[x], m_nKeyWordSize) == 0)
return noDuplicate;
}
else
x = m_cItemNum;
}
char *tmpptr = m_pszKeyWordsOfBlock[m_cItemNum];
for ( int y = m_cItemNum; y > x; y-- )
{
m_pszKeyWordsOfBlock[y] = m_pszKeyWordsOfBlock[y - 1];
m_plPointersOfBlock[y] = m_plPointersOfBlock[y - 1];
}
m_pszKeyWordsOfBlock[x] = tmpptr;
m_plPointersOfBlock[x] = ptr;
memcpy(m_pszKeyWordsOfBlock[x], pszKeyWord, m_nKeyWordSize);
if ( ++m_cItemNum == m_nBlockRecNum )
return noFull;
return 0;
}
int NodeMgr::getpos(char *pszKeyWord, int start, int stop)
{
for (;;)
{
int checkpos = (start + stop) / 2;
int cmp = memcmp(pszKeyWord, m_pszKeyWordsOfBlock[checkpos], m_nKeyWordSize);
if (cmp == 0)
return checkpos;
if (cmp < 0)
if (checkpos == start)
return checkpos;
else
{
stop = checkpos;
continue;
}
else
if (start == stop)
return -1;
else
{
start = checkpos + 1;
continue;
}
}
}
long NodeMgr::find(char *pszKeyWord)
{
if (m_cItemNum > 0)
{
int x = getpos(pszKeyWord, 0, m_cItemNum - 1);
if (x != -1)
{
m_nCurrItemPtr = x;
return m_plPointersOfBlock[x];
}
if (m_cBuffLabel == stLeaf)
{
m_nCurrItemPtr = m_cItemNum;
return -1;
}
m_nCurrItemPtr = m_cItemNum - 1;
return m_plPointersOfBlock[m_cItemNum - 1];
}
return -1;
}
long NodeMgr::ffwd_core(void)
{
if (m_cItemNum > 0)
{
if (m_cBuffLabel == stLeaf)
{
m_nCurrItemPtr = m_cItemNum;
return -1;
}
m_nCurrItemPtr = m_cItemNum - 1;
return m_plPointersOfBlock[m_cItemNum - 1];
}
return -1;
}
long NodeMgr::retrieve(void)
{
if (m_nCurrItemPtr >= m_cItemNum || m_nCurrItemPtr < 0)
return -1;
return m_plPointersOfBlock[m_nCurrItemPtr];
}
long NodeMgr::next(void)
{
if (m_nCurrItemPtr == m_cItemNum - 1)
m_nCurrItemPtr++;
if (m_nCurrItemPtr > m_cItemNum - 1)
return -1;
return m_plPointersOfBlock[++m_nCurrItemPtr];
}
long NodeMgr::prev(void)
{
if (m_nCurrItemPtr == 0)
m_nCurrItemPtr--;
if (m_nCurrItemPtr < 0)
return -1;
return m_plPointersOfBlock[--m_nCurrItemPtr];
}
void NodeMgr::reset(int st)
{
m_lLeftNode = -1;
m_lRightNode = -1;
m_nCurrItemPtr = -1;
m_cItemNum = 0;
m_cBuffLabel = st;
m_lSeemNoUse = 0;
}
void NodeMgr::split(char *block1, char *block2)
{
int tmp_items = m_cItemNum;
m_cItemNum /= 2;
write_data(block1);
m_cItemNum = tmp_items;
for (int x = 0; x < tmp_items / 2; x++)
erase(0);
write_data(block2);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -