GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
mem_stream.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * MODULE: iostream
4  *
5 
6  * COPYRIGHT (C) 2007 Laura Toma
7  *
8  *
9 
10  * Iostream is a library that implements streams, external memory
11  * sorting on streams, and an external memory priority queue on
12  * streams. These are the fundamental components used in external
13  * memory algorithms.
14 
15  * Credits: The library was developed by Laura Toma. The kernel of
16  * class STREAM is based on the similar class existent in the GPL TPIE
17  * project developed at Duke University. The sorting and priority
18  * queue have been developed by Laura Toma based on communications
19  * with Rajiv Wickremesinghe. The library was developed as part of
20  * porting Terraflow to GRASS in 2001. PEARL upgrades in 2003 by
21  * Rajiv Wickremesinghe as part of the Terracost project.
22 
23  *
24  * This program is free software; you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation; either version 2 of the License, or
27  * (at your option) any later version.
28  *
29 
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33  * General Public License for more details. *
34  * **************************************************************************/
35 
36 
37 #ifndef _MEM_STREAM_H
38 #define _MEM_STREAM_H
39 
40 #include <stdlib.h>
41 #include <assert.h>
42 
43 #include <cstring>
44 #include <iostream>
45 
46 template<class T>
47 class MEM_STREAM {
48 private:
49  T *data;
50  T *curr;
51  T *dataend;
52  int len;
53 
54 public:
55  MEM_STREAM(T *data, int len);
56  ~MEM_STREAM(void);
57 
58  // Read and write elements.
59  AMI_err read_item(T **elt);
60 
61  AMI_err write_item(const T &elt);
62 
63  // Return the number of items in the stream.
64  off_t stream_len(void);
65 
66  // Return the path name of this stream.
67  AMI_err name(char **stream_name);
68 
69  // Move to a specific item in the stream.
70  AMI_err seek(off_t offset);
71 
72  char *sprint();
73 };
74 
75 
76 /**********************************************************************/
77 
78 template<class T>
79 MEM_STREAM<T>::MEM_STREAM(T *datap, int lenv) {
80 
81  data = datap;
82  dataend = data + lenv;
83  curr = datap;
84  len = lenv;
85 
86 }
87 
88 
89 /**********************************************************************/
90 // Return the number of items in the stream.
91 template<class T>
93 
94  return len;
95 
96 }
97 
98 
99 
100 /**********************************************************************/
101 // Return the path name of this stream.
102 template<class T>
103 AMI_err MEM_STREAM<T>::name(char **stream_name) {
104 
105  const char *path = "dummy";
106 
107  *stream_name = new char [strlen(path) + 1];
108  strcpy(*stream_name, path);
109 
110  return AMI_ERROR_NO_ERROR;
111 }
112 
113 
114 /**********************************************************************/
115 // Move to a specific offset within the (sub)stream.
116 template<class T>
118 
119  assert(offset <= len);
120 
121  curr = data + offset;
122 
123  return AMI_ERROR_NO_ERROR;
124 }
125 
126 
127 
128 /**********************************************************************/
129 template<class T>
131 }
132 
133 
134 
135 /**********************************************************************/
136 template<class T>
138 
139  assert(data);
140 
141  if(curr == dataend) {
143  }
144  *elt = curr;
145  curr++;
146  return AMI_ERROR_NO_ERROR;
147 }
148 
149 
150 
151 
152 /**********************************************************************/
153 
154 template<class T>
156 
157  assert(data);
158 
159  if(curr == dataend) {
161  }
162  *curr = elt;
163  curr++;
164  return AMI_ERROR_NO_ERROR;
165 }
166 
167 
168 /**********************************************************************/
169 // sprint()
170 // Return a string describing the stream
171 //
172 // This function gives easy access to the file name, length.
173 // It is not reentrant, but this should not be too much of a problem
174 // if you are careful.
175 template<class T>
177  static char buf[BUFSIZ];
178  sprintf(buf, "[MEM_STREAM %d]", stream_len());
179  return buf;
180 }
181 
182 #endif // _MEM_STREAM_H
off_t stream_len(void)
Definition: mem_stream.h:92
AMI_err name(char **stream_name)
Definition: mem_stream.h:103
AMI_err seek(off_t offset)
Definition: mem_stream.h:117
AMI_err write_item(const T &elt)
Definition: mem_stream.h:155
#define assert(condition)
Definition: lz4.c:324
~MEM_STREAM(void)
Definition: mem_stream.h:130
AMI_err read_item(T **elt)
Definition: mem_stream.h:137
MEM_STREAM(T *data, int len)
Definition: mem_stream.h:79
char * sprint()
Definition: mem_stream.h:176
Definition: path.h:16
AMI_err
Definition: ami_stream.h:86