|
|
1.1 ! root 1: .TH BLOCK 3+ ! 2: .CT 2 datatype ! 3: .SH NAME ! 4: block \- adjustable arrays ! 5: .SH SYNOPSIS ! 6: .nf ! 7: .B "#include <Block.h>" ! 8: .PP ! 9: .ds T \fIT\fP ! 10: .ft L ! 11: Blockdeclare(\*T) ! 12: Blockimplement(\*T) ! 13: .PP ! 14: .ft L ! 15: struct Block(\*T) { ! 16: Block(\*T)(unsigned = 0); ! 17: Block(\*T)(const Block(\*T&); ! 18: ~Block(\*T); ! 19: Block(\*T)& operator= (const Block(\*T)&); ! 20: \*T& operator[] (int); ! 21: operator \*T* (); ! 22: unsigned size(); ! 23: unsigned size(unsigned); ! 24: \*T* end(); ! 25: void swap(const Block(\*T)&); ! 26: } ! 27: .ft R ! 28: .fi ! 29: .SH DESCRIPTION ! 30: A ! 31: .BI Block( T ) ! 32: is an array of zero or more ! 33: .IR elements ! 34: of type ! 35: .I T, ! 36: where ! 37: .I T ! 38: is a type name. ! 39: .I T ! 40: must have assignment ! 41: .RB ( operator= ) ! 42: and initialization ! 43: .RB ( \*T(\*T&) ) ! 44: operations. ! 45: .PP ! 46: The macro call ! 47: .BI Blockdeclare( T ) ! 48: declares the class ! 49: .BI Block( T ) . ! 50: It must appear once in every source file that uses type ! 51: .BI Block( T ) . ! 52: The macro call ! 53: .BI Blockimplement( T ) ! 54: defines the functions that implement the block class. ! 55: It must appear exactly once in the entire program. ! 56: .PP ! 57: New elements ! 58: are initialized to the value of an otherwise ! 59: uninitialized static object of type ! 60: .I T. ! 61: .SS Constructors ! 62: .nr xx \w'\fLBlock(\fIT\fL)(\fIb\fL)' ! 63: .TP \n(xxu ! 64: .BI Block( T ) ! 65: An empty block. ! 66: .TP ! 67: .BI Block( T )( n ) ! 68: A block of ! 69: .I n ! 70: elements. ! 71: .TP ! 72: .BI Block( T )( b ) ! 73: A new block ! 74: whose elements are copies of the elements of ! 75: .BR b . ! 76: .SS Operations ! 77: Assignment copies elements and size. ! 78: .TP \n(xxu ! 79: .IB b [ k ] ! 80: A reference to element ! 81: .L k ! 82: of block ! 83: .IR b ; ! 84: undefined if ! 85: .I k ! 86: is out of bounds. ! 87: .TP ! 88: .BI ( T *) b ! 89: A pointer to the first element of block ! 90: .I b. ! 91: .SS Other functions ! 92: .TP \n(xxu ! 93: .IB b .size() ! 94: Return the number of elements in ! 95: .I b. ! 96: .TP ! 97: .IB b .size( n ) ! 98: Set the size of ! 99: .I b ! 100: to ! 101: .LR n . ! 102: If the new size is greater than the old. ! 103: Otherwise, ! 104: .I n ! 105: old elements are kept. ! 106: Return the new size. ! 107: .TP ! 108: .IB b .reserve( n ) ! 109: The size of ! 110: .I b ! 111: is increased, if necessary, to some value greater than ! 112: .I n. ! 113: If ! 114: .I b ! 115: already has room, ! 116: .I b ! 117: is not changed. ! 118: Return zero if memory could not be allocated ! 119: and non-zero otherwise. ! 120: .TP ! 121: .IB b .end() ! 122: Returns ! 123: a pointer to just past the last element in ! 124: .LR b . ! 125: Equivalent to ! 126: .BR (T*)b+b.size() . ! 127: .TP ! 128: .IB a .swap( b ) ! 129: The memory associated with blocks ! 130: .I a ! 131: and ! 132: .I b ! 133: is exchanged. ! 134: .SS Performance ! 135: Most operations ! 136: are implemented by the obvious uses of the ! 137: .L new ! 138: and ! 139: .L delete ! 140: operators. ! 141: .I Reserve ! 142: checks the size inline. ! 143: If it isn't big enough, the size is increased by multiplying by 3/2 ! 144: (and adding one) enough times to increase it beyond ! 145: .I n . ! 146: .SH EXAMPLES ! 147: .EX ! 148: Blockdeclare(long) ! 149: unsigned n = 0; ! 150: Block(long) b; ! 151: long x; ! 152: while (cin >> x) { ! 153: b.reserve(n); ! 154: b[n++] = x; ! 155: } ! 156: .EE ! 157: .SH SEE ALSO ! 158: .IR malloc (3), ! 159: .IR map (3) ! 160: .SH DIAGNOSTICS ! 161: The only error detected is running out of memory; ! 162: this is indicated in all cases by setting the size of the ! 163: block for which allocation failed to zero. ! 164: .SH BUGS ! 165: Elements are copied during reallocation by using ! 166: .L T::operator= ! 167: instead of ! 168: .LR T(T&) . ! 169: .br ! 170: Because the `type parameter' ! 171: .I T ! 172: is implemented by the C preprocessor, white space is ! 173: forbidden inside the parentheses of ! 174: .BI Block( T ) . ! 175:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.