开始把库搞起来了

打算先把基础的东西组织起来,作为一个“大库”(见上一篇《小库还是大库?》)。然后填加各个实用功能,作为一个个小库或者大库,过段时间再想办法组织整理了。

首先是类型系统,我想来想去,觉得还是必须整理一下,尤其是 unsigned XXX 写起来太难看了,可是这又带来了问题——WinDef.h 把好听的名字都占去了。然后,我只能在自己的命名空间下这样来一番了:

xlDef.h
#ifndef __XLDEF_H_44FDC6C3_12F1_4BF3_8F9F_1ABED755E8ED_INCLUDED__
#define __XLDEF_H_44FDC6C3_12F1_4BF3_8F9F_1ABED755E8ED_INCLUDED__


namespace xl
{
   
typedef char CHAR;
   
typedef unsigned char UCHAR;
   
typedef short SHORT;
   
typedef unsigned short USHORT;
   
typedef int INT;
   
typedef unsigned int UINT;
   
typedef long LONG;
   
typedef unsigned long ULONG;
   
typedef long long LONGLONG;
   
typedef unsigned long long ULONGLONG;

   
typedef void VOID;
   
typedef bool BOOLEAN;
   
typedef INT BOOL;

   
typedef UCHAR BYTE;
   
typedef USHORT WORD;
   
typedef ULONG DWORD;
   
typedef ULONGLONG QWORD;

   
const BOOL TRUE = 1;
   
const BOOL FALSE = 0;
   
const VOID *NULL = 0;

   
typedef struct interface;

}
// namespace xl


#endif // #ifndef __XLDEF_H_44FDC6C3_12F1_4BF3_8F9F_1ABED755E8ED_INCLUDED__

但是,问题是,当 using namespace xl 并 #include <Windows.h> 以后,随手写一个“DWORD”,就会有歧义了。如果用的时候要写成 xl::DWORD,那还不如现在就命名成 XL_DWORD 好了……这点仍然在纠结中……路过朋友请给点意见:)

接下来是最基础一层的组成。我想先把 Array、List、BinaryTree 给实现一下。然后第二层中利用 Array 实现 String,利用 List 搞个 Queue、Stack 什么的,利用 BinaryTree 搞个 Map、Set 之类的。只是数据结构中的“图”,不知道怎么搞比较方便,如果可能,也可以在第一层实现掉。

不得不考虑的是 iterator 的问题。由于 STL 的 iterator 深入人心,我很想也搞个类似的支持。但是,STL 的 iterator 是可以跨容器的,也就是,可以在 list 的 insert 里填入 vector 的 iterator 作为参数。我不太了解 STL 的具体做法,但是粗看似乎是用模板实现的。可是这样的话,就有一个很大的问题,这个模板参数将是没有任何约束的,在明明需要 iterator 的参数的位置,我可以随意填一个别的东西,直到有调用 iterator 的什么方法,编译器才给出错误。这样,实际上在接口中没法为使用者提供足够的信息(或者说,提供语法上的约束)让他明白这个参数应该是什么。比较好的做法可能是 .Net 的那一套,定义接口 IEnumerator、IEnumerable 等等,然后各个类都去实现这个接口,迭代器也可以只针对接口编写。但是由于 C++ 中的多态必须用指针表示,那么在参数、返回值(特别是返回值)中,指针的 delete 又给使用带来了麻烦。于是,似乎需要先有一个完善的智能指针作为基础才可以。而智能指针,如果要线程安全,又必须有平台支持,所以似乎智能指针没法放在这么基础的位置。…………

绕了好久,也想了好久,始终还是找不到好的办法。所以,我暂时觉得还是放弃跨容器的 iterator 了,其实说到底,这只是勉强制造的、看似来很精巧、其实不那么完美的东西而已。

Array、List、Tree 暂时设计如下:

