/// <summary> /// Encode list of integers to bytes /// </summary> /// <param name="postings">a posting list</param> /// <returns>encoded bytes stream</returns> public byte[] Encoding(List <Posting> postings) { List <int> concat = new List <int>(); //1. Write document frequency concat.Add(postings.Count); int previousDocID = 0; foreach (Posting p in postings) { //2. Write docID using gap concat.Add(p.DocumentId - previousDocID); //4byte integer per docID List <int> positions = p.Positions; //3. Write term frequency (# of positions) concat.Add(positions.Count); //4byte integer per term frequency //4. Write positions using gap int previousPos = 0; foreach (int pos in positions) { concat.Add(pos - previousPos); //4byte integer per position previousPos = pos; } previousDocID = p.DocumentId; } return(VariableBytes.Compress(concat)); }
/// <summary> /// Encode list of integers to bytes /// </summary> /// <param name="value">List of integers</param> /// <returns>bytes array</returns> public byte[] Encoding(List <MaxPriorityQueue.InvertedIndex> queue) { queue.Sort( delegate(MaxPriorityQueue.InvertedIndex Item1, MaxPriorityQueue.InvertedIndex Item2) { int docID1 = Item1.GetDocumentId(); int docID2 = Item2.GetDocumentId(); if (docID1 < docID2) { return(-1); } if (docID2 < docID1) { return(1); } else { return(0); } }); List <int> concat = new List <int>(); //1. Write the document frequency concat.Add(queue.Count); int previousDocId = 0; int documentID; int termFreq; foreach (MaxPriorityQueue.InvertedIndex item in queue) { documentID = item.GetDocumentId(); //1. Write the document id using gaps concat.Add(documentID - previousDocId); termFreq = item.GetTermFreq(); //3.Write the term frequency concat.Add(termFreq); previousDocId = documentID; } return(VariableBytes.Compress(concat)); }
/// <summary> /// Encode list of integers to bytes /// </summary> /// <param name="value">List of integers</param> /// <returns>bytes array</returns> public byte[] Encoding(List <int> value) { return(VariableBytes.Compress(value)); }