This repository has been archived by the owner on May 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCList.cls
1917 lines (1741 loc) · 60.7 KB
/
CList.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "CList"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
'(C) 2007-2014, Developpement Informatique Service, Francesco Foti
' internet: http://www.devinfo.net
' email: info@devinfo.ch
'
'CList.bas class module
'This the CList class, that handle two dimensional indexed arrays which
'can be searched and sorted on multiple colums.
'
'This file is part of the DISRowList library for Visual Basic, DISRowList hereafter.
'
'THe DISRowList library is distributed under a dual license. An open source
'version is licensed under the GNU GPL v2 and a commercial,y licensed version
'can be obtained from devinfo.net either as a standalone package or as part
'of our "The 10th SDK" software library.
'
'DISRowList is free software; you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation; either version 2 of the License, or
'(at your option) any later version.
'
'DISRowList is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
'GNU General Public License for more details.
'
'You should have received a copy of the GNU General Public License
'along with DISRowList (license.txt); if not, write to the Free Software
'Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'
'When ¦ Version ¦ Who ¦ What
'-----------+----------+-----+-----------------------------------------------------
' ¦ ¦ ¦
Option Explicit
Implements IObjectBytes
'class id for implementing IObjectBytes interface
Private Const klCIDList As Long = 1010&
Private Const kiDefaultGrowSize As Integer = 20
Private mlGrowSize As Long
'We expose publicly the inner data array, to be able to
'let ADO populate it automatically with a GetRows() call.
Public DataArray As Variant '1st dim: Columns, 2nd dim: rows
Private mlDataArraySize As Long 'Size of 2nd dim (rows) of DataArray
Private mlDataArraySlotCount As Long 'Number of used rows (some may have been freed)
'current row index
Private mlCurRow As Long
'Row handles storage
Private malIndex() As Long
Private mlIdxArraySize As Long 'Size of index array (upper bound)
Private mlIdxCount As Long 'Number of elements in the DataArray
'For sorting the list on one or multiple columns
Private Type TSortInfo
sColName As String
lColIndex As Long
fCaseSensitive As Boolean
fDesc As Boolean
End Type
Private mfSorted As Boolean 'If true, the class keeps the list elements sorted
Private msSortColumns As String 'The sort definitions string ( [!]<colname>[+|-] )
Private matSortInfo() As TSortInfo 'Information for each column on which we sort
Private miSortColCt As Integer 'The number of columns on which we sort
'For searching the list
Enum eSearchListAlgorithm
eSearchAlgoSequential
eSearchAlgoDichotomic
End Enum
'[f]ind[i]nfo [f]lag values
Private Const klfifCaseSensitive As Long = 1& 'applies only to text fields.
Private Const klfifHasJokers As Long = 2&
Private Const klfifHasWildcards As Long = 4&
Private Const klfifExactSearch As Long = 8&
Private Const klfifRootSearch As Long = 16&
Private Const klfifSortedDesc As Long = 32&
Private Type TFindInfo
fUseSortKeys As Boolean 'True if we can benefit from a sorted list, because we search on key fields
lSearchFieldsCt As Long 'Number of fields on which we search
asSearchField() As String '1 to lSearchFieldsCt
alColIndex() As Long '1 to lSearchFieldsCt
avSearchValue() As Variant '1 to lSearchFieldsCt
alFlags() As Long '1 to lSearchFieldsCt
eSearchAlgo As eSearchListAlgorithm
End Type
Private mtFindInfo As TFindInfo
'The garbage queue for the rows array (circular array queue)
Private malGarbageQ() As Long
Private mlGarbQSize As Long
Private mlGarbQHead As Long
Private mlGarbQTail As Long
Private mlGarbQCount As Long
'
' Columns
'
Private Type TColDef
iDataType As Integer
lDataSize As Long
lFlags As Long
sColName As String
End Type
Private matColDef() As TColDef
Private malColIndex() As Long
Private mlColCount As Long
Private moColIndexMap As CMapStringToLong 'Store colname-->colindex map
'Special column flags
Private Const klColFlagSortedCaseSensitive As Long = &H10000
Private Const klColFlagSortedCaseInsensitive As Long = &H20000
'For iObjectBytes interface
Private Const ksClassVersion As String = "01.10.00"
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
'
' Constructor / destructor
'
Private Sub Class_Initialize()
mlGrowSize = kiDefaultGrowSize
mfSorted = False
'columns
Set moColIndexMap = New CMapStringToLong 'case sensitive=false, by default
moColIndexMap.Sorted = True
Clear
End Sub
Private Sub Class_Terminate()
Set moColIndexMap = Nothing
End Sub
'Clear removes the rows *AND* the column definitions.
'Use .Reset to remove the rows only
Public Sub Clear()
Reset
'clear sort info
If miSortColCt Then Erase matSortInfo
miSortColCt = 0
mlCurRow = 0&
'clear columns info
If mlColCount Then
Erase matColDef
Erase malColIndex
mlColCount = 0&
End If
moColIndexMap.Clear
End Sub
Public Sub Reset()
If mlIdxCount Then Erase DataArray
mlDataArraySlotCount = 0&
mlDataArraySize = 0&
'Clear index array
mlIdxArraySize = mlGrowSize
ReDim malIndex(1 To mlIdxArraySize)
mlIdxCount = 0&
mlCurRow = 0&
'Clear garbage queue
mlGarbQSize = mlGrowSize
ReDim malGarbageQ(1 To mlGarbQSize)
mlGarbQHead = 1&
mlGarbQTail = 0&
mlGarbQCount = 0&
'clear special column flags
ClearAllSysColFlags
End Sub
Public Property Get ColCaseSensitive() As Boolean
ColCaseSensitive = moColIndexMap.CaseSensitive
End Property
Public Property Let ColCaseSensitive(ByVal pfColCaseSensitive As Boolean)
moColIndexMap.CaseSensitive = pfColCaseSensitive
End Property
Public Sub SyncWithDataArray()
Dim i As Long
On Error Resume Next
mlIdxCount = UBound(DataArray, 2)
If Err.Number = 0 Then
mlIdxCount = mlIdxCount - LBound(DataArray) + 1&
End If
On Error GoTo 0
If mlIdxCount Then
If mlIdxCount > mlGrowSize Then
mlIdxArraySize = ((mlIdxCount \ mlGrowSize) * mlGrowSize) + (mlIdxCount Mod mlGrowSize)
Else
mlIdxArraySize = mlGrowSize
End If
ReDim malIndex(1 To mlIdxArraySize)
'For i = 1& To mlIdxCount
For i = 1& To mlIdxCount
malIndex(i) = i - 1&
Next i
mlDataArraySize = mlIdxCount
mlDataArraySlotCount = mlIdxCount
Else
mlDataArraySize = 0&
mlDataArraySlotCount = 0&
mlIdxArraySize = mlGrowSize
ReDim malIndex(1 To mlIdxArraySize)
End If
ClearAllSysColFlags
mlCurRow = 0&
End Sub
Public Property Get ColCount() As Long
ColCount = mlColCount
End Property
Public Sub AddCol(ByRef psColName As String, _
ByVal pvTemplateValue As Variant, _
ByVal plDataSize As Long, _
ByVal plFlags As Long, _
Optional ByVal plInsertAfter As Long = 0&, _
Optional ByVal plInsertBefore As Long = 0&)
If Len(psColName) Then
If moColIndexMap.Find(psColName) Then
'This key is already associated with an element of this collection
Err.Raise 457&, "CRow::AddCol", "Duplicate column names not allowed"
Exit Sub
End If
End If
'All column contents lost, because we modify the first dim
If mlColCount Then
ReDim DataArray(0 To mlColCount, 0 To 0)
Else
ReDim DataArray(0 To 0, 0 To 0)
End If
Dim lColIndex As Long
Dim i As Long
ReDim Preserve matColDef(1 To mlColCount + 1&)
ReDim Preserve malColIndex(1 To mlColCount + 1&)
If plInsertAfter Then
lColIndex = plInsertAfter + 1&
'Push down other elements
If lColIndex <= mlColCount Then
CopyMemory malColIndex(lColIndex + 1&), malColIndex(lColIndex), (mlColCount - plInsertAfter) * LenB(lColIndex)
For i = 1 To moColIndexMap.Count
If moColIndexMap.Item(i) >= lColIndex Then
moColIndexMap.Item(i) = moColIndexMap.Item(i) + 1&
End If
Next i
End If
ElseIf plInsertBefore Then
'Push down other elements
lColIndex = plInsertBefore
If mlColCount Then
CopyMemory malColIndex(lColIndex + 1&), malColIndex(lColIndex), (mlColCount - lColIndex + 1&) * LenB(lColIndex)
For i = 1 To moColIndexMap.Count
If moColIndexMap.Item(i) >= lColIndex Then
moColIndexMap.Item(i) = moColIndexMap.Item(i) + 1&
End If
Next i
End If
Else
'Append at end of columns
lColIndex = mlColCount + 1&
End If
mlColCount = mlColCount + 1&
malColIndex(lColIndex) = mlColCount
If Len(psColName) Then moColIndexMap.Add psColName, lColIndex
'Add info
With matColDef(mlColCount)
.iDataType = VarType(pvTemplateValue)
.lDataSize = plDataSize
.lFlags = plFlags And &HFFFF&
.sColName = psColName
End With
End Sub
Public Function ColPos(ByVal psColName As String) As Long
Dim lColIndex As Long
If Left$(psColName, 1) <> "#" Then
lColIndex = moColIndexMap.Find(psColName)
If lColIndex = 0& Then
'Error 5 (Invalid procedure call)
Err.Raise 5&, "CRow::Value [Get]", "<" & psColName & ">: Item not found"
Exit Function
End If
ColPos = moColIndexMap.Item(lColIndex)
Else
ColPos = CLng(Val(Right$(psColName, Len(psColName) - 1)))
End If
End Function
Public Function ColExists(ByVal psColName As String) As Boolean
ColExists = CBool(moColIndexMap.Find(psColName))
End Function
Public Property Get ColName(ByVal plColIndex As Long) As String
ColName = matColDef(malColIndex(plColIndex)).sColName
End Property
Public Property Let ColName(ByVal plColIndex As Long, ByVal psNewName As String)
Dim lMappedLong As Long
Dim lMapIndex As Long
Dim i As Long
'Column name must be given
If Len(psNewName) = 0 Then
Err.Raise 5&, "CRow::ColName [Let]", "Missing column name."
Exit Property
End If
If moColIndexMap.Find(psNewName) Then
'This key is already associated with an element of this collection
Err.Raise 457&, "CRow::ColName [Let]", "Duplicate column names not allowed."
Exit Property
End If
'Sequentially search for the index in the map
For i = 1 To moColIndexMap.Count
If moColIndexMap.Item(i) = plColIndex Then
lMapIndex = i
Exit For
End If
Next i
If lMapIndex Then
moColIndexMap.Remove lMapIndex
moColIndexMap.Add psNewName, plColIndex
matColDef(malColIndex(plColIndex)).sColName = psNewName
Else
'Bad column index, error 9: "Subscript out of range"
Err.Raise 9&, "CRow::ColName [Let]", VBA.Error$(9&)
End If
End Property
Public Property Get ColType(ByVal pvIndex As Variant) As Integer
Dim lColIndex As Long
If VarType(pvIndex) = vbString Then
lColIndex = ColPos(pvIndex)
Else
lColIndex = pvIndex
End If
ColType = matColDef(malColIndex(lColIndex)).iDataType
End Property
Public Property Let ColType(ByVal pvIndex As Variant, ByVal piNewType As Integer)
Dim lColIndex As Long
If VarType(pvIndex) = vbString Then
lColIndex = ColPos(pvIndex)
Else
lColIndex = pvIndex
End If
matColDef(malColIndex(lColIndex)).iDataType = piNewType
End Property
Public Property Get ColSize(ByVal pvIndex As Variant) As Long
Dim lColIndex As Long
If VarType(pvIndex) = vbString Then
lColIndex = ColPos(pvIndex)
Else
lColIndex = pvIndex
End If
ColSize = matColDef(malColIndex(lColIndex)).lDataSize
End Property
Public Property Let ColSize(ByVal pvIndex As Variant, ByVal plNewSize As Long)
Dim lColIndex As Long
If VarType(pvIndex) = vbString Then
lColIndex = ColPos(pvIndex)
Else
lColIndex = pvIndex
End If
matColDef(malColIndex(lColIndex)).lDataSize = plNewSize
End Property
Public Property Get ColFlags(ByVal pvIndex As Variant) As Long
Dim lColIndex As Long
If VarType(pvIndex) = vbString Then
lColIndex = ColPos(pvIndex)
Else
lColIndex = pvIndex
End If
ColFlags = matColDef(malColIndex(lColIndex)).lFlags
End Property
Public Property Let ColFlags(ByVal pvIndex As Variant, ByVal plNewFlags As Long)
Dim lColIndex As Long
If VarType(pvIndex) = vbString Then
lColIndex = ColPos(pvIndex)
Else
lColIndex = pvIndex
End If
'bit values above &HFFFF& are reserved for class internal use,
'so we just mask the passed flags with &HFFFF&
matColDef(malColIndex(lColIndex)).lFlags = plNewFlags And &HFFFF&
End Property
'Define list from a flat array.
'Elements must be grouped 4 by 4 for a column definition, and in this
'order: name, type, size, flags.
Public Sub Define(ParamArray pavDefs() As Variant)
Dim lLB As Long
Dim lUB As Long
Dim lCount As Long
Dim lIndex As Long
Dim sColName As String
Dim iDataType As Integer
Dim lDataSize As Long
Dim lFlags As Long
Dim lCol As Long
lLB = LBound(pavDefs)
lUB = UBound(pavDefs)
'There must be a multiple of 4 element count
If (lUB - lLB + 1&) Mod 4 Then
Err.Raise 5&, "CList::Define", "Incorrect number of array elements."
Exit Sub
End If
lCount = (lUB - lLB + 1&) \ 4
Clear
lCol = 1&
For lIndex = 1& To lCount
sColName = pavDefs(lLB + (lCol - 1&) * 4&)
iDataType = pavDefs(lLB + (lCol - 1&) * 4& + 1&)
lDataSize = pavDefs(lLB + (lCol - 1&) * 4& + 2&)
lFlags = pavDefs(lLB + (lCol - 1&) * 4& + 3&)
If moColIndexMap.Find(sColName) = 0 Then
ReDim Preserve matColDef(1 To mlColCount + 1&)
ReDim Preserve malColIndex(1 To mlColCount + 1&)
'Append at end of columns
mlColCount = mlColCount + 1&
malColIndex(mlColCount) = mlColCount
moColIndexMap.Add sColName, mlColCount
'Add info
With matColDef(mlColCount)
.iDataType = iDataType
.lDataSize = lDataSize
.lFlags = lFlags And &HFFFF&
.sColName = sColName
End With
End If
lCol = lCol + 1&
Next lIndex
ReDim DataArray(0 To mlColCount - 1, 0 To 0)
End Sub
Public Sub ArrayDefine(pavColName As Variant, _
Optional pavDataType As Variant, _
Optional pavDataSize As Variant, _
Optional pavDataFlags As Variant)
Dim lLB As Long
Dim lUB As Long
Dim lIndex As Long
Dim sColName As String
Dim iDataType As Integer
Dim lDataSize As Long
Dim lFlags As Long
lLB = LBound(pavColName)
lUB = UBound(pavColName)
Clear
For lIndex = lLB To lUB
sColName = pavColName(lIndex)
If Not IsMissing(pavDataType) Then
iDataType = pavDataType(lIndex)
Else
iDataType = VbVarType.vbString
End If
If Not IsMissing(pavDataSize) Then lDataSize = pavDataSize(lIndex)
If Not IsMissing(pavDataFlags) Then lFlags = pavDataFlags(lIndex)
If moColIndexMap.Find(sColName) = 0 Then
ReDim Preserve matColDef(1 To mlColCount + 1&)
ReDim Preserve malColIndex(1 To mlColCount + 1&)
'Append at end of columns
mlColCount = mlColCount + 1&
malColIndex(mlColCount) = mlColCount
moColIndexMap.Add sColName, mlColCount
'Add info
With matColDef(mlColCount)
.iDataType = iDataType
.lDataSize = lDataSize
.lFlags = lFlags And &HFFFF&
.sColName = sColName
End With
End If
Next lIndex
ReDim DataArray(0 To mlColCount - 1, 0 To 0)
End Sub
'
' CList specific methods
'
Public Property Get CurrentRow() As Long
CurrentRow = mlCurRow
End Property
Public Property Let CurrentRow(ByVal plNewCurRow As Long)
If (plNewCurRow >= 0) And (plNewCurRow <= Me.Count) Then
mlCurRow = plNewCurRow
End If
End Property
Public Property Get GrowSize() As Long
GrowSize = mlGrowSize
End Property
Public Property Let GrowSize(ByVal plGrowSize As Long)
mlGrowSize = plGrowSize
End Property
Public Property Get Count() As Long
Count = mlIdxCount
End Property
'
' Sorting, public methods
'
'Sort columns string format: [!]Column name[+|-][, [!]ColumnName2[+|-] [, ...] ]
Private Sub ParseSortColumns(ByRef psSortColumns As String)
Dim asColName() As String
Dim sColName As String
Dim i As Integer
Dim sCaseSensitive As String
Dim fCaseSensitive As Boolean
Dim sAscDesc As String
'If the sort columns definition is empty, we reinit and exit
If Len(psSortColumns) = 0 Then
If miSortColCt Then Erase matSortInfo()
miSortColCt = 0
mfSorted = False
Exit Sub
End If
'each column on which we'll have to sort is separated by a comma
miSortColCt = SplitString(asColName(), psSortColumns, ",")
ReDim matSortInfo(1 To miSortColCt)
For i = 1 To miSortColCt
sColName = asColName(i)
sCaseSensitive = Left$(sColName, 1)
sAscDesc = Right$(sColName, 1)
With matSortInfo(i)
'The column name must be banged (preceeded by "!") for the sort to be case sensitive
If sCaseSensitive = "!" Then
.fCaseSensitive = True
sColName = Right$(sColName, Len(sColName) - 1)
Else
.fCaseSensitive = False
End If
'Last char of column name may indicate asc or desc sorting
If (sAscDesc = "+") Or (sAscDesc = "-") Then
.fDesc = CBool(sAscDesc = "-")
sColName = Left$(sColName, Len(sColName) - 1)
Else
.fDesc = False
End If
.sColName = sColName
On Error Resume Next
.lColIndex = ColPos(sColName)
If .lColIndex = 0& Then
'this can't be a good thing: we can't sort on an unknown column
'(we borrow VB error 5 (invalid procedure call); we could also
'have used error 9 (subscript out of range))
Err.Raise 5&, "CList::ParseSortColumns", sColName & ": is not an existing column; can't sort list"
Exit Sub
End If
End With
Next i
End Sub
'(Complementary function for handling sort info)
'Need to now if a column is part of the sort key
Private Function IsSortColumn(ByVal plColIndex As Long) As Boolean
Dim i As Integer
Dim sCompColName As String
Dim eCompMethod As VbCompareMethod
If miSortColCt Then
If moColIndexMap.CaseSensitive Then
eCompMethod = vbBinaryCompare
Else
eCompMethod = vbTextCompare
End If
sCompColName = ColName(plColIndex)
For i = 1 To miSortColCt
If StrComp(matSortInfo(i).sColName, sCompColName, eCompMethod) = 0 Then
IsSortColumn = True
Exit Function
End If
Next i
End If
End Function
Public Property Get Sorted() As Boolean
Sorted = mfSorted
End Property
Public Sub SetUnsorted()
If miSortColCt Then Erase matSortInfo()
mfSorted = False: miSortColCt = 0: ClearAllSysColFlags
End Sub
Public Sub SetSorted(ByVal psSortColumns As String)
Dim i As Integer
Dim iColPos As Long
ParseSortColumns psSortColumns
msSortColumns = psSortColumns
If miSortColCt Then
For i = 1 To miSortColCt
iColPos = matSortInfo(i).lColIndex
If matSortInfo(i).fCaseSensitive Then
SetSysColFlags iColPos, klColFlagSortedCaseSensitive
Else
SetSysColFlags iColPos, klColFlagSortedCaseInsensitive
End If
Next i
mfSorted = True
End If
End Sub
Public Sub Sort(ByVal psSortColumns As String, Optional ByVal plStartRow As Long = 1&, Optional ByVal plEndRow As Long = 0&)
Dim i As Integer
Dim iColPos As Long
If plEndRow = 0& Then plEndRow = mlIdxCount
ClearAllSysColFlags
ParseSortColumns psSortColumns 'Will raise an error if a column name doesn't exist or map
msSortColumns = psSortColumns
If miSortColCt Then
mfSorted = False
'resolve sort columns indices
QuickSortRows plStartRow, plEndRow
If (plStartRow = 1&) And (plEndRow = mlIdxCount) Then
'set columns sys flags
For i = 1 To miSortColCt
iColPos = matSortInfo(i).lColIndex
If matSortInfo(i).fCaseSensitive Then
SetSysColFlags iColPos, klColFlagSortedCaseSensitive
Else
SetSysColFlags iColPos, klColFlagSortedCaseInsensitive
End If
Next i
mfSorted = True
mlCurRow = 0&
End If
End If
End Sub
Public Property Get SortColumns()
SortColumns = msSortColumns
End Property
Public Property Get Item(ByVal pvColIndex As Variant, ByVal plRowIndex As Long) As Variant
Attribute Item.VB_UserMemId = 0
Dim lColIndex As Long
If VarType(pvColIndex) = vbString Then
lColIndex = ColPos(pvColIndex)
Else
lColIndex = pvColIndex
End If
Item = DataArray(lColIndex - 1, malIndex(plRowIndex))
End Property
Public Property Let Item(ByVal pvColIndex As Variant, ByVal plRowIndex As Long, ByRef pvCellValue As Variant)
Dim lColIndex As Long
If VarType(pvColIndex) = vbString Then
lColIndex = ColPos(pvColIndex)
Else
lColIndex = pvColIndex
End If
DataArray(lColIndex - 1, malIndex(plRowIndex)) = pvCellValue
If IsSortColumn(lColIndex) Then mfSorted = False
End Property
'
' Add row data
'
Public Function AddRow(prowToAdd As CRow, _
Optional ByVal plInsertAfter As Long = 0&, _
Optional ByVal plInsertBefore As Long = 0&) As Long
Dim lDataIndex As Long
Dim lCol As Long
Dim lRowIndex As Long
'Grow index array if necessary
If mlIdxCount = mlIdxArraySize Then
ReDim Preserve malIndex(1 To mlIdxArraySize + mlGrowSize)
mlIdxArraySize = mlIdxArraySize + mlGrowSize
End If
'Find a slot where to insert the new row data
lDataIndex = AllocRow(prowToAdd)
'Adding a row breaks sorted state
SetUnsorted
If plInsertAfter Then
lRowIndex = plInsertAfter + 1&
'Push down other index pointers
If lRowIndex < mlIdxCount Then
CopyMemory malIndex(lRowIndex + 1&), malIndex(lRowIndex), (mlIdxCount - plInsertAfter) * LenB(lRowIndex)
End If
ElseIf plInsertBefore Then
lRowIndex = plInsertBefore
If mlIdxCount Then
CopyMemory malIndex(lRowIndex + 1&), malIndex(lRowIndex), (mlIdxCount - lRowIndex + 1&) * LenB(lRowIndex)
End If
Else
'Simply insert the new element at the end of the index array
lRowIndex = mlIdxCount + 1&
End If
mlIdxCount = mlIdxCount + 1&
malIndex(lRowIndex) = lDataIndex
'copy row data
For lCol = 1& To mlColCount
If Len(matColDef(lCol).sColName) Then
If prowToAdd.ColExists(matColDef(lCol).sColName) Then
DataArray(lCol - 1&, lDataIndex) = prowToAdd(matColDef(lCol).sColName)
Else
DataArray(lCol - 1&, lDataIndex) = Null
End If
Else
DataArray(lCol - 1&, lDataIndex) = Null
End If
Next lCol
AddRow = lRowIndex
End Function
'Return the index in DataArray() for the new row copy it
Private Function AllocRow(ByRef prowNew As CRow) As Long
Dim lRetIndex As Long
lRetIndex = GarbQPop()
If lRetIndex = -1& Then
'No free slot in the garbage queue, add a new row in the DataArray
If mlDataArraySlotCount = mlDataArraySize Then
If mlColCount Then
ReDim Preserve DataArray(0 To mlColCount - 1, 0 To mlDataArraySize + mlGrowSize - 1&)
Else
ReDim DataArray(0 To mlColCount - 1, 0 To mlDataArraySize + mlGrowSize - 1&)
End If
mlDataArraySize = mlDataArraySize + mlGrowSize
End If
lRetIndex = mlDataArraySlotCount
mlDataArraySlotCount = mlDataArraySlotCount + 1&
End If
AllocRow = lRetIndex
End Function
'Return the index in DataArray() for a new row of data
Private Function AllocRowSlot() As Long
Dim lRetIndex As Long
Dim lCol As Long
lRetIndex = GarbQPop()
If lRetIndex = -1& Then
'No free slot in the garbage queue, add a new row in the DataArray
If mlDataArraySlotCount = mlDataArraySize Then
If mlColCount Then
ReDim Preserve DataArray(0 To mlColCount - 1, 0 To mlDataArraySize + mlGrowSize - 1&)
Else
ReDim DataArray(0 To mlColCount - 1, 0 To mlDataArraySize + mlGrowSize - 1&)
End If
mlDataArraySize = mlDataArraySize + mlGrowSize
End If
lRetIndex = mlDataArraySlotCount
mlDataArraySlotCount = mlDataArraySlotCount + 1&
End If
AllocRowSlot = lRetIndex
End Function
Public Function AddValues(ParamArray pavValues() As Variant) As Long
Dim lLB As Long
Dim lUB As Long
Dim lIndex As Long
Dim lListCol As Long
Dim lDataIndex As Long
Dim lRowIndex As Long
'Grow index array if necessary
If mlIdxCount = mlIdxArraySize Then
ReDim Preserve malIndex(1 To mlIdxArraySize + mlGrowSize)
mlIdxArraySize = mlIdxArraySize + mlGrowSize
End If
'Find a slot where to insert the new row data
lDataIndex = AllocRowSlot()
'Adding a row breaks sorted state
SetUnsorted
'Simply insert the new element at the end of the index array
lRowIndex = mlIdxCount + 1&
mlIdxCount = mlIdxCount + 1&
malIndex(lRowIndex) = lDataIndex
'Assign passed variant array elements
lLB = LBound(pavValues)
lUB = UBound(pavValues)
lListCol = 1&
For lIndex = lLB To lUB
DataArray(malColIndex(lListCol) - 1, lDataIndex) = pavValues(lIndex)
lListCol = lListCol + 1&
Next lIndex
AddValues = lRowIndex
End Function
Public Function AddValuesArray(pavValues As Variant, _
Optional ByVal plInsertAfter As Long = 0&, _
Optional ByVal plInsertBefore As Long = 0&) As Long
Dim lLB As Long
Dim lUB As Long
Dim lIndex As Long
Dim lListCol As Long
Dim lDataIndex As Long
Dim lRowIndex As Long
'Grow index array if necessary
If mlIdxCount = mlIdxArraySize Then
ReDim Preserve malIndex(1 To mlIdxArraySize + mlGrowSize)
mlIdxArraySize = mlIdxArraySize + mlGrowSize
End If
'Find a slot where to insert the new row data
lDataIndex = AllocRowSlot()
'Adding a row breaks sorted state
SetUnsorted
If plInsertAfter Then
lRowIndex = plInsertAfter + 1&
'Push down other index pointers
If lRowIndex < mlIdxCount Then
CopyMemory malIndex(lRowIndex + 1&), malIndex(lRowIndex), (mlIdxCount - plInsertAfter) * LenB(lRowIndex)
End If
ElseIf plInsertBefore Then
lRowIndex = plInsertBefore
If mlIdxCount Then
CopyMemory malIndex(lRowIndex + 1&), malIndex(lRowIndex), (mlIdxCount - lRowIndex + 1&) * LenB(lRowIndex)
End If
Else
'Simply insert the new element at the end of the index array
lRowIndex = mlIdxCount + 1&
End If
mlIdxCount = mlIdxCount + 1&
malIndex(lRowIndex) = lDataIndex
'Assign passed variant array elements
lLB = LBound(pavValues)
lUB = UBound(pavValues)
lListCol = 1&
For lIndex = lLB To lUB
DataArray(malColIndex(lListCol) - 1, lDataIndex) = pavValues(lIndex)
lListCol = lListCol + 1&
Next lIndex
AddValuesArray = lRowIndex
End Function
'
' Update row data
'
Public Sub AssignRow(ByVal plRowIndex As Long, ByRef prowSource As CRow)
Dim lDataIndex As Long
Dim lCol As Long
lDataIndex = malIndex(plRowIndex)
'copy row data
For lCol = 1& To mlColCount
If Len(matColDef(lCol).sColName) Then
If prowSource.ColExists(matColDef(lCol).sColName) Then
DataArray(lCol - 1&, lDataIndex) = prowSource(matColDef(lCol).sColName)
'If the sorted column value is changed, then the sorted state is broken
If IsSortColumn(lCol) And mfSorted Then SetUnsorted
End If
End If
Next lCol
End Sub
Public Sub AssignValues(ByVal plRowIndex As Long, ParamArray pavValues() As Variant)
Dim lLB As Long
Dim lUB As Long
Dim lIndex As Long
Dim lListCol As Long
Dim lDataIndex As Long
lDataIndex = malIndex(plRowIndex)
'Assign passed variant array elements
lLB = LBound(pavValues)
lUB = UBound(pavValues)
lListCol = 1&
For lIndex = lLB To lUB
DataArray(malColIndex(lListCol) - 1, lDataIndex) = pavValues(lIndex)
'If the sorted column value is changed, then the sorted state is broken
If IsSortColumn(lListCol) And mfSorted Then SetUnsorted
lListCol = lListCol + 1&
Next lIndex
End Sub
Public Sub AssignValuesArray(ByVal plRowIndex As Long, ByRef pavValues() As Variant)
Dim lLB As Long
Dim lUB As Long
Dim lIndex As Long
Dim lListCol As Long
Dim lDataIndex As Long
lDataIndex = malIndex(plRowIndex)
'Assign passed variant array elements
lLB = LBound(pavValues)
lUB = UBound(pavValues)
lListCol = 1&
For lIndex = lLB To lUB
DataArray(malColIndex(lListCol) - 1, lDataIndex) = pavValues(lIndex)
'If the sorted column value is changed, then the sorted state is broken
If IsSortColumn(lListCol) And mfSorted Then SetUnsorted
lListCol = lListCol + 1&
Next lIndex
End Sub
'
' Remove rows
'
Public Sub Remove(ByVal plIndex As Long)
Dim lSlot As Long
'decrement current row if needed
If plIndex <= mlCurRow Then
mlCurRow = mlCurRow - 1&
If mlCurRow < 0& Then mlCurRow = 0&
End If
'Remove from the index array
lSlot = malIndex(plIndex)
If plIndex < mlIdxCount Then
CopyMemory malIndex(plIndex), malIndex(plIndex + 1&), (mlIdxCount - 1&) * 4&
End If
mlIdxCount = mlIdxCount - 1&
GarbQPush lSlot
End Sub
Private Sub FreeString(ByVal plSlot As Long)
GarbQPush plSlot
End Sub
'
' Find data
'
Private Sub ClearFindInfo()
With mtFindInfo
.fUseSortKeys = False
If .lSearchFieldsCt Then
Erase .asSearchField
Erase .alColIndex
Erase .avSearchValue
Erase .alFlags
End If
.lSearchFieldsCt = 0&
.eSearchAlgo = eSearchAlgoSequential 'this one can always be applied
End With
End Sub
'Warning, parameters may be 0 based arrays !
Private Sub ParseSearchColumns(ByRef pvSearchColumns As Variant, ByRef pvSearchCriteria As Variant)
Dim iDims As Integer
Dim avBounds As Variant
Dim i As Integer
Dim eCompMethod As VbCompareMethod
Dim lParamIndex As Long
If moColIndexMap.CaseSensitive Then
eCompMethod = vbBinaryCompare
Else
eCompMethod = vbTextCompare
End If
With mtFindInfo
'If pvSearchColumns is an array, then we search on multiple columns
GetVarArrayBounds pvSearchColumns, iDims, avBounds
If iDims Then
lParamIndex = avBounds(0)
.lSearchFieldsCt = avBounds(1) - avBounds(0) + 1&
ReDim .asSearchField(1 To .lSearchFieldsCt)
ReDim .alColIndex(1 To .lSearchFieldsCt)
ReDim .alFlags(1 To .lSearchFieldsCt)
.asSearchField(1&) = pvSearchColumns(lParamIndex)
Else
ReDim .asSearchField(1 To 1)
ReDim .alColIndex(1 To 1)
ReDim .alFlags(1 To 1)
.lSearchFieldsCt = 1&
.asSearchField(1&) = pvSearchColumns
lParamIndex = 1&
End If
'transform field names and compute derived infos