xlArray.h
#ifndef __XLARRAY_H_3B18D7E2_B52A_4D57_BE4B_657F9D17320D_INCLUDED__
#define __XLARRAY_H_3B18D7E2_B52A_4D57_BE4B_657F9D17320D_INCLUDED__


#include "xlDef.h"

namespace xl
{

   
template <typename T>
   
class Array
   
{
   
public:
       
Array();
       
Array(const Array<T> &that);
        ~
Array();

   
public:
       
class Iterator
       
{
       
public:
           
Iterator();
           
Iterator(const Array<T> *array);
           
Iterator(Iterator &that);

       
private:
           
array<T> *array;
           
UINT current;
           
BOOLEAN bof;
           
BOOLEAN eof;

       
public:
           
T &operator * ();
           
T *operator -> ();

       
public:
           
Iterator &operator = (Iterator &that);
           
BOOLEAN operator == (Iterator &that);
           
BOOLEAN operator != (Iterator &that);

       
public:
           
Iterator operator ++ ();
           
Iterator operator ++ (int);
           
Iterator operator -- ();
           
Iterator operator -- (int);
        };

   
public:
       
Iterator Bof();
       
Iterator Begin();
       
Iterator End();
       
Iterator Eof();

   
public:
       
Array<T> &operator=(const Array<T> &that);
       
BOOLEAN operator==(const Array<T> &that) const;
       
BOOLEAN operator!=(const Array<T> &that) const;

   
public:
       
T &operator[](UINT index);
       
const T &operator[](UINT index) const;

   
public:
       
BOOLEAN Empty();
       
UINT Count();

   
public:
       
VOID PushFront(const T &tValue);
       
VOID PushBack(const T &tValue);
       
VOID PopFront();
       
VOID PopBack();
       
VOID Insert(const Iterator &beforeWhich, const T &value);
       
VOID Insert(const Iterator &beforeWhich, const Iterator &firstToInsert, const Iterator &NextOfLastToInsert);
       
Iterator Delete(const Iterator &which);
       
Iterator Delete(const Iterator &firstToDelete, const Iterator &nextOfLastToDelete);
       
VOID SetValue(const Iterator &which, const T &value);
       
VOID SetValue(const Iterator &firstToDelete, const Iterator &nextOfLastToDelete, const T &value);

   
private:
       
T *m_pData;
    };

}
// namespace xl

#endif // #ifndef __XLARRAY_H_3B18D7E2_B52A_4D57_BE4B_657F9D17320D_INCLUDED__

 

xlList.h
#ifndef __XLLIST_H_2BEF1B3C_A056_4EC7_B5E3_9898E7945B54_INCLUDED__
#define __XLLIST_H_2BEF1B3C_A056_4EC7_B5E3_9898E7945B54_INCLUDED__


#include "xlDef.h"

namespace xl
{
   
template <typename T>
   
class List
   
{
   
public:
       
List();
       
List(const List<T> &that);
        ~
List();

   
private:
       
struct Node
       
{
           
T value;
           
Node *prev;
           
Node *next;
        };

   
public:
       
class Iterator
       
{
       
public:
           
Iterator();
           
Iterator(const List<T> *list);
           
Iterator(Iterator &that);

       
private:
           
List<T> *list;
           
Node *current;
           
BOOLEAN bof;
           
BOOLEAN eof;

       
public:
           
T &operator * ();
           
T *operator -> ();

       
public:
           
Iterator &operator = (Iterator &that);
           
BOOLEAN operator == (Iterator &that);
           
BOOLEAN operator != (Iterator &that);

       
public:
           
Iterator operator ++ ();
           
Iterator operator ++ (int);
           
Iterator operator -- ();
           
Iterator operator -- (int);
        };

   
public:
       
Iterator Bof();
       
Iterator Begin();
       
Iterator End();
       
Iterator Eof();

   
public:
       
List<T> &operator=(const List<T> &that);
       
BOOLEAN operator==(const List<T> &that) const;
       
BOOLEAN operator!=(const List<T> &that) const;

   
public:
       
BOOLEAN Empty();
       
UINT Count();

   
public:
       
VOID PushFront(const T &tValue);
       
VOID PushBack(const T &tValue);
       
VOID PopFront();
       
VOID PopBack();
       
VOID Insert(const Iterator &beforeWhich, const T &value);
       
VOID Insert(const Iterator &beforeWhich,
const Iterator &firstToInsert, const Iterator &NextOfLastToInsert);
       
Iterator Delete(const Iterator &which);
       
Iterator Delete(const Iterator &firstToDelete,
const Iterator &nextOfLastToDelete);

   
public:
       
Node *head;
       
Node *tail;
    };

}
// namespace xl

