1 | using HEAL.Attic;
|
---|
2 | using HeuristicLab.Common;
|
---|
3 | using System;
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using System.IO;
|
---|
6 | using System.Linq;
|
---|
7 | using System.Runtime.Serialization;
|
---|
8 | using System.Runtime.Serialization.Formatters.Binary;
|
---|
9 | using System.Text;
|
---|
10 | using System.Threading.Tasks;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Algorithms.OESRALPS.DriftDetection
|
---|
13 | {
|
---|
14 | [StorableType("714CE0E4-188F-44A1-A2D0-92F4D82D2C04")]
|
---|
15 | public class BucketContainer
|
---|
16 | {
|
---|
17 | [Storable]
|
---|
18 | private Bucket[] buckets;
|
---|
19 | [Storable]
|
---|
20 | internal BucketContainer Prev { get; set; }
|
---|
21 | [Storable]
|
---|
22 | internal BucketContainer Next { get; set; }
|
---|
23 | [Storable]
|
---|
24 | public int MaxBuckets { get; }
|
---|
25 | [Storable]
|
---|
26 | public int FirstBucket { get; set; }
|
---|
27 | [Storable]
|
---|
28 | public int NumElementsPerBucket { get; set; }
|
---|
29 | [Storable]
|
---|
30 | public int NumBuckets { get; set; }
|
---|
31 |
|
---|
32 | public BucketContainer () { }
|
---|
33 |
|
---|
34 | public BucketContainer(BucketContainer prev, BucketContainer next, int maxBuckets, int elementsPerBucket)
|
---|
35 | {
|
---|
36 | Prev = prev;
|
---|
37 | Next = next;
|
---|
38 | MaxBuckets = maxBuckets;
|
---|
39 | NumElementsPerBucket = elementsPerBucket;
|
---|
40 | buckets = new Bucket[maxBuckets];
|
---|
41 | FirstBucket = 0;
|
---|
42 | NumBuckets = 0;
|
---|
43 | }
|
---|
44 |
|
---|
45 | private BucketContainer(BucketContainer originalContainer)
|
---|
46 | {
|
---|
47 | MaxBuckets = originalContainer.MaxBuckets;
|
---|
48 | Array.Copy(originalContainer.buckets, 0, buckets, 0, originalContainer.buckets.Length);
|
---|
49 | FirstBucket = originalContainer.FirstBucket;
|
---|
50 | NumElementsPerBucket = originalContainer.NumElementsPerBucket;
|
---|
51 | NumBuckets = originalContainer.NumBuckets;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void AddBucket(Bucket newBucket)
|
---|
55 | {
|
---|
56 | if (newBucket.NumElements != NumElementsPerBucket)
|
---|
57 | throw new Exception("Number of elements should be number of elements per bucket.");
|
---|
58 |
|
---|
59 | buckets[(FirstBucket + NumBuckets) % MaxBuckets] = newBucket;
|
---|
60 | NumBuckets++;
|
---|
61 | }
|
---|
62 |
|
---|
63 | public Bucket[] RemoveBuckets(int num)
|
---|
64 | {
|
---|
65 | Bucket[] resultBuckets = new Bucket[num];
|
---|
66 | if (FirstBucket <= ((FirstBucket + num) % MaxBuckets))
|
---|
67 | {
|
---|
68 | Array.Copy(buckets, FirstBucket, resultBuckets, 0, num);
|
---|
69 | }
|
---|
70 | else
|
---|
71 | {
|
---|
72 | Array.Copy(buckets, FirstBucket, resultBuckets, 0, MaxBuckets - FirstBucket);
|
---|
73 | Array.Copy(buckets, 0, resultBuckets, MaxBuckets - FirstBucket, num - MaxBuckets + FirstBucket);
|
---|
74 | }
|
---|
75 | FirstBucket = (FirstBucket + num) % MaxBuckets;
|
---|
76 | NumBuckets -= num;
|
---|
77 | return resultBuckets;
|
---|
78 | }
|
---|
79 |
|
---|
80 | public Bucket GetBucket(int position)
|
---|
81 | {
|
---|
82 | return buckets[(FirstBucket + (NumBuckets - position - 1)) % MaxBuckets];
|
---|
83 | }
|
---|
84 |
|
---|
85 | public BucketContainer DeepCopy()
|
---|
86 | {
|
---|
87 | if (!typeof(BucketContainer).IsSerializable)
|
---|
88 | {
|
---|
89 | throw new ArgumentException("The type must be serializable.", "source");
|
---|
90 | }
|
---|
91 |
|
---|
92 | // Don't serialize a null object, simply return the default for that object
|
---|
93 | if (ReferenceEquals(this, null))
|
---|
94 | {
|
---|
95 | BucketContainer copyThis = new BucketContainer(this);
|
---|
96 | if (Next != null)
|
---|
97 | {
|
---|
98 | BucketContainer nextCopy = Next.DeepCopy();
|
---|
99 | copyThis.Next = nextCopy;
|
---|
100 | nextCopy.Prev = copyThis;
|
---|
101 | }
|
---|
102 | return copyThis;
|
---|
103 | }
|
---|
104 |
|
---|
105 | IFormatter formatter = new BinaryFormatter();
|
---|
106 | using (Stream stream = new MemoryStream())
|
---|
107 | {
|
---|
108 | formatter.Serialize(stream, this);
|
---|
109 | stream.Seek(0, SeekOrigin.Begin);
|
---|
110 | BucketContainer copyThis = (BucketContainer)formatter.Deserialize(stream);
|
---|
111 | if (Next != null)
|
---|
112 | {
|
---|
113 | BucketContainer nextCopy = Next.DeepCopy();
|
---|
114 | copyThis.Next = nextCopy;
|
---|
115 | nextCopy.Prev = copyThis;
|
---|
116 | }
|
---|
117 | return copyThis;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|