#endif // #ifndef __XLLIST_H_2BEF1B3C_A056_4EC7_B5E3_9898E7945B54_INCLUDED__

 

xlTree.h
#ifndef __XLTREE_H_6BB48AA6_133A_4E9F_944E_504B887B6980_INCLUDED__
#define __XLTREE_H_6BB48AA6_133A_4E9F_944E_504B887B6980_INCLUDED__


#include "xlDef.h"

namespace xl
{
   
template <typename T>
   
class Tree
   
{
   
public:
       
Tree();
       
Tree(const Tree<T> &that);
        ~
Tree();

   
private:
       
struct Node
       
{
           
T value;
           
Node *parent;
           
Node *left;
           
Node *right;
        };

   
private:
       
class Iterator
       
{
       
public:
           
Iterator();
           
Iterator(const Tree<T> *tree);
           
Iterator(Iterator &that);

       
private:
           
Tree<T> *tree;
           
Node *current;
           
BOOLEAN bof;
           
BOOLEAN eof;

       
public:
           
T &operator * ();
           
T *operator -> ();

       
public:
           
Iterator &operator = (Iterator &that);
           
BOOLEAN operator == (Iterator &that);
           
BOOLEAN operator != (Iterator &that);

       
public:
           
Iterator Parent();
           
Iterator Left();
           
Iterator Right();
        };

       
class PreorderIterator : public Iterator
       
{
       
public:
           
Iterator operator ++ ();
           
Iterator operator ++ (int);
           
Iterator operator -- ();
           
Iterator operator -- (int);
        };

       
class InorderIterator : public Iterator
       
{
       
public:
           
Iterator operator ++ ();
           
Iterator operator ++ (int);
           
Iterator operator -- ();
           
Iterator operator -- (int);
        };

       
class PostorderIterator : public Iterator
       
{
       
public:
           
Iterator operator ++ ();
           
Iterator operator ++ (int);
           
Iterator operator -- ();
           
Iterator operator -- (int);
        };

   
public:
       
Iterator Bof();
       
Iterator Begin();
        Iterator Eof();

   
public:
       
Tree<T> &operator=(const Tree<T> &that);
       
BOOLEAN operator==(const Tree<T> &that) const;
       
BOOLEAN operator!=(const Tree<T> &that) const;

   
public:
       
BOOLEAN Empty();
       
UINT Count();

   
public:
        VOID InsertLeft(const Iterator &beforeWhich, const T &value);
       
VOID InsertRight(const Iterator &beforeWhich, const T &value );
       
Iterator Delete(const Iterator &which);

   
public:
       
Node *head;
    };

}
// namespace xl

#endif // #ifndef __XLTREE_H_6BB48AA6_133A_4E9F_944E_504B887B6980_INCLUDED__

(Tree 的接口还没完全考虑好,也不知道有没有必要把 Node 独立出来。)

这样是否大概足够了?敬请大家指教~

(再次重申一下,请不要来留个言说“干吗要重新发明轮子?”、“XXX 不是很好用吗?”之类的,谢谢!欢迎志同道合的朋友探讨,如能为我解惑,那么非常感谢。)

posted on 2009-09-26 17:43  溪流  阅读(11)  评论(0编辑  收藏  举报